aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-10-18 02:07:38 +0200
committerFlorian Dold <florian.dold@gmail.com>2016-10-18 02:07:38 +0200
commit6b51e3e48f7e0d9bc0ef7b3d64ae7176bd8ce9b4 (patch)
tree7e5081c427d8a45c78c86f0bb3b4136e77c754c3 /lib
parent218c7d5bd6a918bd177982f9728ab809e1a3345b (diff)
generate db from schema
Diffstat (limited to 'lib')
-rw-r--r--lib/wallet/db.ts42
-rw-r--r--lib/wallet/query.ts7
-rw-r--r--lib/wallet/wallet.ts38
3 files changed, 47 insertions, 40 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<IDBDatabase> {
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<any> = (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<any,any> = (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<T> {
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<S extends IDBValidKey,T> {
indexName: string;
storeName: string;
+ keyPath: string | string[];
- constructor(s: Store<T>, indexName: string) {
+ constructor(s: Store<T>, 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<IExchangeInfo> {
constructor() {
- super("exchanges");
+ super("exchanges", {keyPath: "baseUrl"});
}
- pubKeyIndex = new Index<string,IExchangeInfo>(this, "pubKey");
+
+ pubKeyIndex = new Index<string,IExchangeInfo>(this, "pubKey", "masterPublicKey");
}
class CoinsStore extends Store<Coin> {
constructor() {
- super("coins");
+ super("coins", {keyPath: "coinPub"});
}
- exchangeBaseUrlIndex = new Index<string,Coin>(this, "exchangeBaseUrl");
+ exchangeBaseUrlIndex = new Index<string,Coin>(this, "exchangeBaseUrl", "exchageBaseUrl");
}
class HistoryStore extends Store<HistoryRecord> {
constructor() {
- super("history");
+ super("history", {
+ keyPath: "id",
+ autoIncrement: true
+ });
}
- timestampIndex = new Index<number,HistoryRecord>(this, "timestamp");
+ timestampIndex = new Index<number,HistoryRecord>(this, "timestamp", "timestamp");
}
class TransactionsStore extends Store<Transaction> {
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<ReserveRecord> = new Store<ReserveRecord>("reserves");
+ export let reserves: Store<ReserveRecord> = new Store<ReserveRecord>("reserves", {keyPath: "reserve_pub"});
export let coins: CoinsStore = new CoinsStore();
- export let refresh: Store<RefreshSession> = new Store<RefreshSession>("refresh");
+ export let refresh: Store<RefreshSession> = new Store<RefreshSession>("refresh", {keyPath: "meltCoinPub"});
export let history: HistoryStore = new HistoryStore();
- export let precoins: Store<PreCoin> = new Store<PreCoin>("precoins");
+ export let precoins: Store<PreCoin> = new Store<PreCoin>("precoins", {keyPath: "coinPub"});
}
@@ -1270,7 +1276,8 @@ export class Wallet {
return;
}
- let coin = await this.q().get<Coin>(Stores.coins, refreshSession.meltCoinPub);
+ let coin = await this.q().get<Coin>(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<any> {
const doPaymentSucceeded = async() => {
- let t = await this.q().get<Transaction>(Stores.transactions, contractHash);
+ let t = await this.q().get<Transaction>(Stores.transactions,
+ contractHash);
if (!t) {
console.error("contract not found");
return;