mongoose 8.20.0 → 9.0.0-rc0
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/eslint.config.mjs +198 -0
- package/lib/aggregate.js +17 -73
- package/lib/cast/bigint.js +1 -1
- package/lib/cast/double.js +1 -1
- package/lib/cast/uuid.js +5 -48
- package/lib/cast.js +2 -2
- package/lib/connection.js +0 -1
- package/lib/cursor/aggregationCursor.js +14 -24
- package/lib/cursor/queryCursor.js +7 -14
- package/lib/document.js +125 -121
- package/lib/drivers/node-mongodb-native/connection.js +3 -10
- package/lib/error/objectParameter.js +1 -2
- package/lib/error/validation.js +0 -8
- package/lib/helpers/clone.js +1 -1
- package/lib/helpers/common.js +1 -1
- package/lib/helpers/indexes/isIndexEqual.js +0 -1
- package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
- package/lib/helpers/model/applyHooks.js +43 -53
- package/lib/helpers/model/applyMethods.js +2 -2
- package/lib/helpers/model/applyStaticHooks.js +1 -48
- package/lib/helpers/model/castBulkWrite.js +1 -1
- package/lib/helpers/parallelLimit.js +18 -36
- package/lib/helpers/pluralize.js +3 -3
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
- package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
- package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
- package/lib/helpers/populate/getSchemaTypes.js +5 -5
- package/lib/helpers/query/cast$expr.js +8 -10
- package/lib/helpers/query/castFilterPath.js +1 -1
- package/lib/helpers/query/castUpdate.js +14 -12
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
- package/lib/helpers/schema/applyPlugins.js +1 -1
- package/lib/helpers/schema/getIndexes.js +1 -7
- package/lib/helpers/timestamps/setupTimestamps.js +3 -6
- package/lib/helpers/updateValidators.js +57 -111
- package/lib/model.js +419 -607
- package/lib/mongoose.js +41 -13
- package/lib/plugins/saveSubdocs.js +24 -51
- package/lib/plugins/sharding.js +5 -4
- package/lib/plugins/validateBeforeSave.js +3 -13
- package/lib/query.js +101 -145
- package/lib/queryHelpers.js +2 -2
- package/lib/schema/array.js +41 -84
- package/lib/schema/documentArray.js +57 -94
- package/lib/schema/documentArrayElement.js +16 -11
- package/lib/schema/string.js +1 -1
- package/lib/schema/subdocument.js +22 -28
- package/lib/schema/uuid.js +0 -21
- package/lib/schema.js +81 -39
- package/lib/schemaType.js +39 -57
- package/lib/types/array/index.js +2 -2
- package/lib/types/array/methods/index.js +4 -4
- package/lib/types/arraySubdocument.js +1 -1
- package/lib/types/buffer.js +10 -10
- package/lib/types/decimal128.js +1 -1
- package/lib/types/documentArray/index.js +1 -1
- package/lib/types/documentArray/methods/index.js +5 -3
- package/lib/types/double.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +15 -43
- package/lib/types/uuid.js +1 -1
- package/lib/utils.js +1 -8
- package/lib/validOptions.js +3 -3
- package/package.json +11 -24
- package/types/connection.d.ts +20 -11
- package/types/document.d.ts +95 -26
- package/types/index.d.ts +143 -39
- package/types/inferhydrateddoctype.d.ts +115 -0
- package/types/inferrawdoctype.d.ts +99 -75
- package/types/inferschematype.d.ts +17 -3
- package/types/middlewares.d.ts +0 -2
- package/types/models.d.ts +131 -199
- package/types/mongooseoptions.d.ts +6 -5
- package/types/pipelinestage.d.ts +1 -1
- package/types/query.d.ts +71 -139
- package/types/schemaoptions.d.ts +1 -1
- package/types/schematypes.d.ts +14 -10
- package/types/types.d.ts +3 -4
- package/types/utility.d.ts +68 -48
- package/types/validation.d.ts +18 -14
- package/browser.js +0 -8
- package/dist/browser.umd.js +0 -2
- package/lib/browser.js +0 -141
- package/lib/browserDocument.js +0 -101
- package/lib/documentProvider.js +0 -30
- package/lib/drivers/browser/binary.js +0 -14
- package/lib/drivers/browser/decimal128.js +0 -7
- package/lib/drivers/browser/index.js +0 -13
- package/lib/drivers/browser/objectid.js +0 -29
- package/lib/helpers/promiseOrCallback.js +0 -54
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
import {
|
|
2
2
|
IsSchemaTypeFromBuiltinClass,
|
|
3
3
|
PathEnumOrString,
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
OptionalPaths,
|
|
5
|
+
RequiredPaths,
|
|
6
|
+
IsPathRequired
|
|
6
7
|
} from './inferschematype';
|
|
8
|
+
import { Binary, UUID } from 'mongodb';
|
|
7
9
|
|
|
8
10
|
declare module 'mongoose' {
|
|
9
|
-
export type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
TSchemaOptions
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
export type InferRawDocTypeFromSchema<TSchema extends Schema<any>> = IsItRecordAndNotAny<ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>> extends true
|
|
12
|
+
? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
|
|
13
|
+
: FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;
|
|
14
|
+
|
|
15
|
+
export type InferRawDocType<
|
|
16
|
+
SchemaDefinition,
|
|
17
|
+
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
|
|
18
|
+
TTransformOptions = { bufferToBinary: false }
|
|
19
|
+
> = Require_id<ApplySchemaOptions<{
|
|
20
|
+
[
|
|
21
|
+
K in keyof (RequiredPaths<SchemaDefinition, TSchemaOptions['typeKey']> &
|
|
22
|
+
OptionalPaths<SchemaDefinition, TSchemaOptions['typeKey']>)
|
|
23
|
+
]: IsPathRequired<SchemaDefinition[K], TSchemaOptions['typeKey']> extends true
|
|
24
|
+
? ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions>
|
|
25
|
+
: ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions> | null;
|
|
26
|
+
}, TSchemaOptions>>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @summary Allows users to optionally choose their own type for a schema field for stronger typing.
|
|
30
|
+
* Make sure to check for `any` because `T extends { __rawDocTypeHint: infer U }` will infer `unknown` if T is `any`.
|
|
31
|
+
*/
|
|
32
|
+
type RawDocTypeHint<T> = IsAny<T> extends true ? never
|
|
33
|
+
: T extends { __rawDocTypeHint: infer U } ? U: never;
|
|
25
34
|
|
|
26
35
|
/**
|
|
27
36
|
* @summary Obtains schema Path type.
|
|
@@ -29,14 +38,26 @@ declare module 'mongoose' {
|
|
|
29
38
|
* @param {PathValueType} PathValueType Document definition path type.
|
|
30
39
|
* @param {TypeKey} TypeKey A generic refers to document definition.
|
|
31
40
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
type ObtainRawDocumentPathType<
|
|
42
|
+
PathValueType,
|
|
43
|
+
TypeKey extends string = DefaultTypeKey,
|
|
44
|
+
TTransformOptions = { bufferToBinary: false }
|
|
45
|
+
> = ResolveRawPathType<
|
|
46
|
+
TypeKey extends keyof PathValueType ?
|
|
47
|
+
TypeKey extends keyof PathValueType[TypeKey] ?
|
|
48
|
+
PathValueType
|
|
49
|
+
: PathValueType[TypeKey]
|
|
50
|
+
: PathValueType,
|
|
51
|
+
TypeKey extends keyof PathValueType ?
|
|
52
|
+
TypeKey extends keyof PathValueType[TypeKey] ?
|
|
53
|
+
{}
|
|
54
|
+
: Omit<PathValueType, TypeKey>
|
|
55
|
+
: {},
|
|
56
|
+
TypeKey,
|
|
57
|
+
TTransformOptions,
|
|
58
|
+
RawDocTypeHint<PathValueType>
|
|
59
|
+
>;
|
|
36
60
|
|
|
37
|
-
// can be efficiently checked like:
|
|
38
|
-
// `[T] extends [neverOrAny] ? T : ...`
|
|
39
|
-
// to avoid edge cases
|
|
40
61
|
type neverOrAny = ' ~neverOrAny~';
|
|
41
62
|
|
|
42
63
|
/**
|
|
@@ -52,54 +73,57 @@ declare module 'mongoose' {
|
|
|
52
73
|
* @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
|
|
53
74
|
* @returns Number, "Number" or "number" will be resolved to number type.
|
|
54
75
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
type ResolveRawPathType<
|
|
77
|
+
PathValueType,
|
|
78
|
+
Options extends SchemaTypeOptions<PathValueType> = {},
|
|
79
|
+
TypeKey extends string = DefaultSchemaOptions['typeKey'],
|
|
80
|
+
TTransformOptions = { bufferToBinary: false },
|
|
81
|
+
TypeHint = never
|
|
82
|
+
> =
|
|
83
|
+
IsNotNever<TypeHint> extends true ? TypeHint
|
|
84
|
+
: [PathValueType] extends [neverOrAny] ? PathValueType
|
|
85
|
+
: PathValueType extends Schema<infer RawDocType, any, any, any, any, any, any, any, any, infer TSchemaDefinition> ? IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : InferRawDocType<TSchemaDefinition, DefaultSchemaOptions, TTransformOptions>
|
|
86
|
+
: PathValueType extends ReadonlyArray<infer Item> ?
|
|
87
|
+
IfEquals<Item, never> extends true ? any[]
|
|
88
|
+
: Item extends Schema<infer RawDocType, any, any, any, any, any, any, any, any, infer TSchemaDefinition> ?
|
|
89
|
+
// If Item is a schema, infer its type.
|
|
90
|
+
Array<IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : InferRawDocType<TSchemaDefinition, DefaultSchemaOptions, TTransformOptions>>
|
|
91
|
+
: TypeKey extends keyof Item ?
|
|
92
|
+
Item[TypeKey] extends Function | String ?
|
|
93
|
+
// If Item has a type key that's a string or a callable, it must be a scalar,
|
|
94
|
+
// so we can directly obtain its path type.
|
|
95
|
+
ObtainRawDocumentPathType<Item, TypeKey, TTransformOptions>[]
|
|
96
|
+
: // If the type key isn't callable, then this is an array of objects, in which case
|
|
97
|
+
// we need to call InferRawDocType to correctly infer its type.
|
|
98
|
+
Array<InferRawDocType<Item, DefaultSchemaOptions, TTransformOptions>>
|
|
99
|
+
: IsSchemaTypeFromBuiltinClass<Item> extends true ? ResolveRawPathType<Item, { enum: Options['enum'] }, TypeKey, TTransformOptions>[]
|
|
100
|
+
: IsItRecordAndNotAny<Item> extends true ?
|
|
101
|
+
Item extends Record<string, never> ?
|
|
102
|
+
ObtainRawDocumentPathType<Item, TypeKey, TTransformOptions>[]
|
|
103
|
+
: Array<InferRawDocType<Item, DefaultSchemaOptions, TTransformOptions>>
|
|
104
|
+
: ObtainRawDocumentPathType<Item, TypeKey, TTransformOptions>[]
|
|
105
|
+
: PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']>
|
|
106
|
+
: IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']>
|
|
107
|
+
: PathValueType extends NumberSchemaDefinition ?
|
|
108
|
+
Options['enum'] extends ReadonlyArray<any> ?
|
|
109
|
+
Options['enum'][number]
|
|
110
|
+
: number
|
|
111
|
+
: PathValueType extends DateSchemaDefinition ? NativeDate
|
|
112
|
+
: PathValueType extends BufferSchemaDefinition ? (TTransformOptions extends { bufferToBinary: true } ? Binary : Buffer)
|
|
113
|
+
: PathValueType extends BooleanSchemaDefinition ? boolean
|
|
114
|
+
: PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
|
|
115
|
+
: PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
|
|
116
|
+
: PathValueType extends BigintSchemaDefinition ? bigint
|
|
117
|
+
: PathValueType extends UuidSchemaDefinition ? Types.UUID
|
|
118
|
+
: PathValueType extends MapSchemaDefinition ? Record<string, ObtainRawDocumentPathType<Options['of'], TypeKey, TTransformOptions>>
|
|
119
|
+
: PathValueType extends DoubleSchemaDefinition ? Types.Double
|
|
120
|
+
: PathValueType extends UnionSchemaDefinition ?
|
|
121
|
+
ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
|
|
122
|
+
: PathValueType extends ArrayConstructor ? any[]
|
|
123
|
+
: PathValueType extends typeof Schema.Types.Mixed ? any
|
|
124
|
+
: IfEquals<PathValueType, ObjectConstructor> extends true ? any
|
|
125
|
+
: IfEquals<PathValueType, {}> extends true ? any
|
|
126
|
+
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
|
|
127
|
+
: PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>
|
|
128
|
+
: unknown;
|
|
105
129
|
}
|
|
@@ -59,6 +59,9 @@ declare module 'mongoose' {
|
|
|
59
59
|
*/
|
|
60
60
|
export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;
|
|
61
61
|
|
|
62
|
+
export type DefaultIdVirtual = { id: string };
|
|
63
|
+
export type AddDefaultId<DocType, TVirtuals, TSchemaOptions> = (DocType extends { id: any } ? TVirtuals : TSchemaOptions extends { id: false } ? TVirtuals : TVirtuals & { id: string });
|
|
64
|
+
|
|
62
65
|
/**
|
|
63
66
|
* @summary Obtains schema Generic type by using generic alias.
|
|
64
67
|
* @param {TSchema} TSchema A generic of schema type instance.
|
|
@@ -76,6 +79,8 @@ declare module 'mongoose' {
|
|
|
76
79
|
| 'TSchemaOptions'
|
|
77
80
|
| 'DocType'
|
|
78
81
|
| 'THydratedDocumentType'
|
|
82
|
+
| 'TSchemaDefinition'
|
|
83
|
+
| 'TLeanResultType'
|
|
79
84
|
> =
|
|
80
85
|
TSchema extends (
|
|
81
86
|
Schema<
|
|
@@ -87,7 +92,9 @@ declare module 'mongoose' {
|
|
|
87
92
|
infer TStaticMethods,
|
|
88
93
|
infer TSchemaOptions,
|
|
89
94
|
infer DocType,
|
|
90
|
-
infer THydratedDocumentType
|
|
95
|
+
infer THydratedDocumentType,
|
|
96
|
+
infer TSchemaDefinition,
|
|
97
|
+
infer TLeanResultType
|
|
91
98
|
>
|
|
92
99
|
) ?
|
|
93
100
|
{
|
|
@@ -95,11 +102,13 @@ declare module 'mongoose' {
|
|
|
95
102
|
M: M;
|
|
96
103
|
TInstanceMethods: TInstanceMethods;
|
|
97
104
|
TQueryHelpers: TQueryHelpers;
|
|
98
|
-
TVirtuals: TVirtuals
|
|
105
|
+
TVirtuals: AddDefaultId<DocType, TVirtuals, TSchemaOptions>;
|
|
99
106
|
TStaticMethods: TStaticMethods;
|
|
100
107
|
TSchemaOptions: TSchemaOptions;
|
|
101
108
|
DocType: DocType;
|
|
102
109
|
THydratedDocumentType: THydratedDocumentType;
|
|
110
|
+
TSchemaDefinition: TSchemaDefinition;
|
|
111
|
+
TLeanResultType: TLeanResultType;
|
|
103
112
|
}[alias]
|
|
104
113
|
: unknown;
|
|
105
114
|
|
|
@@ -236,6 +245,10 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> =
|
|
|
236
245
|
: T extends Record<string, infer V> ? V
|
|
237
246
|
: string;
|
|
238
247
|
|
|
248
|
+
type UnionToType<T extends readonly any[]> = T[number] extends infer U
|
|
249
|
+
? ResolvePathType<U>
|
|
250
|
+
: never;
|
|
251
|
+
|
|
239
252
|
type IsSchemaTypeFromBuiltinClass<T> =
|
|
240
253
|
T extends typeof String ? true
|
|
241
254
|
: T extends typeof Number ? true
|
|
@@ -254,6 +267,7 @@ type IsSchemaTypeFromBuiltinClass<T> =
|
|
|
254
267
|
: T extends Types.Decimal128 ? true
|
|
255
268
|
: T extends NativeDate ? true
|
|
256
269
|
: T extends typeof Schema.Types.Mixed ? true
|
|
270
|
+
: T extends Types.UUID ? true
|
|
257
271
|
: unknown extends Buffer ? false
|
|
258
272
|
: T extends Buffer ? true
|
|
259
273
|
: false;
|
|
@@ -301,12 +315,12 @@ type ResolvePathType<
|
|
|
301
315
|
Options['enum'][number]
|
|
302
316
|
: number
|
|
303
317
|
: PathValueType extends DateSchemaDefinition ? NativeDate
|
|
318
|
+
: PathValueType extends UuidSchemaDefinition ? Types.UUID
|
|
304
319
|
: PathValueType extends BufferSchemaDefinition ? Buffer
|
|
305
320
|
: PathValueType extends BooleanSchemaDefinition ? boolean
|
|
306
321
|
: PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
|
|
307
322
|
: PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
|
|
308
323
|
: PathValueType extends BigintSchemaDefinition ? bigint
|
|
309
|
-
: PathValueType extends UuidSchemaDefinition ? Buffer
|
|
310
324
|
: PathValueType extends DoubleSchemaDefinition ? Types.Double
|
|
311
325
|
: PathValueType extends MapSchemaDefinition ? Map<string, ObtainDocumentPathType<Options['of']>>
|
|
312
326
|
: PathValueType extends UnionSchemaDefinition ?
|
package/types/middlewares.d.ts
CHANGED
|
@@ -36,12 +36,10 @@ declare module 'mongoose' {
|
|
|
36
36
|
|
|
37
37
|
type PreMiddlewareFunction<ThisType = any> = (
|
|
38
38
|
this: ThisType,
|
|
39
|
-
next: CallbackWithoutResultAndOptionalError,
|
|
40
39
|
opts?: Record<string, any>
|
|
41
40
|
) => void | Promise<void> | Kareem.SkipWrappedFunction;
|
|
42
41
|
type PreSaveMiddlewareFunction<ThisType = any> = (
|
|
43
42
|
this: ThisType,
|
|
44
|
-
next: CallbackWithoutResultAndOptionalError,
|
|
45
43
|
opts: SaveOptions
|
|
46
44
|
) => void | Promise<void> | Kareem.SkipWrappedFunction;
|
|
47
45
|
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | Kareem.OverwriteMiddlewareResult;
|