如何将新的对象存储添加到现有的indexeddb [英] How to add a new Objectstore to an existing indexeddb

查看:42
本文介绍了如何将新的对象存储添加到现有的indexeddb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何将新的对象存储添加到其中具有版本号的现有indexeddb实例.当我尝试创建新版本时,现有的对象库将被删除.

Can anybody tell me how to add a new objectstore to an existing indexeddb instance which has a version number in it. When I try to create a new version existing object stores are getting deleted.

我有一个版本"1",其中包含大约10个带有数据的对象存储.当我尝试使用新版本号打开同一数据库时,我丢失了当前数据和对象存储.

I have a version '1', which has around 10 object stores with data. When I try to open the same database with new version number, I lost the current data and object stores.

这是我尝试过的.

var _upRequest = indexedDB.open("employees");

    _upRequest.onsuccess = function (e) {
        var thisDb = e.target.result;
        var version = parseInt(thisDb.version)+1;
        thisDb.close();
        var openRequest = indexedDB.open("employees", version);

        //handle setup, this will be run by Firefox
        openRequest.onupgradeneeded = function (e) {
            console.log("running onupgradeneeded");
            var thisDb = e.target.result;

            //Create Employee
            if (!thisDb.objectStoreNames.contains("employee")) {
                console.log("I need to make the employee objectstore");
                var objectStore = thisDb.createObjectStore("employee", { keyPath: "id", autoIncrement: true });
                objectStore.createIndex("searchkey", "searchkey", { unique: false });
            }
            thisDb.close();
        }

        openRequest.onsuccess = function (e) {

            db = e.target.result;

            db.onerror = function (e) {
                alert("Sorry, an unforseen error was thrown.");
                console.log("***ERROR***");
                console.dir(e.target);
            }

            db.close();
        }
    }

推荐答案

我没有直接答案,但是您目前编写的代码对我来说很奇怪.

I don't have a direct answer but your code as it is currently written seems strange to me.

我首先要尝试的一件事是正确绑定onupgradeneed,onerror,而不是过早关闭数据库.

One thing I would try first is properly binding onupgradeneeded, onerror, and not closing the database prematurely.

不要这样做:

var request = indexedDB.open();
request.onsuccess = function() {
  request.onupgradeneeded = function() {};
};

相反,请执行以下操作:

Do this instead:

var request = indexedDB.open();
request.onsuccess = function() {};
request.onupgradeneeded = function() {};

类似地,立即绑定onerror,不仅以后需要onupgrade或成功时也这样,

Similarly, bind onerror immediately, not only later in onupgradeneeded or onsuccess, like this:

var request = indexedDB.open();
request.onsuccess = function() {};
request.onupgradeneeded = function() {};
request.onerror = function() {};

当indexedDB.open检测到更高版本或第一个版本时,它将调度需要升级的事件, wait 以使隐含的"versionchange"事务完成,然后调度成功事件.

When indexedDB.open detects a higher version or first version, it will dispatch an upgradeneeded event, wait for the implied 'versionchange' transaction to complete, and then dispatch a success event.

我不太了解您的代码.我不确定为什么要打开一个连接,将其关闭,然后打开第二个连接,然后再绑定升级所需的处理程序.

I can't quite make sense of your code. I am not sure why you are opening a connection, closing it, then opening a second connection, then late binding the upgradeneeded handler.

这是添加对象存储然后访问它所需要做的一切:

This is all you need to do to add an object store and then access it:

var request = indexedDB.open('name', aNumberGreaterThanTheCurrentVersion);
request.onupgradeneeded = function(event) {
  console.log('Performing upgrade');
  var db = event.target.result;
  console.log('Creating object store');
  db.createObjectStore('mystore');
};

request.onsuccess = function(event) {
  console.log('Connected to database');
  var db = event.target.result;
  var tx = db.transaction('mystore');
  var store = tx.objectStore('mystore');
  console.log('Doing something with store "mystore"');
  // ...
  console.log('Finished doing something, now closing');
  db.close();
};

request.onerror = console.error;

另一个说明. indexedDB会产生不同类型的错误,因此区分它们很重要. request.onerror不会像日志消息所建议的那样引发"异常.相反,发生的是indexedDB调度了一个错误事件.不会抛出调度的错误事件.现在,除此以外,indexedDB的几个函数都可以引发异常.这些需要由try/catch块捕获.您甚至都不会看到调度的错误事件.您将收到一个实际的脚本错误,该脚本错误将停止执行,并且会在控制台中自动显示并带有一些信息提示消息.

Another note. indexedDB produces different types of errors, and it is important to distinguish them. request.onerror doesn't "throw" an exception as your log message suggests. Instead, what happened is that indexedDB dispatched an error event. Dispatched error events are not thrown. Now, with that aside, several of indexedDB's functions can throw exceptions. These need to be caught by a try/catch block. You won't even see a dispatched error event. You will get an actual script error that halts execution and will automatically appear in the console with a slightly-informative message.

这篇关于如何将新的对象存储添加到现有的indexeddb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆