2016-07-09 53 views
0

我目前正在运行带有3个ObjectStores的IndexedDB函数和以下onupgradeneeded函数。保持索引数据库升级时存在ObjectStore数据

var openRequest = indexedDB.open("DB_v3", 1); 
openRequest.onupgradeneeded = function(e) { 
    var thisDB = e.target.result; 
    if(!thisDB.objectStoreNames.contains("ObjectStore1")) { 
     var objectStore = thisDB.createObjectStore("ObjectStore1", {autoIncrement:true}); 
     objectStore.createIndex("name", "name", {unique:true}); 
    } 
    if(!thisDB.objectStoreNames.contains("ObjectStore2")) { 
     //create objectstore & index as above 
    } 
    if(!thisDB.objectStoreNames.contains("ObjectStore3")) { 
     //create objectstore & index as above 
    } 
} 

我现在想要做的是添加第4个ObjectStore。 这对于我更改版本并在onupgradeneeded中添加第4个条目。

var openRequest = indexedDB.open("DB_v4", 1); 
openRequest.onupgradeneeded = function(e) { 

    //same as above 

    if(!thisDB.objectStoreNames.contains("ObjectStore4")) { 
     //create objectstore & index as above 
    } 
} 

所有这一切工作正常,但问题是,一旦onupgradeneeded被调用已有的ObjectStores失去他们的所有数据。
onupgradeneeded如何查看以保留已经存在的ObjectStore的数据?

回答

3

indexedDB.open("DB_v3", 1)打开一个名为DB_v3数据库的版本为1

indexedDB.open("DB_v4", 1)打开一个名为DB_v4数据库的版本为1

数据库使用不同的名称是完全分开的。你不升级,你只是创建两个不同的数据库。如果要升级现有数据库,请保持名称相同并增加版本。那么旧的数据将仍然存在。