如何在没有 indexedxdb 升级版本的情况下触发 upgradeneeded 事件
how fire upgradeneeded event with out upgrade version of the indedexdb
我对“需要升级”活动有疑问。
每次用户重新加载页面时我都需要检查数据库,但是如何在不升级 indexeddb 版本的情况下触发它,或者这是唯一的解决方案?
request.addEventListener('upgradeneeded', event => {
var db = event.target.result;
var planningObjectStore = db.transaction("planningSave", "read").objectStore("planningSave");
});
"upgradeneeded"
仅在您需要更改架构时触发,您可以通过更改版本号来发出信号。如果您不修改架构 - 例如您只是 reading/writing 到现有的对象存储 - 请改用 "success"
事件。此外,"upgradeneeded"
事件中有一个隐式交易,因此无需在那里调用 transaction()
。
var request = indexedDB.open("mydb", 1); // version 1
// only fires for newly created databases, before "success"
request.addEventListener("upgradeneeded", event => {
var db = event.target.result;
var planningObjectStore = db.createObjectStore("planningSave");
// write initial data into the store
});
// fires after any successful open of the database
request.addEventListener("success", event => {
var db = event.target.result;
var tx = db.transaction("planningSave");
var planningObjectStore = tx.objectStore("planningSave");
// read data within the new transaction
});
我对“需要升级”活动有疑问。
每次用户重新加载页面时我都需要检查数据库,但是如何在不升级 indexeddb 版本的情况下触发它,或者这是唯一的解决方案?
request.addEventListener('upgradeneeded', event => {
var db = event.target.result;
var planningObjectStore = db.transaction("planningSave", "read").objectStore("planningSave");
});
"upgradeneeded"
仅在您需要更改架构时触发,您可以通过更改版本号来发出信号。如果您不修改架构 - 例如您只是 reading/writing 到现有的对象存储 - 请改用 "success"
事件。此外,"upgradeneeded"
事件中有一个隐式交易,因此无需在那里调用 transaction()
。
var request = indexedDB.open("mydb", 1); // version 1
// only fires for newly created databases, before "success"
request.addEventListener("upgradeneeded", event => {
var db = event.target.result;
var planningObjectStore = db.createObjectStore("planningSave");
// write initial data into the store
});
// fires after any successful open of the database
request.addEventListener("success", event => {
var db = event.target.result;
var tx = db.transaction("planningSave");
var planningObjectStore = tx.objectStore("planningSave");
// read data within the new transaction
});