aboutsummaryrefslogtreecommitdiff
path: root/src/db.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-12-19 13:48:37 +0100
committerFlorian Dold <florian.dold@gmail.com>2019-12-19 13:48:37 +0100
commit49e3b3e5b9bbf1ce356ef68f301d50c689ceecb9 (patch)
tree7a0afa6a1920bdb3d60b27f951fa78adba7541ad /src/db.ts
parent20ebc44420c76207fa197b7c53201429ffd89bde (diff)
prepare for schema migrations
Diffstat (limited to 'src/db.ts')
-rw-r--r--src/db.ts37
1 files changed, 30 insertions, 7 deletions
diff --git a/src/db.ts b/src/db.ts
index e1c5c8f85..da43cf827 100644
--- a/src/db.ts
+++ b/src/db.ts
@@ -1,7 +1,7 @@
import { Stores } from "./types/dbTypes";
-import { openDatabase, Database } from "./util/query";
+import { openDatabase, Database, Store, Index } from "./util/query";
-const TALER_DB_NAME = "taler";
+const TALER_DB_NAME = "taler-wallet";
/**
* Current database version, should be incremented
@@ -9,7 +9,7 @@ const TALER_DB_NAME = "taler";
* In the future we might consider adding migration functions for
* each version increment.
*/
-export const WALLET_DB_VERSION = 28;
+export const WALLET_DB_VERSION = 1;
/**
* Return a promise that resolves
@@ -18,18 +18,41 @@ export const WALLET_DB_VERSION = 28;
export function openTalerDatabase(
idbFactory: IDBFactory,
onVersionChange: () => void,
- onUpgradeUnsupported: (oldVersion: number, newVersion: number) => void,
): Promise<IDBDatabase> {
+ const onUpgradeNeeded = (
+ db: IDBDatabase,
+ oldVersion: number,
+ newVersion: number,
+ ) => {
+ 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");
+ }
+ };
+
return openDatabase(
idbFactory,
TALER_DB_NAME,
WALLET_DB_VERSION,
- Stores,
onVersionChange,
- onUpgradeUnsupported,
+ onUpgradeNeeded,
);
}
export function deleteTalerDatabase(idbFactory: IDBFactory) {
Database.deleteDatabase(idbFactory, TALER_DB_NAME);
-} \ No newline at end of file
+}