diff options
Diffstat (limited to 'packages/taler-wallet-core/src/db.ts')
-rw-r--r-- | packages/taler-wallet-core/src/db.ts | 78 |
1 files changed, 53 insertions, 25 deletions
diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts index ba5295dda..efc0333f4 100644 --- a/packages/taler-wallet-core/src/db.ts +++ b/packages/taler-wallet-core/src/db.ts @@ -104,7 +104,7 @@ import { RetryInfo, TaskIdentifiers } from "./operations/common.js"; * for all previous versions must be written, which should be * avoided. */ -export const TALER_DB_NAME = "taler-wallet-main-v9"; +export const TALER_WALLET_MAIN_DB_NAME = "taler-wallet-main-v9"; /** * Name of the metadata database. This database is used @@ -112,7 +112,7 @@ export const TALER_DB_NAME = "taler-wallet-main-v9"; * * (Minor migrations are handled via upgrade transactions.) */ -export const TALER_META_DB_NAME = "taler-wallet-meta"; +export const TALER_WALLET_META_DB_NAME = "taler-wallet-meta"; export const CURRENT_DB_CONFIG_KEY = "currentMainDbName"; @@ -2806,25 +2806,36 @@ export interface DbDump { }; } -export function exportDb(db: IDBDatabase): Promise<DbDump> { - const dbDump: DbDump = { - databases: {}, - }; +export async function exportSingleDb( + idb: IDBFactory, + dbName: string, +): Promise<DbDumpDatabase> { + const myDb = await openDatabase( + idb, + dbName, + undefined, + () => { + // May not happen, since we're not requesting a specific version + throw Error("unexpected version change"); + }, + () => { + logger.info("unexpected onupgradeneeded"); + }, + ); - const walletDb: DbDumpDatabase = { - version: db.version, + const singleDbDump: DbDumpDatabase = { + version: myDb.version, stores: {}, }; - dbDump.databases[db.name] = walletDb; return new Promise((resolve, reject) => { - const tx = db.transaction(Array.from(db.objectStoreNames)); + const tx = myDb.transaction(Array.from(myDb.objectStoreNames)); tx.addEventListener("complete", () => { - resolve(dbDump); + resolve(singleDbDump); }); // tslint:disable-next-line:prefer-for-of - for (let i = 0; i < db.objectStoreNames.length; i++) { - const name = db.objectStoreNames[i]; + for (let i = 0; i < myDb.objectStoreNames.length; i++) { + const name = myDb.objectStoreNames[i]; const store = tx.objectStore(name); const storeDump: DbStoreDump = { autoIncrement: store.autoIncrement, @@ -2842,7 +2853,7 @@ export function exportDb(db: IDBDatabase): Promise<DbDump> { unique: index.unique, }; } - walletDb.stores[name] = storeDump; + singleDbDump.stores[name] = storeDump; store.openCursor().addEventListener("success", (e: Event) => { const cursor = (e.target as any).result; if (cursor) { @@ -2862,6 +2873,23 @@ export function exportDb(db: IDBDatabase): Promise<DbDump> { }); } +export async function exportDb(idb: IDBFactory): Promise<DbDump> { + const dbDump: DbDump = { + databases: {}, + }; + + dbDump.databases[TALER_WALLET_META_DB_NAME] = await exportSingleDb( + idb, + TALER_WALLET_META_DB_NAME, + ); + dbDump.databases[TALER_WALLET_MAIN_DB_NAME] = await exportSingleDb( + idb, + TALER_WALLET_MAIN_DB_NAME, + ); + + return dbDump; +} + export interface DatabaseDump { name: string; stores: { [s: string]: any }; @@ -2902,13 +2930,13 @@ export async function importDb(db: IDBDatabase, object: any): Promise<void> { // looks like a IDBDatabase const someDatabase = object.databases; - if (TALER_META_DB_NAME in someDatabase) { + if (TALER_WALLET_META_DB_NAME in someDatabase) { //looks like a taler database const currentMainDbValue = - someDatabase[TALER_META_DB_NAME].objectStores.metaConfig.records[0] - .value.value; + someDatabase[TALER_WALLET_META_DB_NAME].objectStores.metaConfig + .records[0].value.value; - if (currentMainDbValue !== TALER_DB_NAME) { + if (currentMainDbValue !== TALER_WALLET_MAIN_DB_NAME) { console.log("not the current database version"); } @@ -3236,7 +3264,7 @@ export async function openTalerDatabase( ): Promise<DbAccess<typeof WalletStoresV1>> { const metaDbHandle = await openDatabase( idbFactory, - TALER_META_DB_NAME, + TALER_WALLET_META_DB_NAME, 1, () => {}, onMetaDbUpgradeNeeded, @@ -3249,17 +3277,17 @@ export async function openTalerDatabase( .runReadWrite(async (tx) => { const dbVersionRecord = await tx.metaConfig.get(CURRENT_DB_CONFIG_KEY); if (!dbVersionRecord) { - currentMainVersion = TALER_DB_NAME; + currentMainVersion = TALER_WALLET_MAIN_DB_NAME; await tx.metaConfig.put({ key: CURRENT_DB_CONFIG_KEY, - value: TALER_DB_NAME, + value: TALER_WALLET_MAIN_DB_NAME, }); } else { currentMainVersion = dbVersionRecord.value; } }); - if (currentMainVersion !== TALER_DB_NAME) { + if (currentMainVersion !== TALER_WALLET_MAIN_DB_NAME) { switch (currentMainVersion) { case "taler-wallet-main-v2": case "taler-wallet-main-v3": @@ -3275,7 +3303,7 @@ export async function openTalerDatabase( .runReadWrite(async (tx) => { await tx.metaConfig.put({ key: CURRENT_DB_CONFIG_KEY, - value: TALER_DB_NAME, + value: TALER_WALLET_MAIN_DB_NAME, }); }); break; @@ -3288,7 +3316,7 @@ export async function openTalerDatabase( const mainDbHandle = await openDatabase( idbFactory, - TALER_DB_NAME, + TALER_WALLET_MAIN_DB_NAME, WALLET_DB_MINOR_VERSION, onVersionChange, onTalerDbUpgradeNeeded, @@ -3305,7 +3333,7 @@ export async function deleteTalerDatabase( idbFactory: IDBFactory, ): Promise<void> { return new Promise((resolve, reject) => { - const req = idbFactory.deleteDatabase(TALER_DB_NAME); + const req = idbFactory.deleteDatabase(TALER_WALLET_MAIN_DB_NAME); req.onerror = () => reject(req.error); req.onsuccess = () => resolve(); }); |