mongoose 7.6.1 → 7.6.2
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/model.js +34 -21
- package/lib/schema/SubdocumentPath.js +6 -0
- package/lib/schema/documentarray.js +6 -0
- package/lib/schema.js +12 -20
- package/package.json +1 -1
- package/types/document.d.ts +5 -0
- package/types/inferschematype.d.ts +1 -1
- package/types/models.d.ts +29 -1
package/lib/model.js
CHANGED
|
@@ -1060,40 +1060,45 @@ Model.prototype.$__deleteOne = function $__deleteOne(options, cb) {
|
|
|
1060
1060
|
};
|
|
1061
1061
|
|
|
1062
1062
|
/**
|
|
1063
|
-
* Returns
|
|
1063
|
+
* Returns the model instance used to create this document if no `name` specified.
|
|
1064
|
+
* If `name` specified, returns the model with the given `name`.
|
|
1064
1065
|
*
|
|
1065
1066
|
* #### Example:
|
|
1066
1067
|
*
|
|
1067
|
-
* const doc = new Tank;
|
|
1068
|
-
*
|
|
1068
|
+
* const doc = new Tank({});
|
|
1069
|
+
* doc.$model() === Tank; // true
|
|
1070
|
+
* await doc.$model('User').findById(id);
|
|
1069
1071
|
*
|
|
1070
|
-
* @param {String} name model name
|
|
1071
|
-
* @method model
|
|
1072
|
+
* @param {String} [name] model name
|
|
1073
|
+
* @method $model
|
|
1072
1074
|
* @api public
|
|
1073
1075
|
* @return {Model}
|
|
1074
1076
|
*/
|
|
1075
1077
|
|
|
1076
|
-
Model.prototype
|
|
1078
|
+
Model.prototype.$model = function $model(name) {
|
|
1079
|
+
if (arguments.length === 0) {
|
|
1080
|
+
return this.constructor;
|
|
1081
|
+
}
|
|
1077
1082
|
return this[modelDbSymbol].model(name);
|
|
1078
1083
|
};
|
|
1079
1084
|
|
|
1080
1085
|
/**
|
|
1081
|
-
* Returns
|
|
1086
|
+
* Returns the model instance used to create this document if no `name` specified.
|
|
1087
|
+
* If `name` specified, returns the model with the given `name`.
|
|
1082
1088
|
*
|
|
1083
1089
|
* #### Example:
|
|
1084
1090
|
*
|
|
1085
|
-
* const doc = new Tank;
|
|
1086
|
-
*
|
|
1091
|
+
* const doc = new Tank({});
|
|
1092
|
+
* doc.$model() === Tank; // true
|
|
1093
|
+
* await doc.$model('User').findById(id);
|
|
1087
1094
|
*
|
|
1088
|
-
* @param {String} name model name
|
|
1089
|
-
* @method
|
|
1095
|
+
* @param {String} [name] model name
|
|
1096
|
+
* @method model
|
|
1090
1097
|
* @api public
|
|
1091
1098
|
* @return {Model}
|
|
1092
1099
|
*/
|
|
1093
1100
|
|
|
1094
|
-
Model.prototype
|
|
1095
|
-
return this[modelDbSymbol].model(name);
|
|
1096
|
-
};
|
|
1101
|
+
Model.prototype.model = Model.prototype.$model;
|
|
1097
1102
|
|
|
1098
1103
|
/**
|
|
1099
1104
|
* Returns a document with `_id` only if at least one document exists in the database that matches
|
|
@@ -3780,6 +3785,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
|
|
|
3780
3785
|
}
|
|
3781
3786
|
|
|
3782
3787
|
setDefaultOptions();
|
|
3788
|
+
const discriminatorKey = this.schema.options.discriminatorKey;
|
|
3783
3789
|
|
|
3784
3790
|
const writeOperations = documents.reduce((accumulator, document, i) => {
|
|
3785
3791
|
if (!options.skipValidation) {
|
|
@@ -3810,6 +3816,12 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
|
|
|
3810
3816
|
|
|
3811
3817
|
_applyCustomWhere(document, where);
|
|
3812
3818
|
|
|
3819
|
+
// Set the discriminator key, so bulk write casting knows which
|
|
3820
|
+
// schema to use re: gh-13907
|
|
3821
|
+
if (document[discriminatorKey] != null && !(discriminatorKey in where)) {
|
|
3822
|
+
where[discriminatorKey] = document[discriminatorKey];
|
|
3823
|
+
}
|
|
3824
|
+
|
|
3813
3825
|
document.$__version(where, delta);
|
|
3814
3826
|
const writeOperation = { updateOne: { filter: where, update: changes } };
|
|
3815
3827
|
utils.injectTimestampsOption(writeOperation.updateOne, options.timestamps);
|
|
@@ -4752,8 +4764,6 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
|
|
4752
4764
|
|
|
4753
4765
|
schema._preCompile();
|
|
4754
4766
|
|
|
4755
|
-
model.prototype.$__setSchema(schema);
|
|
4756
|
-
|
|
4757
4767
|
const _userProvidedOptions = schema._userProvidedOptions || {};
|
|
4758
4768
|
|
|
4759
4769
|
const collectionOptions = {
|
|
@@ -4766,13 +4776,16 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
|
|
4766
4776
|
collectionOptions.autoCreate = schema.options.autoCreate;
|
|
4767
4777
|
}
|
|
4768
4778
|
|
|
4769
|
-
|
|
4779
|
+
const collection = connection.collection(
|
|
4770
4780
|
collectionName,
|
|
4771
4781
|
collectionOptions
|
|
4772
4782
|
);
|
|
4773
4783
|
|
|
4774
|
-
model.prototype
|
|
4775
|
-
model.prototype
|
|
4784
|
+
model.prototype.collection = collection;
|
|
4785
|
+
model.prototype.$collection = collection;
|
|
4786
|
+
model.prototype[modelCollectionSymbol] = collection;
|
|
4787
|
+
|
|
4788
|
+
model.prototype.$__setSchema(schema);
|
|
4776
4789
|
|
|
4777
4790
|
// apply methods and statics
|
|
4778
4791
|
applyMethods(model, schema);
|
|
@@ -4781,8 +4794,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
|
|
4781
4794
|
applyStaticHooks(model, schema.s.hooks, schema.statics);
|
|
4782
4795
|
|
|
4783
4796
|
model.schema = model.prototype.$__schema;
|
|
4784
|
-
model.collection =
|
|
4785
|
-
model.$__collection =
|
|
4797
|
+
model.collection = collection;
|
|
4798
|
+
model.$__collection = collection;
|
|
4786
4799
|
|
|
4787
4800
|
// Create custom query constructor
|
|
4788
4801
|
model.Query = function() {
|
|
@@ -55,6 +55,12 @@ function SubdocumentPath(schema, path, options) {
|
|
|
55
55
|
this.$isSingleNested = true;
|
|
56
56
|
this.base = schema.base;
|
|
57
57
|
SchemaType.call(this, path, options, 'Embedded');
|
|
58
|
+
|
|
59
|
+
if (schema._applyDiscriminators != null) {
|
|
60
|
+
for (const disc of schema._applyDiscriminators.keys()) {
|
|
61
|
+
this.discriminator(disc, schema._applyDiscriminators.get(disc));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
/*!
|
|
@@ -88,6 +88,12 @@ function DocumentArrayPath(key, schema, options, schemaOptions) {
|
|
|
88
88
|
|
|
89
89
|
this.$embeddedSchemaType.caster = this.Constructor;
|
|
90
90
|
this.$embeddedSchemaType.schema = this.schema;
|
|
91
|
+
|
|
92
|
+
if (schema._applyDiscriminators != null) {
|
|
93
|
+
for (const disc of schema._applyDiscriminators.keys()) {
|
|
94
|
+
this.discriminator(disc, schema._applyDiscriminators.get(disc));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
/**
|
package/lib/schema.js
CHANGED
|
@@ -37,6 +37,8 @@ const isPOJO = utils.isPOJO;
|
|
|
37
37
|
|
|
38
38
|
let id = 0;
|
|
39
39
|
|
|
40
|
+
const numberRE = /^\d+$/;
|
|
41
|
+
|
|
40
42
|
/**
|
|
41
43
|
* Schema constructor.
|
|
42
44
|
*
|
|
@@ -723,19 +725,6 @@ Schema.prototype.add = function add(obj, prefix) {
|
|
|
723
725
|
for (const key in val[0].discriminators) {
|
|
724
726
|
schemaType.discriminator(key, val[0].discriminators[key]);
|
|
725
727
|
}
|
|
726
|
-
} else if (val[0] != null && val[0].instanceOfSchema && val[0]._applyDiscriminators instanceof Map) {
|
|
727
|
-
const applyDiscriminators = val[0]._applyDiscriminators;
|
|
728
|
-
const schemaType = this.path(prefix + key);
|
|
729
|
-
for (const disc of applyDiscriminators.keys()) {
|
|
730
|
-
schemaType.discriminator(disc, applyDiscriminators.get(disc));
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
else if (val != null && val.instanceOfSchema && val._applyDiscriminators instanceof Map) {
|
|
734
|
-
const applyDiscriminators = val._applyDiscriminators;
|
|
735
|
-
const schemaType = this.path(prefix + key);
|
|
736
|
-
for (const disc of applyDiscriminators.keys()) {
|
|
737
|
-
schemaType.discriminator(disc, applyDiscriminators.get(disc));
|
|
738
|
-
}
|
|
739
728
|
}
|
|
740
729
|
} else if (Object.keys(val).length < 1) {
|
|
741
730
|
// Special-case: {} always interpreted as Mixed path so leaf at this node
|
|
@@ -1017,7 +1006,7 @@ Schema.prototype.path = function(path, obj) {
|
|
|
1017
1006
|
|
|
1018
1007
|
// subpaths?
|
|
1019
1008
|
return /\.\d+\.?.*$/.test(path)
|
|
1020
|
-
? getPositionalPath(this, path)
|
|
1009
|
+
? getPositionalPath(this, path, cleanPath)
|
|
1021
1010
|
: undefined;
|
|
1022
1011
|
}
|
|
1023
1012
|
|
|
@@ -1634,7 +1623,7 @@ Schema.prototype.pathType = function(path) {
|
|
|
1634
1623
|
}
|
|
1635
1624
|
|
|
1636
1625
|
if (/\.\d+\.|\.\d+$/.test(path)) {
|
|
1637
|
-
return getPositionalPathType(this, path);
|
|
1626
|
+
return getPositionalPathType(this, path, cleanPath);
|
|
1638
1627
|
}
|
|
1639
1628
|
return 'adhocOrUndefined';
|
|
1640
1629
|
};
|
|
@@ -1678,7 +1667,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
|
|
|
1678
1667
|
* @api private
|
|
1679
1668
|
*/
|
|
1680
1669
|
|
|
1681
|
-
function getPositionalPathType(self, path) {
|
|
1670
|
+
function getPositionalPathType(self, path, cleanPath) {
|
|
1682
1671
|
const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
|
|
1683
1672
|
if (subpaths.length < 2) {
|
|
1684
1673
|
return self.paths.hasOwnProperty(subpaths[0]) ?
|
|
@@ -1729,7 +1718,7 @@ function getPositionalPathType(self, path) {
|
|
|
1729
1718
|
val = val.schema.path(subpath);
|
|
1730
1719
|
}
|
|
1731
1720
|
|
|
1732
|
-
self.subpaths[
|
|
1721
|
+
self.subpaths[cleanPath] = val;
|
|
1733
1722
|
if (val) {
|
|
1734
1723
|
return 'real';
|
|
1735
1724
|
}
|
|
@@ -1744,9 +1733,9 @@ function getPositionalPathType(self, path) {
|
|
|
1744
1733
|
* ignore
|
|
1745
1734
|
*/
|
|
1746
1735
|
|
|
1747
|
-
function getPositionalPath(self, path) {
|
|
1748
|
-
getPositionalPathType(self, path);
|
|
1749
|
-
return self.subpaths[
|
|
1736
|
+
function getPositionalPath(self, path, cleanPath) {
|
|
1737
|
+
getPositionalPathType(self, path, cleanPath);
|
|
1738
|
+
return self.subpaths[cleanPath];
|
|
1750
1739
|
}
|
|
1751
1740
|
|
|
1752
1741
|
/**
|
|
@@ -2638,6 +2627,9 @@ Schema.prototype._getSchema = function(path) {
|
|
|
2638
2627
|
// Re: gh-5628, because `schema.path()` doesn't take $ into account.
|
|
2639
2628
|
parts[i] = '0';
|
|
2640
2629
|
}
|
|
2630
|
+
if (numberRE.test(parts[i])) {
|
|
2631
|
+
parts[i] = '$';
|
|
2632
|
+
}
|
|
2641
2633
|
}
|
|
2642
2634
|
return search(parts, _this);
|
|
2643
2635
|
};
|
package/package.json
CHANGED
package/types/document.d.ts
CHANGED
|
@@ -75,6 +75,7 @@ declare module 'mongoose' {
|
|
|
75
75
|
|
|
76
76
|
/** Returns the model with the given name on this document's associated connection. */
|
|
77
77
|
$model<ModelType = Model<unknown>>(name: string): ModelType;
|
|
78
|
+
$model<ModelType = Model<DocType>>(): ModelType;
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
81
|
* A string containing the current operation that Mongoose is executing
|
|
@@ -191,6 +192,10 @@ declare module 'mongoose' {
|
|
|
191
192
|
markModified<T extends keyof DocType>(path: T, scope?: any): void;
|
|
192
193
|
markModified(path: string, scope?: any): void;
|
|
193
194
|
|
|
195
|
+
/** Returns the model with the given name on this document's associated connection. */
|
|
196
|
+
model<ModelType = Model<unknown>>(name: string): ModelType;
|
|
197
|
+
model<ModelType = Model<DocType>>(): ModelType;
|
|
198
|
+
|
|
194
199
|
/** Returns the list of paths that have been modified. */
|
|
195
200
|
modifiedPaths(options?: { includeChildren?: boolean }): Array<string>;
|
|
196
201
|
|
|
@@ -219,7 +219,7 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
|
|
|
219
219
|
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt ? bigint :
|
|
220
220
|
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
|
|
221
221
|
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
|
|
222
|
-
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
|
|
222
|
+
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
|
|
223
223
|
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
|
|
224
224
|
PathValueType extends ArrayConstructor ? any[] :
|
|
225
225
|
PathValueType extends typeof Schema.Types.Mixed ? any:
|
package/types/models.d.ts
CHANGED
|
@@ -362,6 +362,34 @@ declare module 'mongoose' {
|
|
|
362
362
|
init(): Promise<THydratedDocumentType>;
|
|
363
363
|
|
|
364
364
|
/** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
|
|
365
|
+
insertMany(
|
|
366
|
+
docs: Array<TRawDocType>
|
|
367
|
+
): Promise<Array<THydratedDocumentType>>;
|
|
368
|
+
insertMany(
|
|
369
|
+
docs: Array<TRawDocType>,
|
|
370
|
+
options: InsertManyOptions & { lean: true; }
|
|
371
|
+
): Promise<Array<Require_id<TRawDocType>>>;
|
|
372
|
+
insertMany(
|
|
373
|
+
doc: Array<TRawDocType>,
|
|
374
|
+
options: InsertManyOptions & { ordered: false; rawResult: true; }
|
|
375
|
+
): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>> & {
|
|
376
|
+
mongoose: {
|
|
377
|
+
validationErrors: (CastError | Error.ValidatorError)[];
|
|
378
|
+
results: Array<
|
|
379
|
+
Error |
|
|
380
|
+
Object |
|
|
381
|
+
THydratedDocumentType
|
|
382
|
+
>
|
|
383
|
+
}
|
|
384
|
+
}>;
|
|
385
|
+
insertMany(
|
|
386
|
+
docs: Array<TRawDocType>,
|
|
387
|
+
options: InsertManyOptions & { lean: true, rawResult: true; }
|
|
388
|
+
): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>>>;
|
|
389
|
+
insertMany(
|
|
390
|
+
docs: Array<TRawDocType>,
|
|
391
|
+
options: InsertManyOptions & { rawResult: true; }
|
|
392
|
+
): Promise<mongodb.InsertManyResult<Require_id<THydratedDocumentType>>>;
|
|
365
393
|
insertMany<DocContents = TRawDocType>(
|
|
366
394
|
docs: Array<DocContents | TRawDocType>,
|
|
367
395
|
options: InsertManyOptions & { lean: true; }
|
|
@@ -375,7 +403,7 @@ declare module 'mongoose' {
|
|
|
375
403
|
options: InsertManyOptions & { ordered: false; rawResult: true; }
|
|
376
404
|
): Promise<mongodb.InsertManyResult<Require_id<DocContents>> & {
|
|
377
405
|
mongoose: {
|
|
378
|
-
validationErrors: Error[];
|
|
406
|
+
validationErrors: (CastError | Error.ValidatorError)[];
|
|
379
407
|
results: Array<
|
|
380
408
|
Error |
|
|
381
409
|
Object |
|