开发一个Chrome扩展,它需要与IndexedDB集成。试图找出如何使用Dexie.JS
。发现了一堆样本。看起来不太复杂。有一个特别有趣的例子,用于在IndexedDB上使用Dexie开发https://github.com/dfahlander/Dexie.js/blob/master/samples/open-existing-db/dump-databases.html。
但是,当我运行上面的“转储实用程序”时,它没有看到IndexedDB数据库,告诉我:There are databases at the current origin.
。
在developer Application
选项卡中,在Storage下面,我看到了我的IndexedDB
数据库。
这是某种权限问题吗?任何选项卡/用户可以访问任何indexedDB数据库吗?
我该看什么?
谢谢
发布于 2017-09-04 19:57:46
在chrome/opera中,有一个非标准的API webkitGetDatabaseNames(),Dexie.js用于检索当前原点上的数据库名称列表。对于其他浏览器,Dexie通过为每个源保存数据库名的最新数据库来模拟此API,因此:
对于铬浏览器,Dexie.getDatabaseNames()将列出当前原点的所有数据库,但对于非铬浏览器,将只显示用德西创建的数据库。
如果您需要转储每个数据库的内容,请查看本期,这基本上提供:
interface TableDump {
table: string
rows: any[]
}
function export(db: Dexie): TableDump[] {
return db.transaction('r', db.tables, ()=>{
return Promise.all(
db.tables.map(table => table.toArray()
.then(rows => ({table: table.name, rows: rows})));
});
}
function import(data: TableDump[], db: Dexie) {
return db.transaction('rw', db.tables, () => {
return Promise.all(data.map (t =>
db.table(t.table).clear()
.then(()=>db.table(t.table).bulkAdd(t.rows)));
});
}
将这些函数与JSON.stringify()和JSON.parse()组合起来,以完全序列化数据。
const db = new Dexie('mydb');
db.version(1).stores({friends: '++id,name,age'});
(async ()=>{
// Export
const allData = await export (db);
const serialized = JSON.stringify(allData);
// Import
const jsonToImport = '[{"table": "friends", "rows": [{id:1,name:"foo",age:33}]}]';
const dataToImport = JSON.parse(jsonToImport);
await import(dataToImport, db);
})();
https://stackoverflow.com/questions/46025699
复制相似问题