mongoose 8.18.2 → 8.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.umd.js +1 -1
- package/lib/connection.js +1 -1
- package/lib/document.js +6 -2
- package/lib/helpers/query/castUpdate.js +3 -3
- package/lib/helpers/schema/getPath.js +4 -1
- package/lib/model.js +16 -5
- package/lib/mongoose.js +2 -2
- package/lib/schema/documentArray.js +1 -1
- package/lib/schema/map.js +2 -2
- package/lib/schema/subdocument.js +1 -1
- package/lib/schemaType.js +1 -0
- package/package.json +5 -3
- package/types/index.d.ts +17 -8
- package/types/inferrawdoctype.d.ts +76 -95
- package/types/inferschematype.d.ts +214 -213
- package/types/models.d.ts +194 -26
- package/types/query.d.ts +79 -32
- package/types/schemaoptions.d.ts +3 -0
- package/types/schematypes.d.ts +86 -33
- package/types/utility.d.ts +108 -81
package/types/schematypes.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as BSON from 'bson';
|
|
2
2
|
|
|
3
3
|
declare module 'mongoose' {
|
|
4
|
-
|
|
5
4
|
/** The Mongoose Date [SchemaType](/docs/schematypes.html). */
|
|
6
5
|
type Date = Schema.Types.Date;
|
|
7
6
|
|
|
@@ -14,7 +13,6 @@ declare module 'mongoose' {
|
|
|
14
13
|
*/
|
|
15
14
|
type Decimal128 = Schema.Types.Decimal128;
|
|
16
15
|
|
|
17
|
-
|
|
18
16
|
/**
|
|
19
17
|
* The Mongoose Int32 [SchemaType](/docs/schematypes.html). Used for
|
|
20
18
|
* declaring paths in your schema that should be
|
|
@@ -37,7 +35,6 @@ declare module 'mongoose' {
|
|
|
37
35
|
*/
|
|
38
36
|
type Number = Schema.Types.Number;
|
|
39
37
|
|
|
40
|
-
|
|
41
38
|
/**
|
|
42
39
|
* The Mongoose Double [SchemaType](/docs/schematypes.html). Used for
|
|
43
40
|
* declaring paths in your schema that Mongoose should cast to doubles (IEEE 754-2008)/
|
|
@@ -59,41 +56,55 @@ declare module 'mongoose' {
|
|
|
59
56
|
type DefaultType<T> = T extends Schema.Types.Mixed ? any : Partial<ExtractMongooseArray<T>>;
|
|
60
57
|
|
|
61
58
|
class SchemaTypeOptions<T, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> {
|
|
62
|
-
type?:
|
|
63
|
-
T extends
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
59
|
+
type?: T extends string ? StringSchemaDefinition
|
|
60
|
+
: T extends number ? NumberSchemaDefinition
|
|
61
|
+
: T extends boolean ? BooleanSchemaDefinition
|
|
62
|
+
: T extends NativeDate ? DateSchemaDefinition
|
|
63
|
+
: T extends Map<any, any> ? SchemaDefinition<typeof Map>
|
|
64
|
+
: T extends Buffer ? SchemaDefinition<typeof Buffer>
|
|
65
|
+
: T extends Types.ObjectId ? ObjectIdSchemaDefinition
|
|
66
|
+
: T extends Types.ObjectId[] ?
|
|
67
|
+
AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId, EnforcedDocType, THydratedDocumentType>>
|
|
68
|
+
: T extends object[] ?
|
|
69
|
+
| AnyArray<Schema<any, any, any>>
|
|
70
|
+
| AnyArray<SchemaDefinition<Unpacked<T>>>
|
|
71
|
+
| AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType, THydratedDocumentType>>
|
|
72
|
+
: T extends string[] ?
|
|
73
|
+
AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string, EnforcedDocType, THydratedDocumentType>>
|
|
74
|
+
: T extends number[] ?
|
|
75
|
+
AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number, EnforcedDocType, THydratedDocumentType>>
|
|
76
|
+
: T extends boolean[] ?
|
|
77
|
+
AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean, EnforcedDocType, THydratedDocumentType>>
|
|
78
|
+
: T extends Function[] ?
|
|
79
|
+
AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType, THydratedDocumentType>>
|
|
80
|
+
: T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
|
|
77
81
|
|
|
78
82
|
/** Defines a virtual with the given name that gets/sets this path. */
|
|
79
83
|
alias?: string | string[];
|
|
80
84
|
|
|
81
85
|
/** Function or object describing how to validate this schematype. See [validation docs](https://mongoosejs.com/docs/validation.html). */
|
|
82
|
-
validate?:
|
|
86
|
+
validate?:
|
|
87
|
+
| SchemaValidator<T, EnforcedDocType, THydratedDocumentType>
|
|
88
|
+
| AnyArray<SchemaValidator<T, EnforcedDocType, THydratedDocumentType>>;
|
|
83
89
|
|
|
84
90
|
/** Allows overriding casting logic for this individual path. If a string, the given string overwrites Mongoose's default cast error message. */
|
|
85
|
-
cast?:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
cast?:
|
|
92
|
+
| string
|
|
93
|
+
| boolean
|
|
94
|
+
| ((value: any) => T)
|
|
95
|
+
| [(value: any) => T, string]
|
|
96
|
+
| [((value: any) => T) | null, (value: any, path: string, model: Model<any>, kind: string) => string];
|
|
90
97
|
|
|
91
98
|
/**
|
|
92
99
|
* If true, attach a required validator to this path, which ensures this path
|
|
93
100
|
* path cannot be set to a nullish value. If a function, Mongoose calls the
|
|
94
101
|
* function and only checks for nullish values if the function returns a truthy value.
|
|
95
102
|
*/
|
|
96
|
-
required?:
|
|
103
|
+
required?:
|
|
104
|
+
| boolean
|
|
105
|
+
| ((this: EnforcedDocType) => boolean)
|
|
106
|
+
| [boolean, string]
|
|
107
|
+
| [(this: EnforcedDocType) => boolean, string];
|
|
97
108
|
|
|
98
109
|
/**
|
|
99
110
|
* The default value for this path. If a function, Mongoose executes the function
|
|
@@ -164,16 +175,32 @@ declare module 'mongoose' {
|
|
|
164
175
|
set?: (value: any, priorVal?: T, doc?: this) => any;
|
|
165
176
|
|
|
166
177
|
/** array of allowed values for this path. Allowed for strings, numbers, and arrays of strings */
|
|
167
|
-
enum?:
|
|
178
|
+
enum?:
|
|
179
|
+
| Array<string | number | null>
|
|
180
|
+
| ReadonlyArray<string | number | null>
|
|
181
|
+
| { values: Array<string | number | null> | ReadonlyArray<string | number | null>; message?: string }
|
|
182
|
+
| { [path: string]: string | number | null };
|
|
168
183
|
|
|
169
184
|
/** The default [subtype](http://bsonspec.org/spec.html) associated with this buffer when it is stored in MongoDB. Only allowed for buffer paths */
|
|
170
185
|
subtype?: number;
|
|
171
186
|
|
|
172
187
|
/** The minimum value allowed for this path. Only allowed for numbers and dates. */
|
|
173
|
-
min?:
|
|
188
|
+
min?:
|
|
189
|
+
| number
|
|
190
|
+
| NativeDate
|
|
191
|
+
| [number, string]
|
|
192
|
+
| [NativeDate, string]
|
|
193
|
+
| readonly [number, string]
|
|
194
|
+
| readonly [NativeDate, string];
|
|
174
195
|
|
|
175
196
|
/** The maximum value allowed for this path. Only allowed for numbers and dates. */
|
|
176
|
-
max?:
|
|
197
|
+
max?:
|
|
198
|
+
| number
|
|
199
|
+
| NativeDate
|
|
200
|
+
| [number, string]
|
|
201
|
+
| [NativeDate, string]
|
|
202
|
+
| readonly [number, string]
|
|
203
|
+
| readonly [NativeDate, string];
|
|
177
204
|
|
|
178
205
|
/** Set to false to disable minimizing empty single nested subdocuments by default */
|
|
179
206
|
minimize?: boolean;
|
|
@@ -236,7 +263,7 @@ declare module 'mongoose' {
|
|
|
236
263
|
|
|
237
264
|
/**
|
|
238
265
|
* Specifies the type of queries that the field can be queried on the encrypted field.
|
|
239
|
-
|
|
266
|
+
*/
|
|
240
267
|
queries?: 'equality' | 'range';
|
|
241
268
|
}
|
|
242
269
|
|
|
@@ -268,7 +295,12 @@ declare module 'mongoose' {
|
|
|
268
295
|
static get(getter: (value: any) => any): void;
|
|
269
296
|
|
|
270
297
|
/** Array containing default setters for all instances of this SchemaType */
|
|
271
|
-
static setters: ((
|
|
298
|
+
static setters: ((
|
|
299
|
+
val?: unknown,
|
|
300
|
+
priorVal?: unknown,
|
|
301
|
+
doc?: Document<unknown>,
|
|
302
|
+
options?: Record<string, any> | null
|
|
303
|
+
) => unknown)[];
|
|
272
304
|
|
|
273
305
|
/** Contains the handlers for different query operators for this schema type. */
|
|
274
306
|
$conditionalHandlers: { [op: string]: (val: any, context: any) => any };
|
|
@@ -363,8 +395,9 @@ declare module 'mongoose' {
|
|
|
363
395
|
class Array extends SchemaType implements AcceptsDiscriminator {
|
|
364
396
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
365
397
|
static schemaName: 'Array';
|
|
398
|
+
readonly schemaName: 'Array';
|
|
366
399
|
|
|
367
|
-
static options: { castNonArrays: boolean
|
|
400
|
+
static options: { castNonArrays: boolean };
|
|
368
401
|
|
|
369
402
|
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
|
|
370
403
|
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
|
|
@@ -385,6 +418,7 @@ declare module 'mongoose' {
|
|
|
385
418
|
class BigInt extends SchemaType {
|
|
386
419
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
387
420
|
static schemaName: 'BigInt';
|
|
421
|
+
readonly schemaName: 'BigInt';
|
|
388
422
|
|
|
389
423
|
/** Default options for this SchemaType */
|
|
390
424
|
static defaultOptions: Record<string, any>;
|
|
@@ -393,6 +427,7 @@ declare module 'mongoose' {
|
|
|
393
427
|
class Boolean extends SchemaType {
|
|
394
428
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
395
429
|
static schemaName: 'Boolean';
|
|
430
|
+
readonly schemaName: 'Boolean';
|
|
396
431
|
|
|
397
432
|
/** Configure which values get casted to `true`. */
|
|
398
433
|
static convertToTrue: Set<any>;
|
|
@@ -407,6 +442,7 @@ declare module 'mongoose' {
|
|
|
407
442
|
class Buffer extends SchemaType {
|
|
408
443
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
409
444
|
static schemaName: 'Buffer';
|
|
445
|
+
readonly schemaName: 'Buffer';
|
|
410
446
|
|
|
411
447
|
/**
|
|
412
448
|
* Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
|
|
@@ -421,6 +457,7 @@ declare module 'mongoose' {
|
|
|
421
457
|
class Date extends SchemaType {
|
|
422
458
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
423
459
|
static schemaName: 'Date';
|
|
460
|
+
readonly schemaName: 'Date';
|
|
424
461
|
|
|
425
462
|
/** Declares a TTL index (rounded to the nearest second) for _Date_ types only. */
|
|
426
463
|
expires(when: number | string): this;
|
|
@@ -438,6 +475,7 @@ declare module 'mongoose' {
|
|
|
438
475
|
class Decimal128 extends SchemaType {
|
|
439
476
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
440
477
|
static schemaName: 'Decimal128';
|
|
478
|
+
readonly schemaName: 'Decimal128';
|
|
441
479
|
|
|
442
480
|
/** Default options for this SchemaType */
|
|
443
481
|
static defaultOptions: Record<string, any>;
|
|
@@ -446,6 +484,7 @@ declare module 'mongoose' {
|
|
|
446
484
|
class Int32 extends SchemaType {
|
|
447
485
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
448
486
|
static schemaName: 'Int32';
|
|
487
|
+
readonly schemaName: 'Int32';
|
|
449
488
|
|
|
450
489
|
/** Default options for this SchemaType */
|
|
451
490
|
static defaultOptions: Record<string, any>;
|
|
@@ -454,8 +493,9 @@ declare module 'mongoose' {
|
|
|
454
493
|
class DocumentArray extends SchemaType implements AcceptsDiscriminator {
|
|
455
494
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
456
495
|
static schemaName: 'DocumentArray';
|
|
496
|
+
readonly schemaName: 'DocumentArray';
|
|
457
497
|
|
|
458
|
-
static options: { castNonArrays: boolean
|
|
498
|
+
static options: { castNonArrays: boolean };
|
|
459
499
|
|
|
460
500
|
schemaOptions?: SchemaOptions;
|
|
461
501
|
|
|
@@ -475,6 +515,7 @@ declare module 'mongoose' {
|
|
|
475
515
|
class Map extends SchemaType {
|
|
476
516
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
477
517
|
static schemaName: 'Map';
|
|
518
|
+
readonly schemaName: 'Map';
|
|
478
519
|
|
|
479
520
|
/** Default options for this SchemaType */
|
|
480
521
|
static defaultOptions: Record<string, any>;
|
|
@@ -483,6 +524,7 @@ declare module 'mongoose' {
|
|
|
483
524
|
class Mixed extends SchemaType {
|
|
484
525
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
485
526
|
static schemaName: 'Mixed';
|
|
527
|
+
readonly schemaName: 'Mixed';
|
|
486
528
|
|
|
487
529
|
/** Default options for this SchemaType */
|
|
488
530
|
static defaultOptions: Record<string, any>;
|
|
@@ -491,6 +533,7 @@ declare module 'mongoose' {
|
|
|
491
533
|
class Number extends SchemaType {
|
|
492
534
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
493
535
|
static schemaName: 'Number';
|
|
536
|
+
readonly schemaName: 'Number';
|
|
494
537
|
|
|
495
538
|
/** Sets a enum validator */
|
|
496
539
|
enum(vals: number[]): this;
|
|
@@ -508,6 +551,7 @@ declare module 'mongoose' {
|
|
|
508
551
|
class Double extends SchemaType {
|
|
509
552
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
510
553
|
static schemaName: 'Double';
|
|
554
|
+
readonly schemaName: 'Double';
|
|
511
555
|
|
|
512
556
|
/** Default options for this SchemaType */
|
|
513
557
|
static defaultOptions: Record<string, any>;
|
|
@@ -516,6 +560,7 @@ declare module 'mongoose' {
|
|
|
516
560
|
class ObjectId extends SchemaType {
|
|
517
561
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
518
562
|
static schemaName: 'ObjectId';
|
|
563
|
+
readonly schemaName: 'ObjectId';
|
|
519
564
|
|
|
520
565
|
/** Adds an auto-generated ObjectId default if turnOn is true. */
|
|
521
566
|
auto(turnOn: boolean): this;
|
|
@@ -537,12 +582,19 @@ declare module 'mongoose' {
|
|
|
537
582
|
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
|
|
538
583
|
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
|
|
539
584
|
|
|
540
|
-
cast(
|
|
585
|
+
cast(
|
|
586
|
+
val: any,
|
|
587
|
+
doc?: Document<any>,
|
|
588
|
+
init?: boolean,
|
|
589
|
+
prev?: any,
|
|
590
|
+
options?: any
|
|
591
|
+
): HydratedSingleSubdocument<DocType>;
|
|
541
592
|
}
|
|
542
593
|
|
|
543
594
|
class String extends SchemaType {
|
|
544
595
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
545
596
|
static schemaName: 'String';
|
|
597
|
+
readonly schemaName: 'String';
|
|
546
598
|
|
|
547
599
|
/** Adds an enum validator */
|
|
548
600
|
enum(vals: string[] | any): this;
|
|
@@ -579,6 +631,7 @@ declare module 'mongoose' {
|
|
|
579
631
|
class UUID extends SchemaType {
|
|
580
632
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
581
633
|
static schemaName: 'UUID';
|
|
634
|
+
readonly schemaName: 'UUID';
|
|
582
635
|
|
|
583
636
|
/** Default options for this SchemaType */
|
|
584
637
|
static defaultOptions: Record<string, any>;
|
package/types/utility.d.ts
CHANGED
|
@@ -1,71 +1,87 @@
|
|
|
1
1
|
declare module 'mongoose' {
|
|
2
|
-
type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends
|
|
2
|
+
type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends 1 & IFTYPE
|
|
3
|
+
? THENTYPE
|
|
4
|
+
: ELSETYPE;
|
|
3
5
|
type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
|
|
4
6
|
|
|
5
7
|
type WithLevel1NestedPaths<T, K extends keyof T = keyof T> = {
|
|
6
8
|
[P in K | NestedPaths<Required<T>, K>]: P extends K
|
|
7
|
-
// Handle top-level paths
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// If not a document, then return the type. Otherwise, get the DocType.
|
|
11
|
-
|
|
12
|
-
: Extract<NonNullable<T[P]>, Document> extends Document<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
9
|
+
? // Handle top-level paths
|
|
10
|
+
// First, drill into documents so we don't end up surfacing `$assertPopulated`, etc.
|
|
11
|
+
Extract<NonNullable<T[P]>, Document> extends never
|
|
12
|
+
? // If not a document, then return the type. Otherwise, get the DocType.
|
|
13
|
+
NonNullable<T[P]>
|
|
14
|
+
: Extract<NonNullable<T[P]>, Document> extends Document<
|
|
15
|
+
any,
|
|
16
|
+
any,
|
|
17
|
+
infer DocType,
|
|
18
|
+
any
|
|
19
|
+
>
|
|
20
|
+
? DocType
|
|
21
|
+
: never
|
|
22
|
+
: // Handle nested paths
|
|
23
|
+
P extends `${infer Key}.${infer Rest}`
|
|
24
|
+
? Key extends keyof T
|
|
25
|
+
? T[Key] extends (infer U)[]
|
|
26
|
+
? Rest extends keyof NonNullable<U>
|
|
27
|
+
? NonNullable<U>[Rest]
|
|
28
|
+
: never
|
|
29
|
+
: Rest extends keyof NonNullable<T[Key]>
|
|
30
|
+
? NonNullable<T[Key]>[Rest]
|
|
25
31
|
: never
|
|
26
|
-
: never
|
|
32
|
+
: never
|
|
33
|
+
: never;
|
|
27
34
|
};
|
|
28
35
|
|
|
29
36
|
type NestedPaths<T, K extends keyof T> = K extends string
|
|
30
37
|
? T[K] extends TreatAsPrimitives
|
|
31
38
|
? never
|
|
32
39
|
: Extract<NonNullable<T[K]>, Document> extends never
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
? T[K] extends Array<infer U>
|
|
41
|
+
? U extends Record<string, any>
|
|
42
|
+
? `${K}.${keyof NonNullable<U> & string}`
|
|
43
|
+
: never
|
|
44
|
+
: T[K] extends Record<string, any> | null | undefined
|
|
45
|
+
? `${K}.${keyof NonNullable<T[K]> & string}`
|
|
46
|
+
: never
|
|
47
|
+
: Extract<NonNullable<T[K]>, Document> extends Document<
|
|
48
|
+
any,
|
|
49
|
+
any,
|
|
50
|
+
infer DocType,
|
|
51
|
+
any
|
|
52
|
+
>
|
|
53
|
+
? DocType extends Record<string, any>
|
|
54
|
+
? `${K}.${keyof NonNullable<DocType> & string}`
|
|
55
|
+
: never
|
|
56
|
+
: never
|
|
45
57
|
: never;
|
|
46
58
|
|
|
47
59
|
type WithoutUndefined<T> = T extends undefined ? never : T;
|
|
48
60
|
|
|
49
61
|
/**
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
62
|
+
* @summary Removes keys from a type
|
|
63
|
+
* @description It helps to exclude keys from a type
|
|
64
|
+
* @param {T} T A generic type to be checked.
|
|
65
|
+
* @param {K} K Keys from T that are to be excluded from the generic type
|
|
66
|
+
* @returns T with the keys in K excluded
|
|
67
|
+
*/
|
|
56
68
|
type ExcludeKeys<T, K extends keyof T> = {
|
|
57
69
|
[P in keyof T as P extends K ? never : P]: T[P];
|
|
58
70
|
};
|
|
59
71
|
|
|
60
|
-
type Unpacked<T> = T extends (infer U)[]
|
|
61
|
-
U
|
|
62
|
-
T extends ReadonlyArray<infer U>
|
|
72
|
+
type Unpacked<T> = T extends (infer U)[]
|
|
73
|
+
? U
|
|
74
|
+
: T extends ReadonlyArray<infer U>
|
|
75
|
+
? U
|
|
76
|
+
: T;
|
|
63
77
|
|
|
64
|
-
type UnpackedIntersection<T, U> = T extends null
|
|
78
|
+
type UnpackedIntersection<T, U> = T extends null
|
|
79
|
+
? null
|
|
80
|
+
: T extends (infer A)[]
|
|
65
81
|
? (Omit<A, keyof U> & U)[]
|
|
66
82
|
: keyof U extends never
|
|
67
|
-
|
|
68
|
-
|
|
83
|
+
? T
|
|
84
|
+
: Omit<T, keyof U> & U;
|
|
69
85
|
|
|
70
86
|
type MergeType<A, B> = Omit<A, keyof B> & B;
|
|
71
87
|
|
|
@@ -76,46 +92,54 @@ declare module 'mongoose' {
|
|
|
76
92
|
*/
|
|
77
93
|
type FlatRecord<T> = { [K in keyof T]: T[K] };
|
|
78
94
|
|
|
95
|
+
/** Force an operation like `{ a: 0 } & { b: 1 }` to be computed so that it displays `{ a: 0; b: 1 }`. */
|
|
96
|
+
export type Show<t> = { [k in keyof t]: t[k] } & unknown;
|
|
97
|
+
|
|
79
98
|
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
type IsItRecordAndNotAny<T> = IfEquals<
|
|
99
|
+
* @summary Checks if a type is "Record" or "any".
|
|
100
|
+
* @description It Helps to check if user has provided schema type "EnforcedDocType"
|
|
101
|
+
* @param {T} T A generic type to be checked.
|
|
102
|
+
* @returns true if {@link T} is Record OR false if {@link T} is of any type.
|
|
103
|
+
*/
|
|
104
|
+
type IsItRecordAndNotAny<T> = IfEquals<
|
|
105
|
+
T,
|
|
106
|
+
any,
|
|
107
|
+
false,
|
|
108
|
+
T extends Record<any, any> ? true : false
|
|
109
|
+
>;
|
|
86
110
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
type IfEquals<T, U, Y = true, N = false> =
|
|
95
|
-
|
|
96
|
-
|
|
111
|
+
/**
|
|
112
|
+
* @summary Checks if two types are identical.
|
|
113
|
+
* @param {T} T The first type to be compared with {@link U}.
|
|
114
|
+
* @param {U} U The seconde type to be compared with {@link T}.
|
|
115
|
+
* @param {Y} Y A type to be returned if {@link T} & {@link U} are identical.
|
|
116
|
+
* @param {N} N A type to be returned if {@link T} & {@link U} are not identical.
|
|
117
|
+
*/
|
|
118
|
+
type IfEquals<T, U, Y = true, N = false> = (<G>() => G extends T
|
|
119
|
+
? 1
|
|
120
|
+
: 0) extends <G>() => G extends U ? 1 : 0
|
|
121
|
+
? Y
|
|
122
|
+
: N;
|
|
97
123
|
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
type ThisParameter<T, F> = T extends { (this: infer This): void }
|
|
104
|
-
? This
|
|
105
|
-
: F;
|
|
124
|
+
/**
|
|
125
|
+
* @summary Extracts 'this' parameter from a function, if it exists. Otherwise, returns fallback.
|
|
126
|
+
* @param {T} T Function type to extract 'this' parameter from.
|
|
127
|
+
* @param {F} F Fallback type to return if 'this' parameter does not exist.
|
|
128
|
+
*/
|
|
129
|
+
type ThisParameter<T, F> = T extends { (this: infer This): void } ? This : F;
|
|
106
130
|
|
|
107
|
-
/**
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
type AddThisParameter<T, D> = {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
};
|
|
131
|
+
/**
|
|
132
|
+
* @summary Decorates all functions in an object with 'this' parameter.
|
|
133
|
+
* @param {T} T Object with functions as values to add 'D' parameter to as 'this'. {@link D}
|
|
134
|
+
* @param {D} D The type to be added as 'this' parameter to all functions in {@link T}.
|
|
135
|
+
*/
|
|
136
|
+
type AddThisParameter<T, D> = {
|
|
137
|
+
[K in keyof T]: T[K] extends (...args: infer A) => infer R
|
|
138
|
+
? ThisParameter<T[K], unknown> extends unknown
|
|
139
|
+
? (this: D, ...args: A) => R
|
|
140
|
+
: T[K]
|
|
141
|
+
: T[K];
|
|
142
|
+
};
|
|
119
143
|
|
|
120
144
|
/**
|
|
121
145
|
* @summary Adds timestamp fields to a type
|
|
@@ -124,5 +148,8 @@ type AddThisParameter<T, D> = {
|
|
|
124
148
|
* @param {P} P Optional SchemaTimestampsConfig or boolean to customize timestamp field names
|
|
125
149
|
* @returns T with timestamp fields added
|
|
126
150
|
*/
|
|
127
|
-
export type WithTimestamps<
|
|
151
|
+
export type WithTimestamps<
|
|
152
|
+
T,
|
|
153
|
+
P extends SchemaTimestampsConfig | boolean = true
|
|
154
|
+
> = ResolveTimestamps<T, { timestamps: P }>;
|
|
128
155
|
}
|