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_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) {

View File

@ -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;
}

View File

@ -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;