如何只在indexeddb中添加一次初始数据

时间:2014-12-08 14:06:21

标签: javascript indexeddb

我正在创建一个indexeddb,并且在其中有几个商店。 创建商店时,必须首先添加一些数据。

我有创建数据库和存储的功能:

function db_init(){
    var request = indexedDB.open("db", "1.0");

    request.onupgradeneeded = function(){
        var db = request.result;
        //store 1
        db.createObjectStore("store1", {keypath: "id", autoIncrement: true});
        //add initial datas;

        //store2
        db.createObjectStore("store2", {keypath: "id", autoIncrement: true});

        //...
        //store 3

        // init necessary databases

        db_populate();
    } 

    request.onsuccess = function (){
        db = request.result;
        populate:db();
    }     
}

在db_populate函数中有4个其他函数,我填充数据存储区:

 function db_populate() {

    init_store1();
    init_store2();
    //...

     console.log("db populated with data");
 }

每个init_score都使用以下事务填充商店:

 var tx = db.transaction("store1", "readwrite");
 var store = tx.objectStore("store1");

现在,我有一个问题。每次打开或刷新页面时,都会复制初始数据。还有一个补充。

当我在onupgradeneeded函数末尾添加db_populate时,出现错误:

  Uncaught TypeError: Cannot read property 'transaction' of undefined

在线:

  var tx = db.transaction ("store1", "readwrite");

我想要实现的是创建一次初始数据的数据存储,就是这样。

知道我做错了吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

这是因为您无法在升级所需事件期间插入数据。您需要做的是在升级后关闭连接并再次重新打开数据插入。

流程应该是这样的:



function db_init() {
  var request = indexedDB.open("db");
  var dbShouldInit = false;
  request.onupgradeneeded = function(e) {
    var db = e.target.result;
    dbShouldInit = true;
    //store 1
    db.createObjectStore("store1", {
      keypath: "id",
      autoIncrement: true
    });
    //add initial datas;

    //store2
    db.createObjectStore("store2", {
      keypath: "id",
      autoIncrement: true
    });

  }
  request.onsuccess = function(e) {
    e.target.result.close();
    if(dbShouldInit)//executes only if DB init happened
      db_populate(); //close the db first and then call init
  }

}

function db_populate() {
  init_store1(init_store2); //pass init 2 as callback 
}

function init_store1(callback) {
  var request = indexedDB.open("db");
  request.onsuccess = function(e) {
    var db = e.target.result;
    var tx = db.transaction("store1", "readwrite");
    var store = tx.objectStore("store1");

    //init code here

    tx.oncomplete = function(e) {
      callback(); //here call the init for second function
    };
  }
}

function init_store2() {
  var request = indexedDB.open("db");
  request.onsuccess = function(e) {
    var db = e.target.result;
    var tx = db.transaction("store2", "readwrite");
    var store = tx.objectStore("store2");

    //init code here

    tx.oncomplete = function(e) {
      //here the app can continue
    };
  }
}




答案 1 :(得分:1)

(function () {
  //indexedDB.deleteDatabase("store");
  var openDB = indexedDB.open("store", 1); 
  openDB.onupgradeneeded = function () {
    var db = openDB.result;
    var store;
    if (!db.objectStoreNames.contains("example")) {
      store = db.createObjectStore("example", {keyPath: "some"});
      store.put({some: "initData"});
    }
  };
  openDB.onsuccess = function (e) {
    var db = e.target.result;
    var rqst = db.transaction("example", "readonly")
                 .objectStore("example")
                 .get("initData");
    rqst.onsuccess = function (e) {
      console.log(rqst.result);
    };
  };
})();