我有一个web应用程序,它为indexedDB使用了Dexie包装器,出于某种原因,我需要重新命名现有的数据库,没有任何故障,我无法在Dexie文档上找到重命名。
发布于 2018-03-22 08:44:15
不支持将数据库重命名为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();
});
});
}https://stackoverflow.com/questions/49423263
复制相似问题