在现有的objectStore上创建索引

时间:2018-05-05 20:47:28

标签: javascript indexeddb

作为基本设置的示例,创建了一个索引。

db.onupgradeneeded = function(event) {
   var db = event.target.result;
   var store = db.createObjectStore('name', { keyPath: 'id' });
   store.createIndex('by name', 'name', { unique: false });
};

问题: 是否可以在objectStore future上为同一versionupdate创建/附加更多索引?

因为如果我尝试:

db.onupgradeneeded = function(event) {
   var db = event.target.result;
   var store = db.createObjectStore('name', { keyPath: 'id' });

   store.createIndex('by newName', 'newName', { unique: false });
};

它会抛出current objectStore does already exist的错误。如果我尝试使用事务创建商店引用:

db.onupgradeneeded = function(event) {
   var db = event.target.result;
   var store = db.transaction('name', 'readwrite').objectStore('name');

   store.createIndex('by newName', 'newName', { unique: false });
};

抛出version change transaction is currently running

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。起初可能有点混乱。您希望通过在onupgradeneeded中为您创建的隐式事务来获取现有对象存储。这是 versionchange 类型的事务,它基本上类似于 readwrite 事务,但特定于onupgradeneeded处理函数。

这样的事情:

var request = indexedDB.open(name, oldVersionPlusOne);
request.onupgradeneeded = myOnUpgradeNeeded;

function myOnUpgradeNeeded(event) {
  // Get a reference to the request related to this event
  // @type IDBOpenRequest (a specialized type of IDBRequest)
  var request = event.target;

  // Get a reference to the IDBDatabase object for this request
  // @type IDBDatabase
  var db = request.result;

  // Get a reference to the implicit transaction for this request
  // @type IDBTransaction
  var txn = request.transaction;

  // Now, get a reference to the existing object store
  // @type IDBObjectStore
  var store = txn.objectStore('myStore');

  // Now, optionally inspect index names, or create a new index
  console.log('existing index names in store', store.indexNames);

  // Add a new index to the existing object store
  store.createIndex(...);
}

您还需要注意增加版本以确保调用onupgradeneeded处理函数,并表示您的架构(基本上是表的集合,索引和事物的属性)在新版本中已更改

您还需要重写该功能,以便您只根据版本创建或进行更改。您可以使用event.oldVersion来帮助解决此问题,或db.objectStoreNames.contains

等问题

这样的事情:

function myOnUpgradeNeeded(event) {
  var is_new_db = isNaN(event.oldVersion) || event.oldVersion === 0;
  if(is_new_db) {
    var db = event.target.result;
    var store = db.createObjectStore(...);
    store.createIndex('my-initial-index');
    // Now that you decided you want a second index, you also need 
    // to do this for brand new databases
    store.createIndex('my-second-new-index');
  }

  // But if the database already exists, we are not creating things, 
  // instead we are modifying the existing things to get into the 
  // new state of things we want
  var is_old_db_not_yet_current_version = !isNaN(event.oldVersion) && event.oldVersion < 2;
  if(is_old_db_not_yet_current_version) {
    var txn = event.target.transaction;
    var store = txn.objectStore('store');
    store.createIndex('my-second-new-index');
  }
}

密切关注我使用event.target.transaction代替db.transaction(...)的事实。这些都不是一回事。一个引用现有事务,一个创建一个新事务。

最后,另外,我的个人规则而不是正式的编码要求,你永远不应该在onupgradeneeded中使用db.transaction()。在升级时坚持修改架构,并在其之外进行所有数据更改。