From 6b51e3e48f7e0d9bc0ef7b3d64ae7176bd8ce9b4 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 18 Oct 2016 02:07:38 +0200 Subject: [PATCH] generate db from schema --- lib/wallet/db.ts | 40 ++++++++++++++++++---------------------- lib/wallet/query.ts | 7 +++++-- lib/wallet/wallet.ts | 38 +++++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/lib/wallet/db.ts b/lib/wallet/db.ts index a78abc26a..d4ae1cd48 100644 --- a/lib/wallet/db.ts +++ b/lib/wallet/db.ts @@ -28,6 +28,12 @@ import {IExchangeInfo} from "./types"; const DB_NAME = "taler"; const DB_VERSION = 10; +import {Stores} from "./wallet"; +import {Store, Index} from "./query"; + + + + /** * Return a promise that resolves @@ -47,29 +53,19 @@ export function openTalerDb(): Promise { console.log("DB: upgrade needed: oldVersion = " + e.oldVersion); switch (e.oldVersion) { case 0: // DB does not exist yet - const exchanges = db.createObjectStore("exchanges", - {keyPath: "baseUrl"}); - exchanges.createIndex("pubKey", "masterPublicKey"); - db.createObjectStore("reserves", {keyPath: "reserve_pub"}); - const coins = db.createObjectStore("coins", {keyPath: "coinPub"}); - coins.createIndex("exchangeBaseUrl", "exchangeBaseUrl"); - const transactions = db.createObjectStore("transactions", - {keyPath: "contractHash"}); - transactions.createIndex("repurchase", - [ - "contract.merchant_pub", - "contract.repurchase_correlation_id" - ]); - db.createObjectStore("precoins", {keyPath: "coinPub"}); - const history = db.createObjectStore("history", - { - keyPath: "id", - autoIncrement: true - }); - history.createIndex("timestamp", "timestamp"); - db.createObjectStore("refresh", - {keyPath: "meltCoinPub"}); + for (let n in Stores) { + if ((Stores as any)[n] instanceof Store) { + let si: Store = (Stores as any)[n]; + const s = db.createObjectStore(si.name, si.storeParams); + for (let indexName in (si as any)) { + if ((si as any)[indexName] instanceof Index) { + let ii: Index = (si as any)[indexName]; + s.createIndex(ii.indexName, ii.keyPath); + } + } + } + } break; default: if (e.oldVersion != DB_VERSION) { diff --git a/lib/wallet/query.ts b/lib/wallet/query.ts index ddd22b4cf..6255ffb94 100644 --- a/lib/wallet/query.ts +++ b/lib/wallet/query.ts @@ -27,18 +27,21 @@ export class Store { name: string; validator?: (v: T) => T; + storeParams: IDBObjectStoreParameters; - constructor(name: string, validator?: (v: T) => T) { + constructor(name: string, storeParams: IDBObjectStoreParameters, validator?: (v: T) => T) { this.name = name; this.validator = validator; + this.storeParams = storeParams; } } export class Index { indexName: string; storeName: string; + keyPath: string | string[]; - constructor(s: Store, indexName: string) { + constructor(s: Store, indexName: string, keyPath: string | string[]) { this.storeName = s.name; this.indexName = indexName; } diff --git a/lib/wallet/wallet.ts b/lib/wallet/wallet.ts index f55ba2170..e39b492ed 100644 --- a/lib/wallet/wallet.ts +++ b/lib/wallet/wallet.ts @@ -305,46 +305,52 @@ function getWithdrawDenomList(amountAvailable: AmountJson, } -namespace Stores { +export namespace Stores { class ExchangeStore extends Store { constructor() { - super("exchanges"); + super("exchanges", {keyPath: "baseUrl"}); } - pubKeyIndex = new Index(this, "pubKey"); + + pubKeyIndex = new Index(this, "pubKey", "masterPublicKey"); } class CoinsStore extends Store { constructor() { - super("coins"); + super("coins", {keyPath: "coinPub"}); } - exchangeBaseUrlIndex = new Index(this, "exchangeBaseUrl"); + exchangeBaseUrlIndex = new Index(this, "exchangeBaseUrl", "exchageBaseUrl"); } class HistoryStore extends Store { constructor() { - super("history"); + super("history", { + keyPath: "id", + autoIncrement: true + }); } - timestampIndex = new Index(this, "timestamp"); + timestampIndex = new Index(this, "timestamp", "timestamp"); } class TransactionsStore extends Store { constructor() { - super("transactions"); + super("transactions", {keyPath: "contractHash"}); } - repurchaseIndex = new Index<[string,string],Transaction>(this, "repurchase"); + repurchaseIndex = new Index<[string,string],Transaction>(this, "repurchase", [ + "contract.merchant_pub", + "contract.repurchase_correlation_id" + ]); } - export let exchanges: ExchangeStore = new ExchangeStore(); export let transactions: TransactionsStore = new TransactionsStore(); - export let reserves: Store = new Store("reserves"); + export let reserves: Store = new Store("reserves", {keyPath: "reserve_pub"}); export let coins: CoinsStore = new CoinsStore(); - export let refresh: Store = new Store("refresh"); + export let refresh: Store = new Store("refresh", {keyPath: "meltCoinPub"}); export let history: HistoryStore = new HistoryStore(); - export let precoins: Store = new Store("precoins"); + export let precoins: Store = new Store("precoins", {keyPath: "coinPub"}); } @@ -1270,7 +1276,8 @@ export class Wallet { return; } - let coin = await this.q().get(Stores.coins, refreshSession.meltCoinPub); + let coin = await this.q().get(Stores.coins, + refreshSession.meltCoinPub); if (!coin) { console.error("can't melt coin, it does not exist"); return; @@ -1475,7 +1482,8 @@ export class Wallet { async paymentSucceeded(contractHash: string): Promise { const doPaymentSucceeded = async() => { - let t = await this.q().get(Stores.transactions, contractHash); + let t = await this.q().get(Stores.transactions, + contractHash); if (!t) { console.error("contract not found"); return;