diff options
author | Florian Dold <florian@dold.me> | 2023-08-30 17:18:18 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-08-30 17:18:18 +0200 |
commit | 0a4782a0da631aba31dc0ecef7427df2467cc3e6 (patch) | |
tree | ca7a065a587792f0d9d6b8021db2f96b86c73c39 /packages/taler-wallet-core/src | |
parent | 183aed4ba50e0dd4b9442cd85db8bdd91fa188e5 (diff) | |
download | wallet-core-0a4782a0da631aba31dc0ecef7427df2467cc3e6.tar.xz |
wallet-core: make primitive backup import work
Diffstat (limited to 'packages/taler-wallet-core/src')
-rw-r--r-- | packages/taler-wallet-core/src/db.ts | 78 |
1 files changed, 19 insertions, 59 deletions
diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts index efc0333f4..e68385267 100644 --- a/packages/taler-wallet-core/src/db.ts +++ b/packages/taler-wallet-core/src/db.ts @@ -2890,15 +2890,9 @@ export async function exportDb(idb: IDBFactory): Promise<DbDump> { return dbDump; } -export interface DatabaseDump { - name: string; - stores: { [s: string]: any }; - version: string; -} - async function recoverFromDump( db: IDBDatabase, - dump: DatabaseDump, + dbDump: DbDumpDatabase, ): Promise<void> { return new Promise((resolve, reject) => { const tx = db.transaction(Array.from(db.objectStoreNames), "readwrite"); @@ -2907,67 +2901,33 @@ async function recoverFromDump( }); for (let i = 0; i < db.objectStoreNames.length; i++) { const name = db.objectStoreNames[i]; - const storeDump = dump.stores[name]; + const storeDump = dbDump.stores[name]; if (!storeDump) continue; - Object.keys(storeDump).forEach(async (key) => { - const value = storeDump[key]; - if (!value) return; - tx.objectStore(name).put(value); - }); + for (let rec of storeDump.records) { + tx.objectStore(name).put(rec.value, rec.key); + } } tx.commit(); }); } -export async function importDb(db: IDBDatabase, object: any): Promise<void> { - if ("name" in object && "stores" in object && "version" in object) { - // looks like a database dump - const dump = object as DatabaseDump; - return recoverFromDump(db, dump); - } - - if ("databases" in object && "$types" in object) { - // looks like a IDBDatabase - const someDatabase = object.databases; - - if (TALER_WALLET_META_DB_NAME in someDatabase) { - //looks like a taler database - const currentMainDbValue = - someDatabase[TALER_WALLET_META_DB_NAME].objectStores.metaConfig - .records[0].value.value; - - if (currentMainDbValue !== TALER_WALLET_MAIN_DB_NAME) { - console.log("not the current database version"); - } - - const talerDb = someDatabase[currentMainDbValue]; - - const objectStoreNames = Object.keys(talerDb.objectStores); - - const dump: DatabaseDump = { - name: talerDb.schema.databaseName, - version: talerDb.schema.databaseVersion, - stores: {}, - }; - - for (let i = 0; i < objectStoreNames.length; i++) { - const name = objectStoreNames[i]; - const storeDump = {} as { [s: string]: any }; - dump.stores[name] = storeDump; - talerDb.objectStores[name].records.map((r: any) => { - const pkey = r.primaryKey; - const key = - typeof pkey === "string" || typeof pkey === "number" - ? pkey - : pkey.join(","); - storeDump[key] = r.value; - }); - } +function checkDbDump(x: any): x is DbDump { + return "databases" in x; +} - return recoverFromDump(db, dump); +export async function importDb(db: IDBDatabase, dumpJson: any): Promise<void> { + const d = dumpJson; + if (checkDbDump(d)) { + const walletDb = d.databases[TALER_WALLET_MAIN_DB_NAME]; + if (!walletDb) { + throw Error( + `unable to import, main wallet database (${TALER_WALLET_MAIN_DB_NAME}) not found`, + ); } + await recoverFromDump(db, walletDb); + } else { + throw Error("unable to import, doesn't look like a valid DB dump"); } - throw Error("could not import database"); } export interface FixupDescription { |