1 | import {
|
2 | Schema,
|
3 | InferSchemaType,
|
4 | SchemaType,
|
5 | SchemaTypeOptions,
|
6 | TypeKeyBaseType,
|
7 | Types,
|
8 | NumberSchemaDefinition,
|
9 | StringSchemaDefinition,
|
10 | BooleanSchemaDefinition,
|
11 | DateSchemaDefinition,
|
12 | ObtainDocumentType,
|
13 | DefaultTypeKey,
|
14 | ObjectIdSchemaDefinition,
|
15 | IfEquals
|
16 | } from 'mongoose';
|
17 |
|
18 | declare module 'mongoose' {
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | type ObtainDocumentType<DocDefinition, EnforcedDocType = any, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
|
27 | IsItRecordAndNotAny<EnforcedDocType> extends true ? EnforcedDocType : {
|
28 | [K in keyof (RequiredPaths<DocDefinition, TypeKey> &
|
29 | OptionalPaths<DocDefinition, TypeKey>)]: ObtainDocumentPathType<DocDefinition[K], TypeKey>;
|
30 | };
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'>;
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | type ObtainSchemaGeneric<TSchema, alias extends 'EnforcedDocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'TVirtuals' | 'TStaticMethods' | 'TPathTypeKey' | 'DocType'> =
|
49 | TSchema extends Schema<infer EnforcedDocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer TVirtuals, infer TStaticMethods, infer TPathTypeKey, infer DocType>
|
50 | ? {
|
51 | EnforcedDocType: EnforcedDocType;
|
52 | M: M;
|
53 | TInstanceMethods: TInstanceMethods;
|
54 | TQueryHelpers: TQueryHelpers;
|
55 | TVirtuals: TVirtuals;
|
56 | TStaticMethods: TStaticMethods;
|
57 | TPathTypeKey: TPathTypeKey;
|
58 | DocType: DocType;
|
59 | }[alias]
|
60 | : unknown;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | type IsPathRequired<P, TypeKey extends TypeKeyBaseType> =
|
69 | P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
|
70 | ? true
|
71 | : P extends (Record<TypeKey, ArrayConstructor | any[]>)
|
72 | ? P extends { default: undefined }
|
73 | ? false
|
74 | : true
|
75 | : P extends (Record<TypeKey, any>)
|
76 | ? P extends { default: any }
|
77 | ? IfEquals<P['default'], undefined, false, true>
|
78 | : false
|
79 | : false;
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | type PathWithTypePropertyBaseType<TypeKey extends TypeKeyBaseType> = { [k in TypeKey]: any };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | type RequiredPathKeys<T, TypeKey extends TypeKeyBaseType> = {
|
95 | [K in keyof T]: IsPathRequired<T[K], TypeKey> extends true ? IfEquals<T[K], any, never, K> : never;
|
96 | }[keyof T];
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | type RequiredPaths<T, TypeKey extends TypeKeyBaseType> = {
|
105 | [K in RequiredPathKeys<T, TypeKey>]: T[K];
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | type OptionalPathKeys<T, TypeKey extends TypeKeyBaseType> = {
|
115 | [K in keyof T]: IsPathRequired<T[K], TypeKey> extends true ? never : K;
|
116 | }[keyof T];
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | type OptionalPaths<T, TypeKey extends TypeKeyBaseType> = {
|
125 | [K in OptionalPathKeys<T, TypeKey>]?: T[K];
|
126 | };
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | type ObtainDocumentPathType<PathValueType, TypeKey extends TypeKeyBaseType> = PathValueType extends Schema<any>
|
135 | ? InferSchemaType<PathValueType>
|
136 | : ResolvePathType<
|
137 | PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType,
|
138 | PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {},
|
139 | TypeKey
|
140 | >;
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (infer E)[] ? E : T extends { values: any } ? PathEnumOrString<T['values']> : string;
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
|
156 | PathValueType extends Schema ? InferSchemaType<PathValueType> :
|
157 | PathValueType extends (infer Item)[] ? IfEquals<Item, never, any[], Item extends Schema ? Types.DocumentArray<ResolvePathType<Item>> : ResolvePathType<Item>[]> :
|
158 | PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
|
159 | PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
|
160 | PathValueType extends DateSchemaDefinition ? Date :
|
161 | PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
|
162 | PathValueType extends BooleanSchemaDefinition ? boolean :
|
163 | PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
|
164 | PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
|
165 | PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
|
166 | PathValueType extends ArrayConstructor ? any[] :
|
167 | PathValueType extends typeof Schema.Types.Mixed ? any:
|
168 | IfEquals<PathValueType, ObjectConstructor> extends true ? any:
|
169 | IfEquals<PathValueType, {}> extends true ? any:
|
170 | PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
|
171 | PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, TypeKey> :
|
172 | unknown;
|