mongoose 8.20.1 → 8.20.3
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/aggregate.js +1 -1
- package/lib/cast.js +1 -1
- package/lib/connection.js +9 -9
- package/lib/document.js +58 -17
- package/lib/drivers/node-mongodb-native/connection.js +2 -2
- package/lib/error/objectParameter.js +1 -1
- package/lib/helpers/common.js +1 -1
- package/lib/helpers/indexes/applySchemaCollation.js +1 -1
- package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
- package/lib/helpers/model/applyMethods.js +1 -1
- package/lib/helpers/model/castBulkWrite.js +13 -6
- package/lib/helpers/populate/getModelsMapForPopulate.js +3 -3
- package/lib/helpers/populate/modelNamesFromRefPath.js +1 -1
- package/lib/helpers/populate/removeDeselectedForeignField.js +1 -1
- package/lib/helpers/projection/applyProjection.js +2 -2
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
- package/lib/helpers/setDefaultsOnInsert.js +2 -2
- package/lib/helpers/timestamps/setupTimestamps.js +1 -1
- package/lib/helpers/update/applyTimestampsToUpdate.js +38 -25
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +1 -1
- package/lib/model.js +12 -15
- package/lib/mongoose.js +3 -4
- package/lib/query.js +3 -3
- package/lib/schema/array.js +1 -1
- package/lib/schema.js +21 -21
- package/lib/schemaType.js +8 -8
- package/lib/types/array/index.js +5 -5
- package/lib/types/documentArray/index.js +6 -6
- package/lib/types/objectid.js +1 -1
- package/lib/utils.js +1 -15
- package/lib/virtualType.js +1 -1
- package/package.json +3 -1
- package/types/index.d.ts +1 -1
- package/types/inferrawdoctype.d.ts +1 -1
- package/types/inferschematype.d.ts +16 -23
- package/types/models.d.ts +38 -85
- package/types/virtuals.d.ts +3 -3
package/lib/model.js
CHANGED
|
@@ -351,7 +351,7 @@ Model.prototype.$__handleSave = function(options, callback) {
|
|
|
351
351
|
const asyncLocalStorage = this[modelDbSymbol].base.transactionAsyncLocalStorage?.getStore();
|
|
352
352
|
if (session != null) {
|
|
353
353
|
saveOptions.session = session;
|
|
354
|
-
} else if (!
|
|
354
|
+
} else if (!Object.hasOwn(options, 'session') && asyncLocalStorage?.session != null) {
|
|
355
355
|
// Only set session from asyncLocalStorage if `session` option wasn't originally passed in options
|
|
356
356
|
saveOptions.session = asyncLocalStorage.session;
|
|
357
357
|
}
|
|
@@ -523,11 +523,9 @@ Model.prototype.$__save = function(options, callback) {
|
|
|
523
523
|
const versionBump = this.$__.version;
|
|
524
524
|
// was this an update that required a version bump?
|
|
525
525
|
if (versionBump && !this.$__.inserting) {
|
|
526
|
-
const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
|
|
527
|
-
this.$__.version = undefined;
|
|
528
|
-
const key = this.$__schema.options.versionKey;
|
|
529
|
-
const version = this.$__getValue(key) || 0;
|
|
530
526
|
if (numAffected <= 0) {
|
|
527
|
+
const key = this.$__schema.options.versionKey;
|
|
528
|
+
const version = this.$__getValue(key) || 0;
|
|
531
529
|
// the update failed. pass an error back
|
|
532
530
|
this.$__undoReset();
|
|
533
531
|
const err = this.$__.$versionError ||
|
|
@@ -535,10 +533,7 @@ Model.prototype.$__save = function(options, callback) {
|
|
|
535
533
|
return callback(err, this);
|
|
536
534
|
}
|
|
537
535
|
|
|
538
|
-
|
|
539
|
-
if (doIncrement) {
|
|
540
|
-
this.$__setValue(key, version + 1);
|
|
541
|
-
}
|
|
536
|
+
this._applyVersionIncrement();
|
|
542
537
|
}
|
|
543
538
|
if (result != null && numAffected <= 0) {
|
|
544
539
|
this.$__undoReset();
|
|
@@ -620,7 +615,7 @@ Model.prototype.save = async function save(options) {
|
|
|
620
615
|
}
|
|
621
616
|
|
|
622
617
|
options = new SaveOptions(options);
|
|
623
|
-
if (
|
|
618
|
+
if (Object.hasOwn(options, 'session')) {
|
|
624
619
|
this.$session(options.session);
|
|
625
620
|
}
|
|
626
621
|
if (this.$__.timestamps != null) {
|
|
@@ -785,7 +780,7 @@ Model.prototype.deleteOne = function deleteOne(options) {
|
|
|
785
780
|
options = {};
|
|
786
781
|
}
|
|
787
782
|
|
|
788
|
-
if (
|
|
783
|
+
if (Object.hasOwn(options, 'session')) {
|
|
789
784
|
this.$session(options.session);
|
|
790
785
|
}
|
|
791
786
|
|
|
@@ -3033,7 +3028,7 @@ Model.$__insertMany = function(arr, options, callback) {
|
|
|
3033
3028
|
const lean = !!options.lean;
|
|
3034
3029
|
|
|
3035
3030
|
const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore();
|
|
3036
|
-
if ((!options || !
|
|
3031
|
+
if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) {
|
|
3037
3032
|
options = { ...options, session: asyncLocalStorage.session };
|
|
3038
3033
|
}
|
|
3039
3034
|
|
|
@@ -3429,7 +3424,7 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
|
|
|
3429
3424
|
|
|
3430
3425
|
const validations = options?._skipCastBulkWrite ? [] : ops.map(op => castBulkWrite(this, op, options));
|
|
3431
3426
|
const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore();
|
|
3432
|
-
if ((!options || !
|
|
3427
|
+
if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) {
|
|
3433
3428
|
options = { ...options, session: asyncLocalStorage.session };
|
|
3434
3429
|
}
|
|
3435
3430
|
|
|
@@ -3666,6 +3661,8 @@ function handleSuccessfulWrite(document) {
|
|
|
3666
3661
|
}
|
|
3667
3662
|
|
|
3668
3663
|
document.$__reset();
|
|
3664
|
+
document._applyVersionIncrement();
|
|
3665
|
+
|
|
3669
3666
|
document.schema.s.hooks.execPost('save', document, [document], {}, (err) => {
|
|
3670
3667
|
if (err) {
|
|
3671
3668
|
reject(err);
|
|
@@ -5187,10 +5184,10 @@ Model._applyQueryMiddleware = function _applyQueryMiddleware() {
|
|
|
5187
5184
|
|
|
5188
5185
|
function _getContexts(hook) {
|
|
5189
5186
|
const ret = {};
|
|
5190
|
-
if (
|
|
5187
|
+
if (Object.hasOwn(hook, 'query')) {
|
|
5191
5188
|
ret.query = hook.query;
|
|
5192
5189
|
}
|
|
5193
|
-
if (
|
|
5190
|
+
if (Object.hasOwn(hook, 'document')) {
|
|
5194
5191
|
ret.document = hook.document;
|
|
5195
5192
|
}
|
|
5196
5193
|
return ret;
|
package/lib/mongoose.js
CHANGED
|
@@ -600,12 +600,11 @@ Mongoose.prototype.model = function model(name, schema, collection, options) {
|
|
|
600
600
|
|
|
601
601
|
// connection.model() may be passing a different schema for
|
|
602
602
|
// an existing model name. in this case don't read from cache.
|
|
603
|
-
const overwriteModels = _mongoose.options
|
|
603
|
+
const overwriteModels = Object.hasOwn(_mongoose.options, 'overwriteModels') ?
|
|
604
604
|
_mongoose.options.overwriteModels :
|
|
605
605
|
options.overwriteModels;
|
|
606
|
-
if (_mongoose.models
|
|
607
|
-
if (originalSchema &&
|
|
608
|
-
originalSchema.instanceOfSchema &&
|
|
606
|
+
if (Object.hasOwn(_mongoose.models, name) && options.cache !== false && overwriteModels !== true) {
|
|
607
|
+
if (originalSchema?.instanceOfSchema &&
|
|
609
608
|
originalSchema !== _mongoose.models[name].schema) {
|
|
610
609
|
throw new _mongoose.Error.OverwriteModelError(name);
|
|
611
610
|
}
|
package/lib/query.js
CHANGED
|
@@ -2094,7 +2094,7 @@ Query.prototype._optionsForExec = function(model) {
|
|
|
2094
2094
|
applyWriteConcern(model.schema, options);
|
|
2095
2095
|
|
|
2096
2096
|
const asyncLocalStorage = this.model?.db?.base.transactionAsyncLocalStorage?.getStore();
|
|
2097
|
-
if (!this.options
|
|
2097
|
+
if (!Object.hasOwn(this.options, 'session') && asyncLocalStorage?.session != null) {
|
|
2098
2098
|
options.session = asyncLocalStorage.session;
|
|
2099
2099
|
}
|
|
2100
2100
|
|
|
@@ -4590,7 +4590,7 @@ Query.prototype.exec = async function exec(op) {
|
|
|
4590
4590
|
throw new MongooseError('Query has invalid `op`: "' + this.op + '"');
|
|
4591
4591
|
}
|
|
4592
4592
|
|
|
4593
|
-
if (this.options && this.options.sort && typeof this.options.sort === 'object' && this.options.sort
|
|
4593
|
+
if (this.options && this.options.sort && typeof this.options.sort === 'object' && Object.hasOwn(this.options.sort, '')) {
|
|
4594
4594
|
throw new Error('Invalid field "" passed to sort()');
|
|
4595
4595
|
}
|
|
4596
4596
|
|
|
@@ -5039,7 +5039,7 @@ Query.prototype.cast = function(model, obj) {
|
|
|
5039
5039
|
model = model || this.model;
|
|
5040
5040
|
const discriminatorKey = model.schema.options.discriminatorKey;
|
|
5041
5041
|
if (obj != null &&
|
|
5042
|
-
|
|
5042
|
+
Object.hasOwn(obj, discriminatorKey)) {
|
|
5043
5043
|
model = getDiscriminatorByValue(model.discriminators, obj[discriminatorKey]) || model;
|
|
5044
5044
|
}
|
|
5045
5045
|
|
package/lib/schema/array.js
CHANGED
|
@@ -81,7 +81,7 @@ function SchemaArray(key, cast, options, schemaOptions, parentSchema) {
|
|
|
81
81
|
: utils.getFunctionName(cast);
|
|
82
82
|
|
|
83
83
|
const Types = require('./index.js');
|
|
84
|
-
const caster =
|
|
84
|
+
const caster = Object.hasOwn(Types, name) ? Types[name] : cast;
|
|
85
85
|
|
|
86
86
|
this.casterConstructor = caster;
|
|
87
87
|
|
package/lib/schema.js
CHANGED
|
@@ -1441,17 +1441,17 @@ Schema.prototype._gatherChildSchemas = function _gatherChildSchemas() {
|
|
|
1441
1441
|
*/
|
|
1442
1442
|
|
|
1443
1443
|
function _getPath(schema, path, cleanPath) {
|
|
1444
|
-
if (schema.paths
|
|
1444
|
+
if (Object.hasOwn(schema.paths, path)) {
|
|
1445
1445
|
return schema.paths[path];
|
|
1446
1446
|
}
|
|
1447
|
-
if (schema.subpaths
|
|
1447
|
+
if (Object.hasOwn(schema.subpaths, cleanPath)) {
|
|
1448
1448
|
const subpath = schema.subpaths[cleanPath];
|
|
1449
1449
|
if (subpath === 'nested') {
|
|
1450
1450
|
return undefined;
|
|
1451
1451
|
}
|
|
1452
1452
|
return subpath;
|
|
1453
1453
|
}
|
|
1454
|
-
if (schema.singleNestedPaths
|
|
1454
|
+
if (Object.hasOwn(schema.singleNestedPaths, cleanPath) && typeof schema.singleNestedPaths[cleanPath] === 'object') {
|
|
1455
1455
|
const singleNestedPath = schema.singleNestedPaths[cleanPath];
|
|
1456
1456
|
if (singleNestedPath === 'nested') {
|
|
1457
1457
|
return undefined;
|
|
@@ -1639,20 +1639,20 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
|
|
|
1639
1639
|
childSchemaOptions.typeKey = options.typeKey;
|
|
1640
1640
|
}
|
|
1641
1641
|
// propagate 'strict' option to child schema
|
|
1642
|
-
if (
|
|
1642
|
+
if (Object.hasOwn(options, 'strict')) {
|
|
1643
1643
|
childSchemaOptions.strict = options.strict;
|
|
1644
1644
|
}
|
|
1645
|
-
if (
|
|
1645
|
+
if (Object.hasOwn(options, 'strictQuery')) {
|
|
1646
1646
|
childSchemaOptions.strictQuery = options.strictQuery;
|
|
1647
1647
|
}
|
|
1648
|
-
if (
|
|
1648
|
+
if (Object.hasOwn(options, 'toObject')) {
|
|
1649
1649
|
childSchemaOptions.toObject = utils.omit(options.toObject, ['transform']);
|
|
1650
1650
|
}
|
|
1651
|
-
if (
|
|
1651
|
+
if (Object.hasOwn(options, 'toJSON')) {
|
|
1652
1652
|
childSchemaOptions.toJSON = utils.omit(options.toJSON, ['transform']);
|
|
1653
1653
|
}
|
|
1654
1654
|
|
|
1655
|
-
if (this._userProvidedOptions
|
|
1655
|
+
if (Object.hasOwn(this._userProvidedOptions, '_id')) {
|
|
1656
1656
|
childSchemaOptions._id = this._userProvidedOptions._id;
|
|
1657
1657
|
} else if (Schema.Types.DocumentArray.defaultOptions._id != null) {
|
|
1658
1658
|
childSchemaOptions._id = Schema.Types.DocumentArray.defaultOptions._id;
|
|
@@ -1689,7 +1689,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
|
|
|
1689
1689
|
`Could not determine the embedded type for array \`${path}\`. ` +
|
|
1690
1690
|
'See https://mongoosejs.com/docs/guide.html#definition for more info on supported schema syntaxes.');
|
|
1691
1691
|
}
|
|
1692
|
-
if (!
|
|
1692
|
+
if (!Object.hasOwn(MongooseTypes, name)) {
|
|
1693
1693
|
throw new TypeError('Invalid schema configuration: ' +
|
|
1694
1694
|
`\`${name}\` is not a valid type within the array \`${path}\`.` +
|
|
1695
1695
|
'See https://bit.ly/mongoose-schematypes for a list of valid schema types.');
|
|
@@ -1850,24 +1850,24 @@ Schema.prototype.indexedPaths = function indexedPaths() {
|
|
|
1850
1850
|
*/
|
|
1851
1851
|
|
|
1852
1852
|
Schema.prototype.pathType = function(path) {
|
|
1853
|
-
if (this.paths
|
|
1853
|
+
if (Object.hasOwn(this.paths, path)) {
|
|
1854
1854
|
return 'real';
|
|
1855
1855
|
}
|
|
1856
|
-
if (this.virtuals
|
|
1856
|
+
if (Object.hasOwn(this.virtuals, path)) {
|
|
1857
1857
|
return 'virtual';
|
|
1858
1858
|
}
|
|
1859
|
-
if (this.nested
|
|
1859
|
+
if (Object.hasOwn(this.nested, path)) {
|
|
1860
1860
|
return 'nested';
|
|
1861
1861
|
}
|
|
1862
1862
|
|
|
1863
1863
|
// Convert to '.$' to check subpaths re: gh-6405
|
|
1864
1864
|
const cleanPath = _pathToPositionalSyntax(path);
|
|
1865
1865
|
|
|
1866
|
-
if (this.subpaths
|
|
1866
|
+
if (Object.hasOwn(this.subpaths, cleanPath) || Object.hasOwn(this.subpaths, path)) {
|
|
1867
1867
|
return 'real';
|
|
1868
1868
|
}
|
|
1869
1869
|
|
|
1870
|
-
const singleNestedPath = this.singleNestedPaths
|
|
1870
|
+
const singleNestedPath = Object.hasOwn(this.singleNestedPaths, cleanPath) || Object.hasOwn(this.singleNestedPaths, path);
|
|
1871
1871
|
if (singleNestedPath) {
|
|
1872
1872
|
return singleNestedPath === 'nested' ? 'nested' : 'real';
|
|
1873
1873
|
}
|
|
@@ -1897,7 +1897,7 @@ Schema.prototype.hasMixedParent = function(path) {
|
|
|
1897
1897
|
path = '';
|
|
1898
1898
|
for (let i = 0; i < subpaths.length; ++i) {
|
|
1899
1899
|
path = i > 0 ? path + '.' + subpaths[i] : subpaths[i];
|
|
1900
|
-
if (this.paths
|
|
1900
|
+
if (Object.hasOwn(this.paths, path) &&
|
|
1901
1901
|
this.paths[path] instanceof MongooseTypes.Mixed) {
|
|
1902
1902
|
return this.paths[path];
|
|
1903
1903
|
}
|
|
@@ -1926,7 +1926,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
|
|
|
1926
1926
|
function getPositionalPathType(self, path, cleanPath) {
|
|
1927
1927
|
const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
|
|
1928
1928
|
if (subpaths.length < 2) {
|
|
1929
|
-
return self.paths
|
|
1929
|
+
return Object.hasOwn(self.paths, subpaths[0]) ?
|
|
1930
1930
|
self.paths[subpaths[0]] :
|
|
1931
1931
|
'adhocOrUndefined';
|
|
1932
1932
|
}
|
|
@@ -2633,7 +2633,7 @@ Schema.prototype.virtual = function(name, options) {
|
|
|
2633
2633
|
*/
|
|
2634
2634
|
|
|
2635
2635
|
Schema.prototype.virtualpath = function(name) {
|
|
2636
|
-
return this.virtuals
|
|
2636
|
+
return Object.hasOwn(this.virtuals, name) ? this.virtuals[name] : null;
|
|
2637
2637
|
};
|
|
2638
2638
|
|
|
2639
2639
|
/**
|
|
@@ -2781,8 +2781,8 @@ Schema.prototype.loadClass = function(model, virtualsOnly) {
|
|
|
2781
2781
|
// Stop copying when hit certain base classes
|
|
2782
2782
|
if (model === Object.prototype ||
|
|
2783
2783
|
model === Function.prototype ||
|
|
2784
|
-
model.prototype
|
|
2785
|
-
model.prototype
|
|
2784
|
+
Object.hasOwn(model.prototype, '$isMongooseModelPrototype') ||
|
|
2785
|
+
Object.hasOwn(model.prototype, '$isMongooseDocumentPrototype')) {
|
|
2786
2786
|
return this;
|
|
2787
2787
|
}
|
|
2788
2788
|
|
|
@@ -2795,7 +2795,7 @@ Schema.prototype.loadClass = function(model, virtualsOnly) {
|
|
|
2795
2795
|
return;
|
|
2796
2796
|
}
|
|
2797
2797
|
const prop = Object.getOwnPropertyDescriptor(model, name);
|
|
2798
|
-
if (
|
|
2798
|
+
if (Object.hasOwn(prop, 'value')) {
|
|
2799
2799
|
this.static(name, prop.value);
|
|
2800
2800
|
}
|
|
2801
2801
|
}, this);
|
|
@@ -3021,7 +3021,7 @@ Schema.prototype._transformDuplicateKeyError = function _transformDuplicateKeyEr
|
|
|
3021
3021
|
return error;
|
|
3022
3022
|
}
|
|
3023
3023
|
const firstKey = keys[0];
|
|
3024
|
-
if (!this._duplicateKeyErrorMessagesByPath
|
|
3024
|
+
if (!Object.hasOwn(this._duplicateKeyErrorMessagesByPath, firstKey)) {
|
|
3025
3025
|
return error;
|
|
3026
3026
|
}
|
|
3027
3027
|
return new MongooseError(this._duplicateKeyErrorMessagesByPath[firstKey], { cause: error });
|
package/lib/schemaType.js
CHANGED
|
@@ -47,10 +47,10 @@ function SchemaType(path, options, instance, parentSchema) {
|
|
|
47
47
|
this.instance = instance;
|
|
48
48
|
this.schemaName = this.constructor.schemaName;
|
|
49
49
|
this.validators = [];
|
|
50
|
-
this.getters = this.constructor
|
|
50
|
+
this.getters = Object.hasOwn(this.constructor, 'getters') ?
|
|
51
51
|
this.constructor.getters.slice() :
|
|
52
52
|
[];
|
|
53
|
-
this.setters = this.constructor
|
|
53
|
+
this.setters = Object.hasOwn(this.constructor, 'setters') ?
|
|
54
54
|
this.constructor.setters.slice() :
|
|
55
55
|
[];
|
|
56
56
|
|
|
@@ -63,7 +63,7 @@ function SchemaType(path, options, instance, parentSchema) {
|
|
|
63
63
|
for (const option of defaultOptionsKeys) {
|
|
64
64
|
if (option === 'validate') {
|
|
65
65
|
this.validate(defaultOptions.validate);
|
|
66
|
-
} else if (
|
|
66
|
+
} else if (Object.hasOwn(defaultOptions, option) && !Object.hasOwn(options, option)) {
|
|
67
67
|
options[option] = defaultOptions[option];
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -340,7 +340,7 @@ SchemaType.prototype.cast = function cast() {
|
|
|
340
340
|
*/
|
|
341
341
|
|
|
342
342
|
SchemaType.set = function set(option, value) {
|
|
343
|
-
if (!
|
|
343
|
+
if (!Object.hasOwn(this, 'defaultOptions')) {
|
|
344
344
|
this.defaultOptions = Object.assign({}, this.defaultOptions);
|
|
345
345
|
}
|
|
346
346
|
this.defaultOptions[option] = value;
|
|
@@ -363,7 +363,7 @@ SchemaType.set = function set(option, value) {
|
|
|
363
363
|
*/
|
|
364
364
|
|
|
365
365
|
SchemaType.get = function(getter) {
|
|
366
|
-
this.getters =
|
|
366
|
+
this.getters = Object.hasOwn(this, 'getters') ? this.getters : [];
|
|
367
367
|
this.getters.push(getter);
|
|
368
368
|
};
|
|
369
369
|
|
|
@@ -504,7 +504,7 @@ SchemaType.prototype.unique = function unique(value, message) {
|
|
|
504
504
|
'false and `unique` set to true');
|
|
505
505
|
}
|
|
506
506
|
|
|
507
|
-
if (!this.options
|
|
507
|
+
if (!Object.hasOwn(this.options, 'index') && value === false) {
|
|
508
508
|
return this;
|
|
509
509
|
}
|
|
510
510
|
|
|
@@ -543,7 +543,7 @@ SchemaType.prototype.text = function(bool) {
|
|
|
543
543
|
'false and `text` set to true');
|
|
544
544
|
}
|
|
545
545
|
|
|
546
|
-
if (!this.options
|
|
546
|
+
if (!Object.hasOwn(this.options, 'index') && bool === false) {
|
|
547
547
|
return this;
|
|
548
548
|
}
|
|
549
549
|
|
|
@@ -580,7 +580,7 @@ SchemaType.prototype.sparse = function(bool) {
|
|
|
580
580
|
'false and `sparse` set to true');
|
|
581
581
|
}
|
|
582
582
|
|
|
583
|
-
if (!this.options
|
|
583
|
+
if (!Object.hasOwn(this.options, 'index') && bool === false) {
|
|
584
584
|
return this;
|
|
585
585
|
}
|
|
586
586
|
|
package/lib/types/array/index.js
CHANGED
|
@@ -79,13 +79,13 @@ function MongooseArray(values, path, doc, schematype) {
|
|
|
79
79
|
|
|
80
80
|
const proxy = new Proxy(__array, {
|
|
81
81
|
get: function(target, prop) {
|
|
82
|
-
if (
|
|
82
|
+
if (Object.hasOwn(internals, prop)) {
|
|
83
83
|
return internals[prop];
|
|
84
84
|
}
|
|
85
|
-
if (
|
|
85
|
+
if (Object.hasOwn(mongooseArrayMethods, prop)) {
|
|
86
86
|
return mongooseArrayMethods[prop];
|
|
87
87
|
}
|
|
88
|
-
if (schematype && schematype.virtuals && schematype.virtuals
|
|
88
|
+
if (schematype && schematype.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
|
|
89
89
|
return schematype.virtuals[prop].applyGetters(undefined, target);
|
|
90
90
|
}
|
|
91
91
|
if (typeof prop === 'string' && numberRE.test(prop) && schematype?.$embeddedSchemaType != null) {
|
|
@@ -97,9 +97,9 @@ function MongooseArray(values, path, doc, schematype) {
|
|
|
97
97
|
set: function(target, prop, value) {
|
|
98
98
|
if (typeof prop === 'string' && numberRE.test(prop)) {
|
|
99
99
|
mongooseArrayMethods.set.call(proxy, prop, value, false);
|
|
100
|
-
} else if (
|
|
100
|
+
} else if (Object.hasOwn(internals, prop)) {
|
|
101
101
|
internals[prop] = value;
|
|
102
|
-
} else if (schematype && schematype.virtuals
|
|
102
|
+
} else if (schematype?.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
|
|
103
103
|
schematype.virtuals[prop].applySetters(value, target);
|
|
104
104
|
} else {
|
|
105
105
|
__array[prop] = value;
|
|
@@ -73,16 +73,16 @@ function MongooseDocumentArray(values, path, doc, schematype) {
|
|
|
73
73
|
prop === 'isMongooseDocumentArrayProxy') {
|
|
74
74
|
return true;
|
|
75
75
|
}
|
|
76
|
-
if (
|
|
76
|
+
if (Object.hasOwn(internals, prop)) {
|
|
77
77
|
return internals[prop];
|
|
78
78
|
}
|
|
79
|
-
if (
|
|
79
|
+
if (Object.hasOwn(DocumentArrayMethods, prop)) {
|
|
80
80
|
return DocumentArrayMethods[prop];
|
|
81
81
|
}
|
|
82
|
-
if (schematype && schematype.virtuals && schematype.virtuals
|
|
82
|
+
if (schematype && schematype.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
|
|
83
83
|
return schematype.virtuals[prop].applyGetters(undefined, target);
|
|
84
84
|
}
|
|
85
|
-
if (
|
|
85
|
+
if (Object.hasOwn(ArrayMethods, prop)) {
|
|
86
86
|
return ArrayMethods[prop];
|
|
87
87
|
}
|
|
88
88
|
|
|
@@ -91,9 +91,9 @@ function MongooseDocumentArray(values, path, doc, schematype) {
|
|
|
91
91
|
set: function(target, prop, value) {
|
|
92
92
|
if (typeof prop === 'string' && numberRE.test(prop)) {
|
|
93
93
|
DocumentArrayMethods.set.call(proxy, prop, value, false);
|
|
94
|
-
} else if (
|
|
94
|
+
} else if (Object.hasOwn(internals, prop)) {
|
|
95
95
|
internals[prop] = value;
|
|
96
|
-
} else if (schematype && schematype.virtuals
|
|
96
|
+
} else if (schematype?.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
|
|
97
97
|
schematype.virtuals[prop].applySetters(value, target);
|
|
98
98
|
} else {
|
|
99
99
|
__array[prop] = value;
|
package/lib/types/objectid.js
CHANGED
|
@@ -30,7 +30,7 @@ Object.defineProperty(ObjectId.prototype, '_id', {
|
|
|
30
30
|
* Convenience `valueOf()` to allow comparing ObjectIds using double equals re: gh-7299
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
|
-
if (!ObjectId.prototype
|
|
33
|
+
if (!Object.hasOwn(ObjectId.prototype, 'valueOf')) {
|
|
34
34
|
ObjectId.prototype.valueOf = function objectIdValueOf() {
|
|
35
35
|
return this.toString();
|
|
36
36
|
};
|
package/lib/utils.js
CHANGED
|
@@ -708,18 +708,6 @@ exports.object.vals = function vals(o) {
|
|
|
708
708
|
return ret;
|
|
709
709
|
};
|
|
710
710
|
|
|
711
|
-
const hop = Object.prototype.hasOwnProperty;
|
|
712
|
-
|
|
713
|
-
/**
|
|
714
|
-
* Safer helper for hasOwnProperty checks
|
|
715
|
-
*
|
|
716
|
-
* @param {Object} obj
|
|
717
|
-
* @param {String} prop
|
|
718
|
-
*/
|
|
719
|
-
|
|
720
|
-
exports.object.hasOwnProperty = function(obj, prop) {
|
|
721
|
-
return hop.call(obj, prop);
|
|
722
|
-
};
|
|
723
711
|
|
|
724
712
|
/**
|
|
725
713
|
* Determine if `val` is null or undefined
|
|
@@ -770,8 +758,6 @@ exports.array.flatten = function flatten(arr, filter, ret) {
|
|
|
770
758
|
* ignore
|
|
771
759
|
*/
|
|
772
760
|
|
|
773
|
-
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
774
|
-
|
|
775
761
|
exports.hasUserDefinedProperty = function(obj, key) {
|
|
776
762
|
if (obj == null) {
|
|
777
763
|
return false;
|
|
@@ -786,7 +772,7 @@ exports.hasUserDefinedProperty = function(obj, key) {
|
|
|
786
772
|
return false;
|
|
787
773
|
}
|
|
788
774
|
|
|
789
|
-
if (
|
|
775
|
+
if (Object.hasOwn(obj, key)) {
|
|
790
776
|
return true;
|
|
791
777
|
}
|
|
792
778
|
if (typeof obj === 'object' && key in obj) {
|
package/lib/virtualType.js
CHANGED
|
@@ -143,7 +143,7 @@ VirtualType.prototype.set = function(fn) {
|
|
|
143
143
|
VirtualType.prototype.applyGetters = function(value, doc) {
|
|
144
144
|
if (utils.hasUserDefinedProperty(this.options, ['ref', 'refPath']) &&
|
|
145
145
|
doc.$$populatedVirtuals &&
|
|
146
|
-
doc.$$populatedVirtuals
|
|
146
|
+
Object.hasOwn(doc.$$populatedVirtuals, this.path)) {
|
|
147
147
|
value = doc.$$populatedVirtuals[this.path];
|
|
148
148
|
}
|
|
149
149
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "8.20.
|
|
4
|
+
"version": "8.20.3",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -91,6 +91,7 @@
|
|
|
91
91
|
"docs:prepare:publish:5x": "git checkout 5.x && git merge 5.x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && git checkout gh-pages && npm run docs:copy:tmp:5x",
|
|
92
92
|
"docs:prepare:publish:6x": "git checkout 6.x && git merge 6.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/6.x ./tmp && git checkout gh-pages && npm run docs:copy:tmp:6x",
|
|
93
93
|
"docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/7.x && mv ./tmp ./docs/7.x",
|
|
94
|
+
"docs:prepare:publish:8x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/8.x && mv ./tmp ./docs/8.x",
|
|
94
95
|
"docs:check-links": "blc http://127.0.0.1:8089 -ro",
|
|
95
96
|
"lint": "eslint .",
|
|
96
97
|
"lint-js": "eslint . --ext .js --ext .cjs",
|
|
@@ -113,6 +114,7 @@
|
|
|
113
114
|
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
|
|
114
115
|
"test-coverage": "nyc --reporter=html --reporter=text npm test",
|
|
115
116
|
"ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
|
|
117
|
+
"ts-benchmark:local": "node ./scripts/create-tarball && cd ./benchmarks/typescript/simple && rm -rf ./node_modules && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
|
|
116
118
|
"attest-benchmark": "node ./benchmarks/typescript/infer.bench.mts"
|
|
117
119
|
},
|
|
118
120
|
"main": "./index.js",
|
package/types/index.d.ts
CHANGED
|
@@ -542,7 +542,7 @@ declare module 'mongoose' {
|
|
|
542
542
|
|
|
543
543
|
/** Adds static "class" methods to Models compiled from this schema. */
|
|
544
544
|
static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
|
|
545
|
-
static(obj:
|
|
545
|
+
static(obj: Partial<TStaticMethods> & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;
|
|
546
546
|
static(name: string, fn: (this: TModelType, ...args: any[]) => any): this;
|
|
547
547
|
|
|
548
548
|
/** Object of currently defined statics on this schema. */
|
|
@@ -97,7 +97,7 @@ declare module 'mongoose' {
|
|
|
97
97
|
ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
|
|
98
98
|
: PathValueType extends ArrayConstructor ? any[]
|
|
99
99
|
: PathValueType extends typeof Schema.Types.Mixed ? any
|
|
100
|
-
:
|
|
100
|
+
: PathValueType extends ObjectConstructor ? any
|
|
101
101
|
: IfEquals<PathValueType, {}> extends true ? any
|
|
102
102
|
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
|
|
103
103
|
: PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>
|
|
@@ -127,11 +127,6 @@ declare module 'mongoose' {
|
|
|
127
127
|
: T;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
type IsPathDefaultUndefined<PathType> =
|
|
131
|
-
PathType extends { default: undefined } ? true
|
|
132
|
-
: PathType extends { default: (...args: any[]) => undefined } ? true
|
|
133
|
-
: false;
|
|
134
|
-
|
|
135
130
|
type RequiredPropertyDefinition =
|
|
136
131
|
| {
|
|
137
132
|
required: true | string | [true, string | undefined] | { isRequired: true };
|
|
@@ -150,16 +145,17 @@ type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
|
|
|
150
145
|
P extends { required: false } ?
|
|
151
146
|
false
|
|
152
147
|
: true
|
|
153
|
-
: P extends
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
: true
|
|
157
|
-
: P extends Record<TypeKey, any> ?
|
|
158
|
-
P extends { default: any } ?
|
|
159
|
-
IfEquals<P['default'], undefined, false, true>
|
|
160
|
-
: false
|
|
148
|
+
: P extends { default: undefined | null | ((...args: any[]) => undefined) | ((...args: any[]) => null) } ? false
|
|
149
|
+
: P extends { default: any } ? true
|
|
150
|
+
: P extends Record<TypeKey, ArrayConstructor | any[]> ? true
|
|
161
151
|
: false;
|
|
162
152
|
|
|
153
|
+
// Internal type used to efficiently check for never or any types
|
|
154
|
+
// can be efficiently checked like:
|
|
155
|
+
// `[T] extends [neverOrAny] ? T : ...`
|
|
156
|
+
// to avoid edge cases
|
|
157
|
+
type neverOrAny = ' ~neverOrAny~';
|
|
158
|
+
|
|
163
159
|
/**
|
|
164
160
|
* @summary A Utility to obtain schema's required path keys.
|
|
165
161
|
* @param {T} T A generic refers to document definition.
|
|
@@ -238,6 +234,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> =
|
|
|
238
234
|
|
|
239
235
|
type IsSchemaTypeFromBuiltinClass<T> =
|
|
240
236
|
T extends typeof String ? true
|
|
237
|
+
: unknown extends Buffer ? false
|
|
241
238
|
: T extends typeof Number ? true
|
|
242
239
|
: T extends typeof Boolean ? true
|
|
243
240
|
: T extends typeof Buffer ? true
|
|
@@ -254,7 +251,6 @@ type IsSchemaTypeFromBuiltinClass<T> =
|
|
|
254
251
|
: T extends Types.Decimal128 ? true
|
|
255
252
|
: T extends NativeDate ? true
|
|
256
253
|
: T extends typeof Schema.Types.Mixed ? true
|
|
257
|
-
: unknown extends Buffer ? false
|
|
258
254
|
: T extends Buffer ? true
|
|
259
255
|
: false;
|
|
260
256
|
|
|
@@ -270,12 +266,10 @@ type ResolvePathType<
|
|
|
270
266
|
Options extends SchemaTypeOptions<PathValueType> = {},
|
|
271
267
|
TypeKey extends string = DefaultSchemaOptions['typeKey'],
|
|
272
268
|
TypeHint = never
|
|
273
|
-
> =
|
|
274
|
-
|
|
275
|
-
never,
|
|
276
|
-
PathValueType extends Schema ? InferSchemaType<PathValueType>
|
|
269
|
+
> = [TypeHint] extends [never]
|
|
270
|
+
? PathValueType extends Schema ? InferSchemaType<PathValueType>
|
|
277
271
|
: PathValueType extends AnyArray<infer Item> ?
|
|
278
|
-
|
|
272
|
+
[Item] extends [never]
|
|
279
273
|
? any[]
|
|
280
274
|
: Item extends Schema ?
|
|
281
275
|
// If Item is a schema, infer its type.
|
|
@@ -314,7 +308,7 @@ type ResolvePathType<
|
|
|
314
308
|
: never
|
|
315
309
|
: PathValueType extends ArrayConstructor ? any[]
|
|
316
310
|
: PathValueType extends typeof Schema.Types.Mixed ? any
|
|
317
|
-
:
|
|
311
|
+
: PathValueType extends ObjectConstructor ? any
|
|
318
312
|
: IfEquals<PathValueType, {}> extends true ? any
|
|
319
313
|
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
|
|
320
314
|
: PathValueType extends Record<string, any> ?
|
|
@@ -325,6 +319,5 @@ type ResolvePathType<
|
|
|
325
319
|
typeKey: TypeKey;
|
|
326
320
|
}
|
|
327
321
|
>
|
|
328
|
-
: unknown
|
|
329
|
-
TypeHint
|
|
330
|
-
>;
|
|
322
|
+
: unknown
|
|
323
|
+
: TypeHint;
|