2017-09-03 59 views
0

使用需要与IndexedDB集成的Chrome扩展。试图弄清楚如何使用Dexie.JS。发现了一堆样品。这些看起来不太复杂。有一个具体的例子特别有趣在https://github.com/dfahlander/Dexie.js/blob/master/samples/open-existing-db/dump-databases.html倾销indexedDB数据

然而,随着Dexie探索IndexedDB的,当我运行上面的一个 - “转储程序”,它不会看到IndexedDB的数据库,告诉我:There are databases at the current origin.

从开发人员工具Application选项卡下的存储,我看到我的IndexedDB数据库。

这是某种权限问题?任何标签/用户都可以访问任何indexedDB数据库吗?

我该看什么?

谢谢

+2

扩展自己的页面有其自己的起源。内容脚本使用网页来源。 – wOxxOm

回答

0

在镀铬/歌剧,有一个非标准的API webkitGetDatabaseNames()的Dexie.js用于检索当前的源数据库的名称列表。对于其他浏览器,Dexie通过为每个来源保留最新的数据库名称数据库来模拟此API,因此:

对于铬浏览器,Dexie.getDatabaseNames()将列出当前原点的所有数据库,但是对于非铬浏览器,只会显示使用Dexie创建的数据库。

如果您需要转储每个数据库的内容,看看this issue,基本上得出:

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); 
})();