gpt4 book ai didi

javascript - 是否可以中止 IndexedDB 升级事件?

转载 作者:行者123 更新时间:2023-11-30 11:37:05 26 4
gpt4 key购买 nike

我正在尝试实现一个 IndexedDB 接口(interface),该接口(interface)允许用户从以前的数据库 (DB) 版本(在 onupgradeneeded 处理程序中检测到 oldVersionevent.target.oldVersion) 到最新版本(currentVersion 即将开放),这意味着我需要:

  1. 取消或中止currentVersion开启操作
  2. 打开以前的数据库(在onupgradeneeded 中检测到oldVersion)
  3. 读取它的数据
  4. 关闭数据库
  5. (继续打开 currentVersion 数据库的正常过程)

我在打开 oldVersion 时遇到问题,因为我无法中止(异常 11)currentVersion 升级事件(它也是 not cancelable )。

Exception 11: An operation was called on an object on which it is not allowed or at a time when it is not allowed.

是否有可能以某种方式取消或中止 currentVersion 的升级事件,以便我打开 oldVersion...?

注意:...如果没有,是否有任何其他方法可以从我缺少的旧版本数据库中迁移数据?

最佳答案

Is it possible to somehow cancel or abort the upgrade event for currentVersion, in order for me to open the oldVersion...?

var rq = indexedDB.open(name, ver);
rq.onupgradeneeded = function(e) {
rq.transaction.abort();
};
rq.onsuccess = function(e) { console.log('THIS SHOULD NOT RUN'); };
rq.onerror = function(e) { console.log('This should run'); };

is there anyother way to migrate data from older versions of a DB that I'm missing?

通常这就是 upgradeneeded 的全部目的 - 在提供给您的 verionchange 事务期间,您从旧版本迁移数据和模式。

var rq = indexedDB.open(name, 2);
rq.onupgradeneeded = function(e) {
var db = rq.result;
if (e.oldVersion < 1) {
// database didn't exist at all, create new schema
db.createObjectStore('store2');
} else if (e.oldVersion < 2) {
// do the migration - assumes v1 had 'store1'
var store1 = rq.transaction.objectStore('store1');
var store2 = db.createObjectStore('store2');
var r = store1.openCursor();
r.onsuccess = function() {
var cursor = r.result;
if (cursor) {
store2.put(cursor.value, cursor.key);
cursor.continue();
} else {
// migration done, delete old store
db.deleteObjectStore('store1');
}
};
}
};

关于javascript - 是否可以中止 IndexedDB 升级事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com