2019-12-13 13:10:20 +01:00
|
|
|
import { Stores } from "./types/dbTypes";
|
2019-12-19 13:48:37 +01:00
|
|
|
import { openDatabase, Database, Store, Index } from "./util/query";
|
2019-07-31 01:33:56 +02:00
|
|
|
|
2019-12-19 13:48:37 +01:00
|
|
|
const TALER_DB_NAME = "taler-wallet";
|
2019-07-31 01:33:56 +02:00
|
|
|
|
2019-12-13 13:10:20 +01:00
|
|
|
/**
|
|
|
|
* Current database version, should be incremented
|
|
|
|
* each time we do incompatible schema changes on the database.
|
|
|
|
* In the future we might consider adding migration functions for
|
|
|
|
* each version increment.
|
|
|
|
*/
|
2019-12-19 13:48:37 +01:00
|
|
|
export const WALLET_DB_VERSION = 1;
|
2019-12-13 13:10:20 +01:00
|
|
|
|
2019-07-31 01:33:56 +02:00
|
|
|
/**
|
|
|
|
* Return a promise that resolves
|
|
|
|
* to the taler wallet db.
|
|
|
|
*/
|
2019-12-12 22:39:45 +01:00
|
|
|
export function openTalerDatabase(
|
2019-07-31 01:33:56 +02:00
|
|
|
idbFactory: IDBFactory,
|
|
|
|
onVersionChange: () => void,
|
|
|
|
): Promise<IDBDatabase> {
|
2019-12-19 13:48:37 +01:00
|
|
|
const onUpgradeNeeded = (
|
|
|
|
db: IDBDatabase,
|
|
|
|
oldVersion: number,
|
|
|
|
newVersion: number,
|
2020-04-07 10:07:32 +02:00
|
|
|
): void => {
|
2019-12-19 13:48:37 +01:00
|
|
|
switch (oldVersion) {
|
|
|
|
case 0: // DB does not exist yet
|
|
|
|
for (const n in Stores) {
|
|
|
|
if ((Stores as any)[n] instanceof Store) {
|
|
|
|
const si: Store<any> = (Stores as any)[n];
|
|
|
|
const s = db.createObjectStore(si.name, si.storeParams);
|
|
|
|
for (const indexName in si as any) {
|
|
|
|
if ((si as any)[indexName] instanceof Index) {
|
|
|
|
const ii: Index<any, any> = (si as any)[indexName];
|
|
|
|
s.createIndex(ii.indexName, ii.keyPath, ii.options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Error("unsupported existig DB version");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
return openDatabase(
|
|
|
|
idbFactory,
|
|
|
|
TALER_DB_NAME,
|
|
|
|
WALLET_DB_VERSION,
|
|
|
|
onVersionChange,
|
2019-12-19 13:48:37 +01:00
|
|
|
onUpgradeNeeded,
|
2019-12-12 22:39:45 +01:00
|
|
|
);
|
2019-07-31 01:33:56 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 10:07:32 +02:00
|
|
|
export function deleteTalerDatabase(idbFactory: IDBFactory): void {
|
2019-12-12 22:39:45 +01:00
|
|
|
Database.deleteDatabase(idbFactory, TALER_DB_NAME);
|
2019-12-19 13:48:37 +01:00
|
|
|
}
|