【问题标题】:How do you create a second store in indexeddb?如何在 indexeddb 中创建第二个存储?
【发布时间】:2019-11-13 21:09:04
【问题描述】:

所以这是我的第一个方法。 我创建了一个商店并创建了一个索引“groupname”,该值是具有多个键和值的整个对象。这可行,但是当我尝试添加另一个商店时,商店出现并且索引显示但值是空的......

有人对如何创建第二个 storeobject 有什么建议吗?

var dbName = "DSD DB";
var request = window.indexedDB.open(dbName, 2);
var db;

request.onerror = function(event) {
    console.log(event.target.errorCode);
};

request.onsuccess = function(event) {
    db = event.target.result;
    console.log(db);
};

// This event is only implemented in recent browsers   
request.onupgradeneeded = function(event) {
    // Save the IDBDatabase interface 
    var db = event.target.result;

    // Create an objectStore for this database
    var objectStore = db.createObjectStore("groups", { keyPath: "groupname" });

    objectStore.transaction.oncomplete = function(event) {
        // Store values in the newly created objectStore.
        var groupObjectStore = db.transaction("groups", "readwrite").objectStore("groups");
        jsonReceivingOneGroup.groups.forEach(function(group) {
            groupObjectStore.add(group);
        });
        console.log(groupObjectStore);
    };
};

提前致谢!

【问题讨论】:

    标签: jquery transactions local-storage indexeddb idbobjectstore


    【解决方案1】:
    // This event is only implemented in recent browsers   
    request.onupgradeneeded = function(event) {
        // Save the IDBDatabase interface 
        var db = event.target.result;
    
        // Create an objectStore for this database
        var objectStore = db.createObjectStore("groups", { keyPath: "groupname" });
    
        // Create a second object store
        var aSecondObjectStore = db.createObjectStore("foo");
    
        var aThirdObjectStore = db.createObjectStore("bar");
    
        var aFourthObjectStore = db.createObjectStore("baz");
    
        // etc...
    };
    

    如果要修改现有数据库并向其添加存储,则需要打开与版本号比以前更高的数据库的连接。 onupgradeneeded 仅在 (a) 首次创建数据库或 (b) 以更高版本号打开数据库时调用。

    首先,修改您的onupgradeneeded 函数以创建额外的对象存储。接下来,打开更高版本号的数据库。打开更高版本号的数据库:

    var openRequest = indexedDB.open(dbName, 3);
    

    我在这里使用 3 只是因为您的示例显示 2,而 3 比 2 大一。

    然后,当你走到这一步时,你会得到一个错误。当您使用版本 3 打开并运行 onupgradeneeded 函数时,它将尝试再次创建 groups 对象存储。但是那家店已经存在了。所以现在你需要引入一些逻辑来检查它。有几种方法可以做到这一点。一种简单的方法是检查商店是否已经存在,如果商店不存在,则仅创建商店。要检查数据库中是否已经存在对象存储,可以使用db.objectStoreNames.contains

    // This event is only implemented in recent browsers   
    request.onupgradeneeded = function(event) {
        // Save the IDBDatabase interface 
        var db = event.target.result;
    
        // Get the list of existing stores, if any
        var stores = db.objectStoreNames;
    
        // Check whether the store exists before creating it
        if (!stores.contains("groups")) {
          // Create an objectStore for this database
          var objectStore = db.createObjectStore("groups", { keyPath: "groupname" });
        }
    };
    

    【讨论】:

    • 谢谢@Josh。我想我困惑的地方是,我是在原始代码行中更改版本号还是我写一个新的并在那里增加版本?
    • 写新的onupgradeneeded,然后改版本@leenbean16
    【解决方案2】:

    我能够通过 ajax 调用创建另一个包含类似数据的表:

    var transaction = db.transaction(["pallets"], "readwrite");
    var objectStore = transaction.objectStore("pallets");
    
    $.each(masterJson.pallets, function(i, pallet) {
        var request = objectStore.put(pallet);
    })
    
    request.onsuccess = function(event) {
        console.log(event.target.response);
    };
    

    这个链接真的很有帮助:https://dzone.com/articles/complete-sample-app-indexeddb

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 1970-01-01
      • 2017-09-08
      • 2014-03-10
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多