-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
deep.ts
27 lines (25 loc) · 953 Bytes
/
deep.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import type { MergeObjects, Query } from './query.js';
import type { ItemType, RelationalFields } from './schema.js';
import type { UnpackList } from './utils.js';
/**
* Deep filter object
*/
export type QueryDeep<Schema extends object, Item> = UnpackList<Item> extends infer FlatItem
? RelationalFields<Schema, FlatItem> extends never
? never
: {
[Field in RelationalFields<Schema, FlatItem> as ExtractCollection<Schema, FlatItem[Field]> extends any[]
? Field
: never]?: ExtractCollection<Schema, FlatItem[Field]> extends infer CollectionItem
? Query<Schema, CollectionItem> extends infer TQuery
? MergeObjects<
QueryDeep<Schema, CollectionItem>,
{
[Key in keyof Omit<TQuery, 'deep' | 'alias' | 'fields'> as `_${string & Key}`]: TQuery[Key];
}
>
: never
: never;
}
: never;
type ExtractCollection<Schema extends object, Item> = Extract<Item, ItemType<Schema>>;