如何在onupgradeneeded中为indexedDB中的两个存储创建数据

时间:2015-12-08 12:33:48

标签: indexeddb

我可以问一下如何在创建商店的时候在indexedDb中的两个商店(表格)中创建数据吗?

在此代码中:jsfiddle

$( document ).ready(function() {

function open() {
  indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

    var version = 1;
    var request = indexedDB.open("appaa", version);

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

        e.target.transaction.onerror = indexedDB.onerror;

        if(db.objectStoreNames.contains("tab1")) {
            db.deleteObjectStore("tab1");
        }

        var storeColl = db.createObjectStore("tab1", {
            keyPath: "id", autoIncrement : true
        });
        storeColl.createIndex("name", "name", { unique: true });
        storeColl.createIndex("description", "description", { unique: false });

        storeColl.transaction.oncomplete = function(event) {
            var collObjectStore = db.transaction("tab1", "readwrite").objectStore("tab1");
            collObjectStore.add({name: "a", description: "b"});
            collObjectStore.add({name: "c", description: "d"});                
        };


      if(db.objectStoreNames.contains("tab2")) {
            db.deleteObjectStore("tab2");
        }

        var storeColl2 = db.createObjectStore("tab2", {
            keyPath: "id", autoIncrement : true
        });
        storeColl2.createIndex("name2", "name2", { unique: true });
        storeColl2.createIndex("description2", "description2", { unique: false });

        storeColl2.transaction.oncomplete = function(event) {
            var collObjectStore2 = db.transaction("tab2", "readwrite").objectStore("tab2");
            collObjectStore2.add({name: "a2", description: "b2"});
            collObjectStore2.add({name: "c2", description: "d2"});                
        };



    };

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

    };

    request.onerror = function(){

    };


  };

open();

});

如果我只创建一个商店,则会添加数据。但是,如果我创建两个商店,则仅在第二个商店中创建数据。

2 个答案:

答案 0 :(得分:1)

虽然storeColl引用了两个不同的对象存储,但是对storeColl.transaction的两次调用都返回相同的事务对象 - " versionchange"作为升级过程的一部分自动创建的事务。所以当你的代码执行时:

storeColl.transaction.oncomplete = ...;

...

storeColl.transaction.oncomplete = ...;

oncomplete的第二个赋值将覆盖第一个,而且只分配给oncomplete的最后一个处理程序。

一个简单的解决方法是使用storeColl.transaction.addEventListener('complete', ...);代替,因为你可以通过这种方式添加多个事件监听器。

答案 1 :(得分:1)

您无需等待createObjectStore完成。您可以使用event.target.transaction立即存储数据。这不是推荐的做法。但是,这是一个例子:

function onUpgradeNeeded(event) {
  // ...

  // Create the object stores
  var db = event.target.result;
  var tab1Create = db.createObjectStore("tab1", {
    keyPath: "id", autoIncrement : true
  });

  var tab2Create = db.createObjectStore("tab2", {
    keyPath: "id", autoIncrement : true
  });

  // ...

  // Time to add some data:
  // Get a reference to the current transaction. The transaction 
  // type is 'versionchange', which allows for 'readwrite' operations
  var transaction = event.target.transaction;

  // Access the object stores using the transaction and add objects
  var tab1 = transaction.objectStore('tab1');
  tab1.add({name: "a", description: "b"});
  tab1.add({name: "c", description: "d"});
  var tab2 = transaction.objectStore('tab2');
  tab2.add({name: "a2", description: "b2"});
  tab2.add({name: "c2", description: "d2"});
}