diff options
author | Florian Dold <florian@dold.me> | 2023-09-07 20:35:46 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-09-08 00:12:52 +0200 |
commit | c660db82c12e08020661828f1d8383baa7ef0e02 (patch) | |
tree | 655b3aa9cf91fae4a4b4052356b97a9b3b119376 /packages/taler-wallet-core/src/util | |
parent | 33f2798004b1235eeb33a66d08bf22243f5ae566 (diff) |
wallet-core: address DB FIXMEs, systematic state numbering
Diffstat (limited to 'packages/taler-wallet-core/src/util')
-rw-r--r-- | packages/taler-wallet-core/src/util/coinSelection.ts | 2 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/util/query.ts | 109 |
2 files changed, 82 insertions, 29 deletions
diff --git a/packages/taler-wallet-core/src/util/coinSelection.ts b/packages/taler-wallet-core/src/util/coinSelection.ts index abf0bbc02..ce9fec186 100644 --- a/packages/taler-wallet-core/src/util/coinSelection.ts +++ b/packages/taler-wallet-core/src/util/coinSelection.ts @@ -1002,7 +1002,7 @@ export async function selectPeerCoins( x.coinAvailability, x.denominations, x.refreshGroups, - x.peerPushPaymentInitiations, + x.peerPushDebit, ]) .runReadWrite(async (tx) => { const exchanges = await tx.exchanges.iter().toArray(); diff --git a/packages/taler-wallet-core/src/util/query.ts b/packages/taler-wallet-core/src/util/query.ts index 1c3ff6a2a..309c17a43 100644 --- a/packages/taler-wallet-core/src/util/query.ts +++ b/packages/taler-wallet-core/src/util/query.ts @@ -35,7 +35,7 @@ import { IDBKeyPath, IDBKeyRange, } from "@gnu-taler/idb-bridge"; -import { Logger, j2s } from "@gnu-taler/taler-util"; +import { Codec, Logger, j2s } from "@gnu-taler/taler-util"; const logger = new Logger("query.ts"); @@ -387,11 +387,11 @@ export interface StoreReadWriteAccessor<RecordType, IndexMap> { export interface StoreWithIndexes< StoreName extends string, - SD extends StoreDescriptor<unknown>, + RecordType, IndexMap, > { storeName: StoreName; - store: SD; + store: StoreDescriptor<RecordType>; indexMap: IndexMap; /** @@ -401,19 +401,13 @@ export interface StoreWithIndexes< mark: Symbol; } -export type GetRecordType<T> = T extends StoreDescriptor<infer X> ? X : unknown; - const storeWithIndexesSymbol = Symbol("StoreWithIndexesMark"); -export function describeStore< - StoreName extends string, - SD extends StoreDescriptor<unknown>, - IndexMap, ->( +export function describeStore<StoreName extends string, RecordType, IndexMap>( name: StoreName, - s: SD, + s: StoreDescriptor<RecordType>, m: IndexMap, -): StoreWithIndexes<StoreName, SD, IndexMap> { +): StoreWithIndexes<StoreName, RecordType, IndexMap> { return { storeName: name, store: s, @@ -422,13 +416,72 @@ export function describeStore< }; } +export function describeStoreV2< + StoreName extends string, + RecordType, + IndexMap extends { [x: string]: IndexDescriptor } = {}, +>(args: { + storeName: StoreName; + recordCodec: Codec<RecordType>; + keyPath?: IDBKeyPath | IDBKeyPath[]; + autoIncrement?: boolean; + /** + * Database version that this store was added in, or + * undefined if added in the first version. + */ + versionAdded?: number; + indexes?: IndexMap; +}): StoreWithIndexes<StoreName, RecordType, IndexMap> { + return { + storeName: args.storeName, + store: { + _dummy: undefined as any, + autoIncrement: args.autoIncrement, + keyPath: args.keyPath, + versionAdded: args.versionAdded, + }, + indexMap: args.indexes ?? ({} as IndexMap), + mark: storeWithIndexesSymbol, + }; +} + +type KeyPathComponents = string | number; + +/** + * Follow a key path (dot-separated) in an object. + */ +type DerefKeyPath<T, P> = P extends `${infer PX extends keyof T & + KeyPathComponents}` + ? T[PX] + : P extends `${infer P0 extends keyof T & KeyPathComponents}.${infer Rest}` + ? DerefKeyPath<T[P0], Rest> + : unknown; + +/** + * Return a path if it is a valid dot-separate path to an object. + * Otherwise, return "never". + */ +type ValidateKeyPath<T, P> = P extends `${infer PX extends keyof T & + KeyPathComponents}` + ? PX + : P extends `${infer P0 extends keyof T & KeyPathComponents}.${infer Rest}` + ? `${P0}.${ValidateKeyPath<T[P0], Rest>}` + : never; + +// function foo<T, P>( +// x: T, +// p: P extends ValidateKeyPath<T, P> ? P : never, +// ): void {} + +// foo({x: [0,1,2]}, "x.0"); + export type GetReadOnlyAccess<BoundStores> = { [P in keyof BoundStores]: BoundStores[P] extends StoreWithIndexes< - infer SN, - infer SD, - infer IM + infer StoreName, + infer RecordType, + infer IndexMap > - ? StoreReadOnlyAccessor<GetRecordType<SD>, IM> + ? StoreReadOnlyAccessor<RecordType, IndexMap> : unknown; }; @@ -446,11 +499,11 @@ export type DbReadOnlyTransaction< } ? { [P in Stores]: StoreMap[P] extends StoreWithIndexes< - infer SN, - infer SD, - infer IM + infer StoreName, + infer RecordType, + infer IndexMap > - ? StoreReadOnlyAccessor<GetRecordType<SD>, IM> + ? StoreReadOnlyAccessor<RecordType, IndexMap> : unknown; } : unknown; @@ -463,22 +516,22 @@ export type DbReadWriteTransaction< } ? { [P in Stores]: StoreMap[P] extends StoreWithIndexes< - infer SN, - infer SD, - infer IM + infer StoreName, + infer RecordType, + infer IndexMap > - ? StoreReadWriteAccessor<GetRecordType<SD>, IM> + ? StoreReadWriteAccessor<RecordType, IndexMap> : unknown; } : unknown; export type GetReadWriteAccess<BoundStores> = { [P in keyof BoundStores]: BoundStores[P] extends StoreWithIndexes< - infer SN, - infer SD, - infer IM + infer StoreName, + infer RecordType, + infer IndexMap > - ? StoreReadWriteAccessor<GetRecordType<SD>, IM> + ? StoreReadWriteAccessor<RecordType, IndexMap> : unknown; }; |