generate db from schema

This commit is contained in:
Florian Dold 2016-10-18 02:07:38 +02:00
parent 218c7d5bd6
commit 6b51e3e48f
3 changed files with 46 additions and 39 deletions

View File

@ -28,6 +28,12 @@ import {IExchangeInfo} from "./types";
const DB_NAME = "taler"; const DB_NAME = "taler";
const DB_VERSION = 10; const DB_VERSION = 10;
import {Stores} from "./wallet";
import {Store, Index} from "./query";
/** /**
* Return a promise that resolves * Return a promise that resolves
@ -47,29 +53,19 @@ export function openTalerDb(): Promise<IDBDatabase> {
console.log("DB: upgrade needed: oldVersion = " + e.oldVersion); console.log("DB: upgrade needed: oldVersion = " + e.oldVersion);
switch (e.oldVersion) { switch (e.oldVersion) {
case 0: // DB does not exist yet 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"}); for (let n in Stores) {
const history = db.createObjectStore("history", if ((Stores as any)[n] instanceof Store) {
{ let si: Store<any> = (Stores as any)[n];
keyPath: "id", const s = db.createObjectStore(si.name, si.storeParams);
autoIncrement: true for (let indexName in (si as any)) {
}); if ((si as any)[indexName] instanceof Index) {
history.createIndex("timestamp", "timestamp"); let ii: Index<any,any> = (si as any)[indexName];
db.createObjectStore("refresh", s.createIndex(ii.indexName, ii.keyPath);
{keyPath: "meltCoinPub"}); }
}
}
}
break; break;
default: default:
if (e.oldVersion != DB_VERSION) { if (e.oldVersion != DB_VERSION) {

View File

@ -27,18 +27,21 @@
export class Store<T> { export class Store<T> {
name: string; name: string;
validator?: (v: T) => T; 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.name = name;
this.validator = validator; this.validator = validator;
this.storeParams = storeParams;
} }
} }
export class Index<S extends IDBValidKey,T> { export class Index<S extends IDBValidKey,T> {
indexName: string; indexName: string;
storeName: 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.storeName = s.name;
this.indexName = indexName; this.indexName = indexName;
} }

View File

@ -305,46 +305,52 @@ function getWithdrawDenomList(amountAvailable: AmountJson,
} }
namespace Stores { export namespace Stores {
class ExchangeStore extends Store<IExchangeInfo> { class ExchangeStore extends Store<IExchangeInfo> {
constructor() { 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> { class CoinsStore extends Store<Coin> {
constructor() { 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> { class HistoryStore extends Store<HistoryRecord> {
constructor() { 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> { class TransactionsStore extends Store<Transaction> {
constructor() { 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 exchanges: ExchangeStore = new ExchangeStore();
export let transactions: TransactionsStore = new TransactionsStore(); 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 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 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; 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) { if (!coin) {
console.error("can't melt coin, it does not exist"); console.error("can't melt coin, it does not exist");
return; return;
@ -1475,7 +1482,8 @@ export class Wallet {
async paymentSucceeded(contractHash: string): Promise<any> { async paymentSucceeded(contractHash: string): Promise<any> {
const doPaymentSucceeded = async() => { 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) { if (!t) {
console.error("contract not found"); console.error("contract not found");
return; return;