【发布时间】:2018-08-31 13:44:09
【问题描述】:
我有一个使用 Dexie 包装器进行 indexedDB 的 Web 应用程序,由于某种原因,我需要重命名现有数据库而不会出现故障,我在 Dexie 文档中找不到重命名。
【问题讨论】:
标签: javascript html database migration dexie
我有一个使用 Dexie 包装器进行 indexedDB 的 Web 应用程序,由于某种原因,我需要重命名现有数据库而不会出现故障,我在 Dexie 文档中找不到重命名。
【问题讨论】:
标签: javascript html database migration dexie
不支持重命名数据库既不是 Dexie 也不是原生 indexedDB。但是您可以使用以下代码(未经测试)克隆数据库:
function cloneDatabase (sourceName, destinationName) {
//
// Open source database
//
const origDb = new Dexie(sourceName);
return origDb.open().then(()=> {
// Create the destination database
const destDb = new Dexie(destinationName);
//
// Clone Schema
//
const schema = origDb.tables.reduce((result,table)=>{
result[table.name] = [table.schema.primKey]
.concat(table.schema.indexes)
.map(indexSpec => indexSpec.src);
return result;
}, {});
destDb.version(origDb.verno).stores(schema);
//
// Clone Data
//
return origDb.tables.reduce(
(prev, table) => prev
.then(() => table.toArray())
.then(rows => destDb.table(table.name).bulkAdd(rows)),
Promise.resolve()
).then(()=>{
//
// Finally close the databases
//
origDb.close();
destDb.close();
});
});
}
【讨论】: