mongoose 9.8.1 → 9.9.1
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/lib/connection.js +1 -9
- package/lib/document.js +223 -128
- package/lib/helpers/document/applyDefaults.js +71 -7
- package/lib/helpers/model/castBulkWrite.js +2 -2
- package/lib/helpers/parallelLimit.js +41 -16
- package/lib/helpers/schema/idGetter.js +3 -0
- package/lib/helpers/timestamps/setupTimestamps.js +1 -0
- package/lib/helpers/update/applyTimestampsToUpdate.js +4 -5
- package/lib/model.js +71 -52
- package/lib/queryHelpers.js +22 -0
- package/lib/schema.js +44 -2
- package/lib/schemaType.js +4 -3
- package/lib/stateMachine.js +22 -3
- package/package.json +1 -1
- package/types/inferhydrateddoctype.d.ts +19 -3
- package/types/inferrawdoctype.d.ts +19 -3
- package/types/models.d.ts +133 -4
- package/types/schemaoptions.d.ts +1 -0
- package/types/schematypes.d.ts +1 -1
package/lib/stateMachine.js
CHANGED
|
@@ -34,7 +34,6 @@ StateMachine.ctor = function() {
|
|
|
34
34
|
const states = [...arguments];
|
|
35
35
|
|
|
36
36
|
const ctor = function() {
|
|
37
|
-
StateMachine.apply(this, arguments);
|
|
38
37
|
this.paths = {};
|
|
39
38
|
this.states = {};
|
|
40
39
|
};
|
|
@@ -79,6 +78,28 @@ StateMachine.prototype._changeState = function _changeState(path, nextState) {
|
|
|
79
78
|
this.states[nextState][path] = true;
|
|
80
79
|
};
|
|
81
80
|
|
|
81
|
+
/*!
|
|
82
|
+
* ignore
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
StateMachine.prototype.clearAllExcept = function clearAllExcept(state) {
|
|
86
|
+
// State buckets are created lazily, so `states[state]` may not exist yet.
|
|
87
|
+
const bucket = this.states[state];
|
|
88
|
+
const keys = bucket == null ? [] : Object.keys(bucket);
|
|
89
|
+
if (keys.length === 0) {
|
|
90
|
+
this.paths = {};
|
|
91
|
+
this.states = {};
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.paths = {};
|
|
95
|
+
for (const path of keys) {
|
|
96
|
+
this.paths[path] = state;
|
|
97
|
+
}
|
|
98
|
+
this.states = {
|
|
99
|
+
[state]: this.states[state]
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
82
103
|
/*!
|
|
83
104
|
* ignore
|
|
84
105
|
*/
|
|
@@ -91,8 +112,6 @@ StateMachine.prototype.clear = function clear(state) {
|
|
|
91
112
|
if (keys.length === 0) {
|
|
92
113
|
return;
|
|
93
114
|
}
|
|
94
|
-
// Replace the bucket rather than deleting each key: repeated `delete` puts
|
|
95
|
-
// the object in dictionary mode, which is significantly slower.
|
|
96
115
|
this.states[state] = {};
|
|
97
116
|
let i = keys.length;
|
|
98
117
|
|
package/package.json
CHANGED
|
@@ -56,12 +56,28 @@ declare module 'mongoose' {
|
|
|
56
56
|
type HydratedDocTypeHint<T> = T extends { __hydratedDocTypeHint: infer U } ? U
|
|
57
57
|
: never;
|
|
58
58
|
|
|
59
|
+
type DiscriminatorHydratedKey<TBaseSchema extends Schema> =
|
|
60
|
+
ObtainSchemaGeneric<TBaseSchema, 'TSchemaOptions'> extends infer TSchemaOptions ?
|
|
61
|
+
'discriminatorKey' extends keyof TSchemaOptions ?
|
|
62
|
+
TSchemaOptions['discriminatorKey'] extends string ? TSchemaOptions['discriminatorKey'] : '__t'
|
|
63
|
+
: '__t'
|
|
64
|
+
: '__t';
|
|
65
|
+
|
|
66
|
+
type DiscriminatorHydratedBaseType<TBaseSchema extends Schema> =
|
|
67
|
+
InferHydratedDocTypeFromSchema<TBaseSchema> extends Record<DiscriminatorHydratedKey<TBaseSchema>, any> ?
|
|
68
|
+
InferHydratedDocTypeFromSchema<TBaseSchema>
|
|
69
|
+
: MergeType<InferHydratedDocTypeFromSchema<TBaseSchema>, { [K in DiscriminatorHydratedKey<TBaseSchema>]?: null }>;
|
|
70
|
+
|
|
59
71
|
type ResolveDiscriminatorHydratedPathType<TBaseSchema extends Schema, TDiscriminators> =
|
|
60
72
|
IsAny<TDiscriminators> extends true ? never
|
|
61
73
|
: TDiscriminators extends Record<string, any> ?
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
74
|
+
DiscriminatorHydratedBaseType<TBaseSchema> |
|
|
75
|
+
{
|
|
76
|
+
[K in keyof TDiscriminators]: TDiscriminators[K] extends Schema ?
|
|
77
|
+
MergeType<InferHydratedDocTypeFromSchema<TBaseSchema>, InferHydratedDocTypeFromSchema<TDiscriminators[K]>> &
|
|
78
|
+
{ [KDiscriminatorKey in DiscriminatorHydratedKey<TBaseSchema>]: K }
|
|
79
|
+
: never
|
|
80
|
+
}[keyof TDiscriminators]
|
|
65
81
|
: never;
|
|
66
82
|
|
|
67
83
|
type HydratedDiscriminatorEnumType<T> = string extends keyof T ? never
|
|
@@ -45,12 +45,28 @@ declare module 'mongoose' {
|
|
|
45
45
|
type RawDocTypeHint<T> = IsAny<T> extends true ? never
|
|
46
46
|
: T extends { __rawDocTypeHint: infer U } ? U: never;
|
|
47
47
|
|
|
48
|
+
type DiscriminatorRawKey<TBaseSchema extends Schema> =
|
|
49
|
+
ObtainSchemaGeneric<TBaseSchema, 'TSchemaOptions'> extends infer TSchemaOptions ?
|
|
50
|
+
'discriminatorKey' extends keyof TSchemaOptions ?
|
|
51
|
+
TSchemaOptions['discriminatorKey'] extends string ? TSchemaOptions['discriminatorKey'] : '__t'
|
|
52
|
+
: '__t'
|
|
53
|
+
: '__t';
|
|
54
|
+
|
|
55
|
+
type DiscriminatorRawBaseType<TBaseSchema extends Schema> =
|
|
56
|
+
InferRawDocTypeFromSchema<TBaseSchema> extends Record<DiscriminatorRawKey<TBaseSchema>, any> ?
|
|
57
|
+
InferRawDocTypeFromSchema<TBaseSchema>
|
|
58
|
+
: MergeType<InferRawDocTypeFromSchema<TBaseSchema>, { [K in DiscriminatorRawKey<TBaseSchema>]?: null }>;
|
|
59
|
+
|
|
48
60
|
type ResolveDiscriminatorRawPathType<TBaseSchema extends Schema, TDiscriminators> =
|
|
49
61
|
IsAny<TDiscriminators> extends true ? never
|
|
50
62
|
: TDiscriminators extends Record<string, any> ?
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
63
|
+
DiscriminatorRawBaseType<TBaseSchema> |
|
|
64
|
+
{
|
|
65
|
+
[K in keyof TDiscriminators]: TDiscriminators[K] extends Schema ?
|
|
66
|
+
MergeType<InferRawDocTypeFromSchema<TBaseSchema>, InferRawDocTypeFromSchema<TDiscriminators[K]>> &
|
|
67
|
+
{ [KDiscriminatorKey in DiscriminatorRawKey<TBaseSchema>]: K }
|
|
68
|
+
: never
|
|
69
|
+
}[keyof TDiscriminators]
|
|
54
70
|
: never;
|
|
55
71
|
|
|
56
72
|
type RawDiscriminatorEnumType<T> = string extends keyof T ? never
|
package/types/models.d.ts
CHANGED
|
@@ -457,6 +457,18 @@ declare module 'mongoose' {
|
|
|
457
457
|
'findOne',
|
|
458
458
|
TInstanceMethods & TVirtuals
|
|
459
459
|
>;
|
|
460
|
+
findById<ResultDoc = THydratedDocumentType>(
|
|
461
|
+
id: any,
|
|
462
|
+
projection: ProjectionType<TRawDocType> | null | undefined,
|
|
463
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
464
|
+
): QueryWithHelpers<
|
|
465
|
+
ResultDoc | null,
|
|
466
|
+
ResultDoc,
|
|
467
|
+
TQueryHelpers,
|
|
468
|
+
TLeanResultType,
|
|
469
|
+
'findOne',
|
|
470
|
+
TInstanceMethods & TVirtuals
|
|
471
|
+
>;
|
|
460
472
|
findById<ResultDoc = THydratedDocumentType>(
|
|
461
473
|
id?: any,
|
|
462
474
|
projection?: ProjectionType<TRawDocType> | null | undefined,
|
|
@@ -495,6 +507,18 @@ declare module 'mongoose' {
|
|
|
495
507
|
'findOne',
|
|
496
508
|
TInstanceMethods & TVirtuals
|
|
497
509
|
>;
|
|
510
|
+
findOne<ResultDoc = THydratedDocumentType>(
|
|
511
|
+
filter: QueryFilter<TRawDocType>,
|
|
512
|
+
projection: ProjectionType<TRawDocType> | null | undefined,
|
|
513
|
+
options: QueryOptions<TRawDocType> & { lean: false } & mongodb.Abortable
|
|
514
|
+
): QueryWithHelpers<
|
|
515
|
+
ResultDoc | null,
|
|
516
|
+
ResultDoc,
|
|
517
|
+
TQueryHelpers,
|
|
518
|
+
TLeanResultType,
|
|
519
|
+
'findOne',
|
|
520
|
+
TInstanceMethods & TVirtuals
|
|
521
|
+
>;
|
|
498
522
|
findOne<ResultDoc = THydratedDocumentType>(
|
|
499
523
|
filter?: QueryFilter<TRawDocType>,
|
|
500
524
|
projection?: ProjectionType<TRawDocType> | null | undefined,
|
|
@@ -780,12 +804,24 @@ declare module 'mongoose' {
|
|
|
780
804
|
'find',
|
|
781
805
|
TInstanceMethods & TVirtuals
|
|
782
806
|
>;
|
|
807
|
+
find<ResultDoc = THydratedDocumentType>(
|
|
808
|
+
filter: QueryFilter<TRawDocType>,
|
|
809
|
+
projection: ProjectionType<TRawDocType> | null | undefined,
|
|
810
|
+
options: QueryOptions<TRawDocType> & { lean: false } & mongodb.Abortable
|
|
811
|
+
): QueryWithHelpers<
|
|
812
|
+
ResultDoc[],
|
|
813
|
+
ResultDoc,
|
|
814
|
+
TQueryHelpers,
|
|
815
|
+
TLeanResultType,
|
|
816
|
+
'find',
|
|
817
|
+
TInstanceMethods & TVirtuals
|
|
818
|
+
>;
|
|
783
819
|
find<ResultDoc = THydratedDocumentType>(
|
|
784
820
|
filter?: QueryFilter<TRawDocType>,
|
|
785
821
|
projection?: ProjectionType<TRawDocType> | null | undefined,
|
|
786
822
|
options?: QueryOptions<TRawDocType> & mongodb.Abortable
|
|
787
823
|
): QueryWithHelpers<
|
|
788
|
-
ResultDoc[],
|
|
824
|
+
HasLeanOption<TSchema> extends true ? TLeanResultType[] : ResultDoc[],
|
|
789
825
|
ResultDoc,
|
|
790
826
|
TQueryHelpers,
|
|
791
827
|
TLeanResultType,
|
|
@@ -797,7 +833,7 @@ declare module 'mongoose' {
|
|
|
797
833
|
projection?: ProjectionType<TRawDocType> | null | undefined,
|
|
798
834
|
options?: QueryOptions<TRawDocType> & mongodb.Abortable
|
|
799
835
|
): QueryWithHelpers<
|
|
800
|
-
THydratedDocumentType[],
|
|
836
|
+
HasLeanOption<TSchema> extends true ? TLeanResultType[] : THydratedDocumentType[],
|
|
801
837
|
THydratedDocumentType,
|
|
802
838
|
TQueryHelpers,
|
|
803
839
|
TLeanResultType,
|
|
@@ -828,6 +864,17 @@ declare module 'mongoose' {
|
|
|
828
864
|
'findOneAndDelete',
|
|
829
865
|
TInstanceMethods & TVirtuals
|
|
830
866
|
>;
|
|
867
|
+
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
|
|
868
|
+
id: mongodb.ObjectId | any,
|
|
869
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
870
|
+
): QueryWithHelpers<
|
|
871
|
+
ResultDoc | null,
|
|
872
|
+
ResultDoc,
|
|
873
|
+
TQueryHelpers,
|
|
874
|
+
TLeanResultType,
|
|
875
|
+
'findOneAndDelete',
|
|
876
|
+
TInstanceMethods & TVirtuals
|
|
877
|
+
>;
|
|
831
878
|
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
|
|
832
879
|
id: mongodb.ObjectId | any,
|
|
833
880
|
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
|
|
@@ -889,6 +936,18 @@ declare module 'mongoose' {
|
|
|
889
936
|
'findOneAndUpdate',
|
|
890
937
|
TInstanceMethods & TVirtuals
|
|
891
938
|
>;
|
|
939
|
+
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
940
|
+
id: mongodb.ObjectId | any,
|
|
941
|
+
update: UpdateQuery<TRawDocType>,
|
|
942
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
943
|
+
): QueryWithHelpers<
|
|
944
|
+
ResultDoc | null,
|
|
945
|
+
ResultDoc,
|
|
946
|
+
TQueryHelpers,
|
|
947
|
+
TLeanResultType,
|
|
948
|
+
'findOneAndUpdate',
|
|
949
|
+
TInstanceMethods & TVirtuals
|
|
950
|
+
>;
|
|
892
951
|
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
893
952
|
id: mongodb.ObjectId | any,
|
|
894
953
|
update: UpdateQuery<TRawDocType>,
|
|
@@ -949,6 +1008,28 @@ declare module 'mongoose' {
|
|
|
949
1008
|
'findOneAndDelete',
|
|
950
1009
|
TInstanceMethods & TVirtuals
|
|
951
1010
|
>;
|
|
1011
|
+
findOneAndDelete<ResultDoc = THydratedDocumentType>(
|
|
1012
|
+
filter: QueryFilter<TRawDocType>,
|
|
1013
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
1014
|
+
): QueryWithHelpers<
|
|
1015
|
+
ResultDoc | null,
|
|
1016
|
+
ResultDoc,
|
|
1017
|
+
TQueryHelpers,
|
|
1018
|
+
TLeanResultType,
|
|
1019
|
+
'findOneAndDelete',
|
|
1020
|
+
TInstanceMethods & TVirtuals
|
|
1021
|
+
>;
|
|
1022
|
+
findOneAndDelete(
|
|
1023
|
+
filter: Query<any, any>,
|
|
1024
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
1025
|
+
): QueryWithHelpers<
|
|
1026
|
+
THydratedDocumentType | null,
|
|
1027
|
+
THydratedDocumentType,
|
|
1028
|
+
TQueryHelpers,
|
|
1029
|
+
TLeanResultType,
|
|
1030
|
+
'findOneAndDelete',
|
|
1031
|
+
TInstanceMethods & TVirtuals
|
|
1032
|
+
>;
|
|
952
1033
|
findOneAndDelete<ResultDoc = THydratedDocumentType>(
|
|
953
1034
|
filter: QueryFilter<TRawDocType>,
|
|
954
1035
|
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
|
|
@@ -975,7 +1056,7 @@ declare module 'mongoose' {
|
|
|
975
1056
|
filter?: QueryFilter<TRawDocType> | null,
|
|
976
1057
|
options?: QueryOptions<TRawDocType> | null
|
|
977
1058
|
): QueryWithHelpers<
|
|
978
|
-
HasLeanOption<TSchema> extends true ?
|
|
1059
|
+
HasLeanOption<TSchema> extends true ? TLeanResultType | null : ResultDoc | null,
|
|
979
1060
|
ResultDoc,
|
|
980
1061
|
TQueryHelpers,
|
|
981
1062
|
TLeanResultType,
|
|
@@ -986,7 +1067,7 @@ declare module 'mongoose' {
|
|
|
986
1067
|
filter?: Query<any, any> | null,
|
|
987
1068
|
options?: QueryOptions<TRawDocType> | null
|
|
988
1069
|
): QueryWithHelpers<
|
|
989
|
-
HasLeanOption<TSchema> extends true ?
|
|
1070
|
+
HasLeanOption<TSchema> extends true ? TLeanResultType | null : THydratedDocumentType | null,
|
|
990
1071
|
THydratedDocumentType,
|
|
991
1072
|
TQueryHelpers,
|
|
992
1073
|
TLeanResultType,
|
|
@@ -1019,6 +1100,30 @@ declare module 'mongoose' {
|
|
|
1019
1100
|
'findOneAndReplace',
|
|
1020
1101
|
TInstanceMethods & TVirtuals
|
|
1021
1102
|
>;
|
|
1103
|
+
findOneAndReplace<ResultDoc = THydratedDocumentType>(
|
|
1104
|
+
filter: QueryFilter<TRawDocType>,
|
|
1105
|
+
replacement: TRawDocType | AnyObject,
|
|
1106
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
1107
|
+
): QueryWithHelpers<
|
|
1108
|
+
ResultDoc | null,
|
|
1109
|
+
ResultDoc,
|
|
1110
|
+
TQueryHelpers,
|
|
1111
|
+
TLeanResultType,
|
|
1112
|
+
'findOneAndReplace',
|
|
1113
|
+
TInstanceMethods & TVirtuals
|
|
1114
|
+
>;
|
|
1115
|
+
findOneAndReplace(
|
|
1116
|
+
filter: Query<any, any>,
|
|
1117
|
+
replacement: TRawDocType | AnyObject,
|
|
1118
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
1119
|
+
): QueryWithHelpers<
|
|
1120
|
+
THydratedDocumentType | null,
|
|
1121
|
+
THydratedDocumentType,
|
|
1122
|
+
TQueryHelpers,
|
|
1123
|
+
TLeanResultType,
|
|
1124
|
+
'findOneAndReplace',
|
|
1125
|
+
TInstanceMethods & TVirtuals
|
|
1126
|
+
>;
|
|
1022
1127
|
findOneAndReplace<ResultDoc = THydratedDocumentType>(
|
|
1023
1128
|
filter: QueryFilter<TRawDocType>,
|
|
1024
1129
|
replacement: TRawDocType | AnyObject,
|
|
@@ -1141,6 +1246,30 @@ declare module 'mongoose' {
|
|
|
1141
1246
|
'findOneAndUpdate',
|
|
1142
1247
|
TInstanceMethods & TVirtuals
|
|
1143
1248
|
>;
|
|
1249
|
+
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
1250
|
+
filter: QueryFilter<TRawDocType>,
|
|
1251
|
+
update: UpdateQuery<TRawDocType>,
|
|
1252
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
1253
|
+
): QueryWithHelpers<
|
|
1254
|
+
ResultDoc | null,
|
|
1255
|
+
ResultDoc,
|
|
1256
|
+
TQueryHelpers,
|
|
1257
|
+
TLeanResultType,
|
|
1258
|
+
'findOneAndUpdate',
|
|
1259
|
+
TInstanceMethods & TVirtuals
|
|
1260
|
+
>;
|
|
1261
|
+
findOneAndUpdate(
|
|
1262
|
+
filter: Query<any, any>,
|
|
1263
|
+
update: UpdateQuery<TRawDocType>,
|
|
1264
|
+
options: QueryOptions<TRawDocType> & { lean: false }
|
|
1265
|
+
): QueryWithHelpers<
|
|
1266
|
+
THydratedDocumentType | null,
|
|
1267
|
+
THydratedDocumentType,
|
|
1268
|
+
TQueryHelpers,
|
|
1269
|
+
TLeanResultType,
|
|
1270
|
+
'findOneAndUpdate',
|
|
1271
|
+
TInstanceMethods & TVirtuals
|
|
1272
|
+
>;
|
|
1144
1273
|
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
1145
1274
|
filter: QueryFilter<TRawDocType>,
|
|
1146
1275
|
update: UpdateQuery<TRawDocType>,
|
package/types/schemaoptions.d.ts
CHANGED
package/types/schematypes.d.ts
CHANGED
|
@@ -115,7 +115,7 @@ declare module 'mongoose' {
|
|
|
115
115
|
* The default value for this path. If a function, Mongoose executes the function
|
|
116
116
|
* and uses the return value as the default.
|
|
117
117
|
*/
|
|
118
|
-
default?: DefaultType<T> | ((this: THydratedDocumentType, doc: THydratedDocumentType) => DefaultType<T> | null | undefined) | null;
|
|
118
|
+
default?: DefaultType<T> | ((this: THydratedDocumentType, doc: THydratedDocumentType) => DefaultType<T> | null | undefined) | null | undefined;
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
121
|
* The model that `populate()` should use if populating this path.
|