diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-04-13 15:05:38 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-04-13 15:05:38 +0200 |
commit | ace1a1be349c279fe87b536f786bb32c4e17f889 (patch) | |
tree | ea21026e1007588d79c2c4322476856c5f41357b /src/wxBackend.ts | |
parent | 0ef5140e334085f150a60da6f6233dc2ecfb4a89 (diff) | |
download | wallet-core-ace1a1be349c279fe87b536f786bb32c4e17f889.tar.xz |
download/import db from dump page
Diffstat (limited to 'src/wxBackend.ts')
-rw-r--r-- | src/wxBackend.ts | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/wxBackend.ts b/src/wxBackend.ts index 7c3143136..7b1a2b35e 100644 --- a/src/wxBackend.ts +++ b/src/wxBackend.ts @@ -60,6 +60,9 @@ function makeHandlers(db: IDBDatabase, ["dump-db"]: function (detail, sender) { return exportDb(db); }, + ["import-db"]: function (detail, sender) { + return importDb(db, detail.dump); + }, ["get-tab-cookie"]: function (detail, sender) { if (!sender || !sender.tab || !sender.tab.id) { return Promise.resolve(); @@ -634,6 +637,30 @@ function exportDb(db: IDBDatabase): Promise<any> { }); } + +function importDb(db: IDBDatabase, dump: any): Promise<void> { + console.log("importing db", dump); + return new Promise((resolve, reject) => { + let tx = db.transaction(Array.from(db.objectStoreNames), "readwrite"); + for (let storeName in dump.stores) { + let objects = []; + for (let key in dump.stores[storeName]) { + objects.push(dump.stores[storeName][key]); + } + console.log(`importing ${objects.length} records into ${storeName}`); + let store = tx.objectStore(storeName); + let clearReq = store.clear(); + for (let obj of objects) { + store.put(obj); + } + } + tx.addEventListener("complete", () => { + resolve(); + }); + }); +} + + function deleteDb() { indexedDB.deleteDatabase(DB_NAME); } |