3

我正在使用 IndexedDB 开发离线网络应用程序。所以我想了很多关于版本更改时的数据迁移。

例如,我在 DB 版本 3 中有 3 个 ObjectStore。现在我注意到,我应该在所有 3 个 ObjectStore 中都有一个特定的索引。但是不可能在不丢失数据的情况下将索引添加到现有的 ObjectStore。

在“onupgradeneeded”事件中处理数据迁移的解决方案是什么?

4

3 回答 3

9

无需杀死 StoreObject 只需像这样更新它:

request.onupgradeneeded = function(evt) {

        var dataBase = evt.target.result;
        var txn = evt.target.transaction;
        //////////
        var storeCreateIndex = function (objectStore, name, options) {
            if (!objectStore.indexNames.contains(name)) {
                objectStore.createIndex(name, name, options);
            }
        }
        //////////
        var catalogItem, mangaItem, chapterItem, artworkItem;
        if (evt.newVersion != evt.oldVersion) {
            // Get exiting objectStore
            catalogItem = txn.objectStore('CatalogItem');
            mangaItem = txn.objectStore('MangaItem');
            chapterItem = txn.objectStore('ChapterItem');
            artworkItem = txn.objectStore('ArtworkList');
        } else {
            // Fist creation of database objectStore
            catalogItem = dataBase.db.createObjectStore("CatalogItem", { keyPath: "key" });
            mangaItem = dataBase.db.createObjectStore("MangaItem", { keyPath: "key" });
            chapterItem = dataBase.db.createObjectStore("ChapterItem", { keyPath: "key" });
            artworkItem = dataBase.db.createObjectStore("ArtworkList", { keyPath: "key" });
        }
        //////////
        storeCreateIndex(catalogItem, "popularity", { unique: false });
        storeCreateIndex(catalogItem, "author", { unique: false });
        storeCreateIndex(catalogItem, "status", { unique: false });
        storeCreateIndex(catalogItem, "isFavorite", { unique: false });
        storeCreateIndex(chapterItem, "isBookmarked", { unique: false });
        storeCreateIndex(chapterItem, "isDownloaded", { unique: false });
}
于 2014-01-12T18:41:35.913 回答
0

如上面的评论所述:

尝试从“onupgradeneeded”启动新事务将导致错误:

InvalidStateError:DOM IDBDatabase 异常 11

而是使用请求对象引用的事务。例子:

function opendb(oncomplete){
  var version = 1;
  var migrateobjects = [];
  var request = indexedDB.open('mydb', version);

  request.onupgradeneeded = function(e) {
    db = e.target.result;
    transaction = e.target.transaction;

    if(db.objectStoreNames.contains('myobjects')){ 
      migraterequest = transaction.objectStore('myobjects').openCursor();
      migraterequest.onsuccess = function(e){ 
        var cursor = e.target.result;
        if (cursor){
          migrateobjects.push(cursor.value);
          cursor.continue();
        }
      };
      db.deleteObjectStore('myobjects');
    }

    var store = db.createObjectStore('myobjects', {keyPath: 'id'});
  };

  request.onsuccess = function(e) {
    db = e.target.result;
    transaction = db.transaction('myobjects', 'readwrite')
    store = transaction.objectStore('myobjects');
    for(var i=0; i < migrateobjects.length; ++i)
      store.put(migrateobjects[i]);
    transaction.oncomplete = oncomplete;
  };
};
于 2013-05-20T19:45:41.027 回答
0

除非新索引违反现有记录的数据库约束,否则更改索引​​不应清除现有记录。但我确实观察到对象存储在 Chrome 中消失了,但在 Firefox 中没有。

于 2014-01-13T00:54:28.853 回答