diff options
Diffstat (limited to 'packages/taler-wallet-core/src/util/query.ts')
-rw-r--r-- | packages/taler-wallet-core/src/util/query.ts | 109 |
1 files changed, 81 insertions, 28 deletions
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; }; |