diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-18 01:47:40 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-18 01:47:40 +0200 |
commit | 218c7d5bd6a918bd177982f9728ab809e1a3345b (patch) | |
tree | dbe0be3a6ddbce12b9129e8e6e87ba39c17ab932 /lib | |
parent | 700eb32f5a8852a8004782b199e5fcca4c847116 (diff) |
be even more safe in db
Diffstat (limited to 'lib')
-rw-r--r-- | lib/wallet/query.ts | 14 | ||||
-rw-r--r-- | lib/wallet/wallet.ts | 15 |
2 files changed, 18 insertions, 11 deletions
diff --git a/lib/wallet/query.ts b/lib/wallet/query.ts index 03b443a6e..ddd22b4cf 100644 --- a/lib/wallet/query.ts +++ b/lib/wallet/query.ts @@ -34,7 +34,7 @@ export class Store<T> { } } -export class Index<S,T> { +export class Index<S extends IDBValidKey,T> { indexName: string; storeName: string; @@ -49,7 +49,7 @@ export class Index<S,T> { * with indices. */ export interface QueryStream<T> { - indexJoin<S,I>(index: Index<I,S>, + indexJoin<S,I extends IDBValidKey>(index: Index<I,S>, keyFn: (obj: T) => I): QueryStream<[T, S]>; filter(f: (x: any) => boolean): QueryStream<T>; reduce<S>(f: (v: T, acc: S) => S, start?: S): Promise<S>; @@ -92,7 +92,7 @@ abstract class QueryStreamBase<T> implements QueryStream<T> { return new QueryStreamFlatMap(this, f); } - indexJoin<S,I>(index: Index<I,S>, + indexJoin<S,I extends IDBValidKey>(index: Index<I,S>, keyFn: (obj: T) => I): QueryStream<[T, S]> { this.root.addStoreAccess(index.storeName, false); return new QueryStreamIndexJoin(this, index.storeName, index.indexName, keyFn); @@ -301,7 +301,7 @@ export class QueryRoot { return new IterQueryStream(this, store.name, {}); } - iterIndex<S,T>(index: Index<S,T>, only?: S): QueryStream<T> { + iterIndex<S extends IDBValidKey,T>(index: Index<S,T>, only?: S): QueryStream<T> { this.stores.add(index.storeName); return new IterQueryStream(this, index.storeName, { only, @@ -377,7 +377,7 @@ export class QueryRoot { /** * Get one object from a store by its key. */ - getIndexed(storeName: string, indexName: string, key: any): Promise<any> { + getIndexed<I extends IDBValidKey,T>(index: Index<I,T>, key: I): Promise<T|undefined> { if (key === void 0) { throw Error("key must not be undefined"); } @@ -385,13 +385,13 @@ export class QueryRoot { const {resolve, promise} = openPromise(); const doGetIndexed = (tx: IDBTransaction) => { - const req = tx.objectStore(storeName).index(indexName).get(key); + const req = tx.objectStore(index.storeName).index(index.indexName).get(key); req.onsuccess = () => { resolve(req.result); }; }; - this.addWork(doGetIndexed, storeName, false); + this.addWork(doGetIndexed, index.storeName, false); return Promise.resolve() .then(() => this.finish()) .then(() => promise); diff --git a/lib/wallet/wallet.ts b/lib/wallet/wallet.ts index 54c979919..f55ba2170 100644 --- a/lib/wallet/wallet.ts +++ b/lib/wallet/wallet.ts @@ -329,9 +329,17 @@ namespace Stores { timestampIndex = new Index<number,HistoryRecord>(this, "timestamp"); } + class TransactionsStore extends Store<Transaction> { + constructor() { + super("transactions"); + } + + repurchaseIndex = new Index<[string,string],Transaction>(this, "repurchase"); + } + export let exchanges: ExchangeStore = new ExchangeStore(); - export let transactions: Store<Transaction> = new Store<Transaction>("transactions"); + export let transactions: TransactionsStore = new TransactionsStore(); export let reserves: Store<ReserveRecord> = new Store<ReserveRecord>("reserves"); export let coins: CoinsStore = new CoinsStore(); export let refresh: Store<RefreshSession> = new Store<RefreshSession>("refresh"); @@ -1444,10 +1452,9 @@ export class Wallet { console.log("no repurchase: no correlation id"); return {isRepurchase: false}; } - let result: Transaction = await ( + let result: Transaction|undefined = await ( this.q() - .getIndexed("transactions", - "repurchase", + .getIndexed(Stores.transactions.repurchaseIndex, [ contract.merchant_pub, contract.repurchase_correlation_id |