gnosis23 / hello-world-blog

还是 issues 里面写文章方便
https://bohao.work
0 stars 0 forks source link

IndexedDB 有何用 #47

Closed gnosis23 closed 4 years ago

gnosis23 commented 5 years ago

这玩意就存储量大点咯?

入门教程

object stores

类似于 Table 。

var dbPromise = idb.open('test-db3', 1, function(upgradeDb) {
  if (!upgradeDb.objectStoreNames.contains('people')) {
    // define primary key
    upgradeDb.createObjectStore('people', {keyPath: 'email'});
  }
  if (!upgradeDb.objectStoreNames.contains('notes')) {
    // define primary key, auto increment
    upgradeDb.createObjectStore('notes', {autoIncrement: true});
  }
  if (!upgradeDb.objectStoreNames.contains('logs')) {
    // define primary key id, auto increment
    upgradeDb.createObjectStore('logs', {keyPath: 'id', autoIncrement: true});
  }
});

Indexes

索引......

// objectStore.createIndex('indexName', 'property', options)
var dbPromise = idb.open('test-db4', 1, function(upgradeDb) {
  if (!upgradeDb.objectStoreNames.contains('people')) {
    var peopleOS = upgradeDb.createObjectStore('people', {keyPath: 'email'});
    peopleOS.createIndex('gender', 'gender', {unique: false});
    peopleOS.createIndex('ssn', 'ssn', {unique: true});
  }
  if (!upgradeDb.objectStoreNames.contains('notes')) {
    var notesOS = upgradeDb.createObjectStore('notes', {autoIncrement: true});
    notesOS.createIndex('title', 'title', {unique: false});
  }
  if (!upgradeDb.objectStoreNames.contains('logs')) {
    var logsOS = upgradeDb.createObjectStore('logs', {keyPath: 'id',
      autoIncrement: true});
  }
});