diff --git a/src/public/types/dexie.d.ts b/src/public/types/dexie.d.ts index 67189f651..a3697949f 100644 --- a/src/public/types/dexie.d.ts +++ b/src/public/types/dexie.d.ts @@ -13,7 +13,7 @@ import { IndexableType } from './indexable-type'; import { DBCore } from './dbcore'; import { Middleware, DexieStacks } from './middleware'; -export type TableProp = { +export type TableProp = { [K in keyof DX]: DX[K] extends {schema: any, get: any, put: any, add: any, where: any} ? K : never; }[keyof DX] & string; diff --git a/src/public/types/strictly-typed-schema.d.ts b/src/public/types/strictly-typed-schema.d.ts index 5d2ae849f..03b666e80 100644 --- a/src/public/types/strictly-typed-schema.d.ts +++ b/src/public/types/strictly-typed-schema.d.ts @@ -19,6 +19,18 @@ type StringProps = { [K in keyof T]: K extends string ? (T[K] extends string ? K : never) : never; }[keyof T]; +type IgnoreObjects = + | RegExp + | DataView + | Map + | Set + | CryptoKey + | Promise + | ReadableStream + | ReadableStreamDefaultReader + | ReadableStreamDefaultController + | { whenLoaded: Promise }; // Y.Doc + // Nested indexable arrays (configurable max depth) type ArrayKeyPaths< T, @@ -54,17 +66,19 @@ type IndexableKeyPaths< CURRDEPTH extends string = '' > = CURRDEPTH extends MAXDEPTH ? IndexableProps - : - | IndexableKeyPaths - | { - [K in keyof T]: K extends string - ? T[K] extends object - ? `${K}.${keyof T[K] extends string - ? IndexableKeyPaths - : never}` - : never - : never; - }[keyof T]; + : { + [K in keyof T]: K extends string + ? T[K] extends IndexableType + ? K + : T[K] extends IgnoreObjects + ? never + : T[K] extends object + ? `${K}.${keyof T[K] extends string + ? IndexableKeyPaths + : never}` + : never + : never; + }[keyof T]; // Compound index syntax type Combo< @@ -104,73 +118,31 @@ type DexiePrimaryKeySyntax = | `++${NumberProps}` | `@${StringProps}` | `&${IndexableKeyPaths}` - | IndexableKeyPaths - | Exclude<`[${Combo>}]`, SingleCombound> // Combined non-array keys + | `${IndexableKeyPaths}` + | `${IndexableKeyPaths}:Y.Doc` + | `${Exclude<`[${Combo>}]`, SingleCombound>}` // Combined non-array keys | `&${Exclude<`[${Combo>}]`, SingleCombound>}`; +type TypedProperties = { + [K in keyof T]: K extends string + ? T[K] extends { whenLoaded: Promise } + ? `${K}:Y.Doc` + : never + : never; +}[keyof T]; + type LooseStoresSpec = { [tableName: string]: string | null | string[] }; -type StoresSpec = TableProp extends never - ? LooseStoresSpec - : { +type StoresSpec = TableProp> extends never ? LooseStoresSpec : + Dexie extends DX ? LooseStoresSpec : + { [TN in TableProp]?: DX[TN] extends Table - ? string | null | [DexiePrimaryKeySyntax, ...DexieIndexSyntax[]] - : never; - } & LooseStoresSpec; - -// ------- - -/* -type ClazzDecorator = (target: new () => T) => new () => T; -declare function index( - ...indexSpec: DexieIndexSyntax[] -): ClazzDecorator; - -@index( - "[name+id+tags]", - "[allanlarson+apansson+name]", - "ydoc.meta", - "fredriksod", - "&[name+bosse+allanlarson]", - "[allanlarson+apansson+blob.size]", -) - -// Test example with the Friend interface -interface Friend { - name: string; - age: number; - tags: string[]; -} - -// Example usage -const index1: DexieIndexSyntax = "name"; // valid -const index2: DexieIndexSyntax = "age"; // valid -const index3: DexieIndexSyntax = "*tags"; // valid (array field) -const index4: DexieIndexSyntax = "&[name+age]"; // valid (combined index) -const index5: DexieIndexSyntax = "[age+name]"; // valid (combined index) - -// Invalid cases -// const invalidIndex1: DexieIndexSyntax = '*name'; // error (* not allowed for non-array fields) -// const invalidIndex2: DexieIndexSyntax = '[name+*tags]'; // error (composite index cannot include *tags) -// const invalidIndex3: DexieIndexSyntax = '[*tags+age]'; // error (composite index cannot include *tags) - -// ------- - -function stores(storesSpec: StoresSpec): void { - console.log(storesSpec); -} - -const db = new Dexie("MyDatabase") as Dexie & { - friends: EntityTable; - todoItems: EntityTable; -}; - -stores({ - //friends: ["&age"], - todoItems: [ - "&[allanlarson+apansson+blob.size]", - "[allanlarson+apansson+bosse]", - "[allanlarson+bosse+name]", - ], -}); -*/ + ? + | string + | null + | [ + DexiePrimaryKeySyntax, + ...(DexieIndexSyntax | TypedProperties)[] + ] + : string | string[] | null; + };