是否可以中止 IndexedDB 升级事件?
Is it possible to abort an IndexedDB upgrade event?
我正在尝试实现一个 IndexedDB 接口,该接口允许用户从以前的数据库 (DB) 版本迁移数据(oldVersion
在 onupgradeneeded
处理程序中检测到,event.target.oldVersion
)到最新版本(currentVersion
即将开放),这意味着我需要:
- 取消或中止
currentVersion
打开操作
- 打开上一个数据库(
oldVersion
在 onupgradeneeded
中检测到)
- 读取其数据
- 关闭数据库
- (继续打开
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');
}
};
}
};
我正在尝试实现一个 IndexedDB 接口,该接口允许用户从以前的数据库 (DB) 版本迁移数据(oldVersion
在 onupgradeneeded
处理程序中检测到,event.target.oldVersion
)到最新版本(currentVersion
即将开放),这意味着我需要:
- 取消或中止
currentVersion
打开操作 - 打开上一个数据库(
oldVersion
在onupgradeneeded
中检测到) - 读取其数据
- 关闭数据库
- (继续打开
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');
}
};
}
};