无法使用 dexie 示例代码动态更新索引
Can't update index dynamically with dexie sample code
我一直在尝试动态更新数据库索引,但一直失败,卡住了几天。
我正在使用 angular7 & 类型脚本和最新的 dexie 版本。当我尝试使用相同的代码时,出现错误:
我应该做些什么来让它工作?谢谢!
ERROR Error: Uncaught (in promise): UpgradeError: Dexie specification of currently installed DB version is missing
UpgradeError: Dexie specification of currently installed DB version is missing
我只是将示例代码复制粘贴到这里:
changeSchema(db, schemaChanges) {
db.close();
const newDb = new Dexie(db.name);
newDb.version(db.verno + 1).stores(schemaChanges);
return newDb.open();
}
// Open database dynamically:
async playAround() {
let db = new Dexie('FriendsDatabase');
if (!(await Dexie.exists(db.name))) {
db.version(1).stores({});
}
await db.open();
// Add a table with some indexes:
db = await this.changeSchema(db, { friends: 'id, name' });
// Add another index in the friends table
db = await this.changeSchema(db, { friends: 'id, name, age' });
// Remove the age index again:
db = await this.changeSchema(db, { friends: 'id, name' });
// Remove the friends table
db = await this.changeSchema(db, { friends: null });
}
这个样本有问题。我已经用工作示例更新了文档:
async function changeSchema(db, schemaChanges) {
db.close();
const newDb = new Dexie(db.name);
newDb.on('blocked', ()=>false); // Silence console warning of blocked event.
// Workaround: If DB is empty from tables, it needs to be recreated
if (db.tables.length === 0) {
await db.delete();
newDb.version(1).stores(schemaChanges);
return await newDb.open();
}
// Extract current schema in dexie format:
const currentSchema = db.tables.reduce((result,{name, schema}) => {
result[name] = [
schema.primKey.src,
...schema.indexes.map(idx => idx.src)
].join(',');
return result;
}, {});
console.log("Version: " + db.verno);
console.log("Current Schema: ", currentSchema);
// Tell Dexie about current schema:
newDb.version(db.verno).stores(currentSchema);
// Tell Dexie about next schema:
newDb.version(db.verno + 1).stores(schemaChanges);
// Upgrade it:
return await newDb.open();
}
// Open database dynamically:
async function playAround() {
let db = new Dexie ('FriendsDatabase2');
if (!(await Dexie.exists(db.name))) {
console.log("Db does not exist");
db.version(1).stores({});
}
await db.open();
console.log("Could open DB")
// Add a table with some indexes:
db = await changeSchema(db, {friends: 'id, name'});
console.log("Could enforce friends table with id and name")
// Add another index in the friends table
db = await changeSchema(db, {friends: 'id, name, age'});
console.log("Could add the age index")
// Remove the age index again:
db = await changeSchema(db, {friends: 'id, name'})
console.log("Could remove age index")
// Remove the friends table
db = await changeSchema(db, {friends: null});
console.log("Could delete friends table")
}
playAround().catch(err => console.error(err));
Fiddle:
我一直在尝试动态更新数据库索引,但一直失败,卡住了几天。 我正在使用 angular7 & 类型脚本和最新的 dexie 版本。当我尝试使用相同的代码时,出现错误:
我应该做些什么来让它工作?谢谢!
ERROR Error: Uncaught (in promise): UpgradeError: Dexie specification of currently installed DB version is missing
UpgradeError: Dexie specification of currently installed DB version is missing
我只是将示例代码复制粘贴到这里:
changeSchema(db, schemaChanges) {
db.close();
const newDb = new Dexie(db.name);
newDb.version(db.verno + 1).stores(schemaChanges);
return newDb.open();
}
// Open database dynamically:
async playAround() {
let db = new Dexie('FriendsDatabase');
if (!(await Dexie.exists(db.name))) {
db.version(1).stores({});
}
await db.open();
// Add a table with some indexes:
db = await this.changeSchema(db, { friends: 'id, name' });
// Add another index in the friends table
db = await this.changeSchema(db, { friends: 'id, name, age' });
// Remove the age index again:
db = await this.changeSchema(db, { friends: 'id, name' });
// Remove the friends table
db = await this.changeSchema(db, { friends: null });
}
这个样本有问题。我已经用工作示例更新了文档:
async function changeSchema(db, schemaChanges) {
db.close();
const newDb = new Dexie(db.name);
newDb.on('blocked', ()=>false); // Silence console warning of blocked event.
// Workaround: If DB is empty from tables, it needs to be recreated
if (db.tables.length === 0) {
await db.delete();
newDb.version(1).stores(schemaChanges);
return await newDb.open();
}
// Extract current schema in dexie format:
const currentSchema = db.tables.reduce((result,{name, schema}) => {
result[name] = [
schema.primKey.src,
...schema.indexes.map(idx => idx.src)
].join(',');
return result;
}, {});
console.log("Version: " + db.verno);
console.log("Current Schema: ", currentSchema);
// Tell Dexie about current schema:
newDb.version(db.verno).stores(currentSchema);
// Tell Dexie about next schema:
newDb.version(db.verno + 1).stores(schemaChanges);
// Upgrade it:
return await newDb.open();
}
// Open database dynamically:
async function playAround() {
let db = new Dexie ('FriendsDatabase2');
if (!(await Dexie.exists(db.name))) {
console.log("Db does not exist");
db.version(1).stores({});
}
await db.open();
console.log("Could open DB")
// Add a table with some indexes:
db = await changeSchema(db, {friends: 'id, name'});
console.log("Could enforce friends table with id and name")
// Add another index in the friends table
db = await changeSchema(db, {friends: 'id, name, age'});
console.log("Could add the age index")
// Remove the age index again:
db = await changeSchema(db, {friends: 'id, name'})
console.log("Could remove age index")
// Remove the friends table
db = await changeSchema(db, {friends: null});
console.log("Could delete friends table")
}
playAround().catch(err => console.error(err));
Fiddle: