mongoose 9.0.1 → 9.1.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/lib/aggregate.js +1 -1
- package/lib/cast/string.js +1 -1
- package/lib/cast.js +7 -15
- package/lib/collection.js +2 -2
- package/lib/connection.js +20 -14
- package/lib/cursor/changeStream.js +5 -5
- package/lib/document.js +126 -79
- package/lib/drivers/node-mongodb-native/collection.js +33 -126
- package/lib/drivers/node-mongodb-native/connection.js +8 -23
- package/lib/error/cast.js +1 -1
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +4 -4
- package/lib/helpers/clone.js +8 -8
- package/lib/helpers/common.js +4 -4
- package/lib/helpers/cursor/eachAsync.js +1 -1
- package/lib/helpers/discriminator/getConstructor.js +1 -1
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +2 -2
- package/lib/helpers/document/applyDefaults.js +1 -1
- package/lib/helpers/document/applyTimestamps.js +2 -1
- package/lib/helpers/document/applyVirtuals.js +4 -3
- package/lib/helpers/document/cleanModifiedSubpaths.js +1 -1
- package/lib/helpers/document/compile.js +4 -4
- package/lib/helpers/document/getDeepestSubdocumentForPath.js +1 -1
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
- package/lib/helpers/indexes/getRelatedIndexes.js +3 -3
- package/lib/helpers/model/castBulkWrite.js +5 -9
- package/lib/helpers/model/discriminator.js +1 -1
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -1
- package/lib/helpers/populate/assignVals.js +4 -4
- package/lib/helpers/populate/getModelsMapForPopulate.js +25 -23
- package/lib/helpers/populate/getSchemaTypes.js +6 -7
- package/lib/helpers/printJestWarning.js +1 -1
- package/lib/helpers/processConnectionOptions.js +1 -1
- package/lib/helpers/query/castUpdate.js +12 -12
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +2 -2
- package/lib/helpers/query/handleImmutable.js +2 -2
- package/lib/helpers/query/sanitizeFilter.js +1 -1
- package/lib/helpers/schema/applyPlugins.js +1 -1
- package/lib/helpers/schema/applyReadConcern.js +1 -1
- package/lib/helpers/schema/applyWriteConcern.js +4 -2
- package/lib/helpers/schema/getIndexes.js +3 -3
- package/lib/helpers/schema/getSubdocumentStrictValue.js +1 -1
- package/lib/helpers/schema/handleIdOption.js +1 -1
- package/lib/helpers/schema/idGetter.js +1 -1
- package/lib/helpers/schematype/handleImmutable.js +1 -1
- package/lib/helpers/setDefaultsOnInsert.js +2 -5
- package/lib/helpers/timestamps/setDocumentTimestamps.js +2 -2
- package/lib/helpers/timestamps/setupTimestamps.js +2 -2
- package/lib/helpers/update/applyTimestampsToUpdate.js +10 -9
- package/lib/helpers/update/castArrayFilters.js +4 -4
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +1 -1
- package/lib/helpers/updateValidators.js +4 -4
- package/lib/model.js +42 -48
- package/lib/mongoose.js +5 -5
- package/lib/options/virtualOptions.js +1 -1
- package/lib/plugins/saveSubdocs.js +2 -2
- package/lib/plugins/trackTransaction.js +3 -4
- package/lib/query.js +62 -59
- package/lib/queryHelpers.js +9 -12
- package/lib/schema/array.js +10 -12
- package/lib/schema/buffer.js +6 -6
- package/lib/schema/documentArray.js +15 -23
- package/lib/schema/documentArrayElement.js +3 -3
- package/lib/schema/map.js +1 -1
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +22 -4
- package/lib/schema/objectId.js +1 -1
- package/lib/schema/operators/exists.js +1 -1
- package/lib/schema/operators/geospatial.js +1 -1
- package/lib/schema/string.js +2 -2
- package/lib/schema/subdocument.js +9 -12
- package/lib/schema/union.js +1 -1
- package/lib/schema.js +27 -28
- package/lib/schemaType.js +11 -11
- package/lib/types/array/index.js +2 -2
- package/lib/types/array/methods/index.js +38 -8
- package/lib/types/arraySubdocument.js +12 -2
- package/lib/types/buffer.js +1 -1
- package/lib/types/documentArray/index.js +2 -2
- package/lib/types/documentArray/methods/index.js +5 -5
- package/lib/types/map.js +8 -8
- package/lib/types/subdocument.js +15 -5
- package/lib/utils.js +23 -7
- package/package.json +3 -2
- package/types/index.d.ts +26 -9
- package/types/inferrawdoctype.d.ts +9 -3
- package/types/inferschematype.d.ts +20 -27
- package/types/middlewares.d.ts +11 -0
- package/types/models.d.ts +15 -5
- package/types/query.d.ts +1 -1
- package/types/schemaoptions.d.ts +4 -2
- package/types/utility.d.ts +1 -1
- package/types/virtuals.d.ts +3 -3
package/lib/aggregate.js
CHANGED
package/lib/cast/string.js
CHANGED
package/lib/cast.js
CHANGED
|
@@ -42,7 +42,7 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
42
42
|
return obj;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
if (schema
|
|
45
|
+
if (schema?.discriminators != null && obj[schema.options.discriminatorKey] != null) {
|
|
46
46
|
schema = getSchemaDiscriminatorByValue(schema, obj[schema.options.discriminatorKey]) || schema;
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -79,7 +79,7 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
79
79
|
val[k] = cast(discriminatorSchema ? discriminatorSchema : schema, val[k], options, context);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
if (
|
|
82
|
+
if (utils.hasOwnKeys(val[k]) === false && beforeCastKeysLength !== 0) {
|
|
83
83
|
val.splice(k, 1);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
@@ -126,16 +126,12 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
126
126
|
const pathFirstHalf = split.slice(0, j).join('.');
|
|
127
127
|
const pathLastHalf = split.slice(j).join('.');
|
|
128
128
|
const _schematype = schema.path(pathFirstHalf);
|
|
129
|
-
const discriminatorKey = _schematype
|
|
130
|
-
_schematype.schema &&
|
|
131
|
-
_schematype.schema.options &&
|
|
132
|
-
_schematype.schema.options.discriminatorKey;
|
|
129
|
+
const discriminatorKey = _schematype?.schema?.options?.discriminatorKey;
|
|
133
130
|
|
|
134
131
|
// gh-6027: if we haven't found the schematype but this path is
|
|
135
132
|
// underneath an embedded discriminator and the embedded discriminator
|
|
136
133
|
// key is in the query, use the embedded discriminator schema
|
|
137
|
-
if (_schematype != null &&
|
|
138
|
-
(_schematype.schema && _schematype.schema.discriminators) != null &&
|
|
134
|
+
if (_schematype?.schema?.discriminators != null &&
|
|
139
135
|
discriminatorKey != null &&
|
|
140
136
|
pathLastHalf !== discriminatorKey) {
|
|
141
137
|
const discriminatorVal = get(obj, pathFirstHalf + '.' + discriminatorKey);
|
|
@@ -330,9 +326,9 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
330
326
|
$cond = ks[k];
|
|
331
327
|
nested = val[$cond];
|
|
332
328
|
if ($cond === '$elemMatch') {
|
|
333
|
-
if (nested && schematype
|
|
329
|
+
if (nested && schematype?.schema != null) {
|
|
334
330
|
cast(schematype.schema, nested, options, context);
|
|
335
|
-
} else if (nested && schematype
|
|
331
|
+
} else if (nested && schematype?.$isMongooseArray) {
|
|
336
332
|
if (utils.isPOJO(nested) && nested.$not != null) {
|
|
337
333
|
cast(schema, nested, options, context);
|
|
338
334
|
} else {
|
|
@@ -430,11 +426,7 @@ function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions,
|
|
|
430
426
|
if ('strictQuery' in schemaUserProvidedOptions) {
|
|
431
427
|
return schemaUserProvidedOptions.strictQuery;
|
|
432
428
|
}
|
|
433
|
-
const mongooseOptions = context
|
|
434
|
-
context.mongooseCollection &&
|
|
435
|
-
context.mongooseCollection.conn &&
|
|
436
|
-
context.mongooseCollection.conn.base &&
|
|
437
|
-
context.mongooseCollection.conn.base.options;
|
|
429
|
+
const mongooseOptions = context?.mongooseCollection?.conn?.base?.options;
|
|
438
430
|
if (mongooseOptions) {
|
|
439
431
|
if ('strictQuery' in mongooseOptions) {
|
|
440
432
|
return mongooseOptions.strictQuery;
|
package/lib/collection.js
CHANGED
|
@@ -290,7 +290,7 @@ Collection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
|
|
|
290
290
|
if (opts.bufferCommands != null) {
|
|
291
291
|
return opts.bufferCommands;
|
|
292
292
|
}
|
|
293
|
-
if (opts
|
|
293
|
+
if (opts?.schemaUserProvidedOptions?.bufferCommands != null) {
|
|
294
294
|
return opts.schemaUserProvidedOptions.bufferCommands;
|
|
295
295
|
}
|
|
296
296
|
|
|
@@ -308,7 +308,7 @@ Collection.prototype._getBufferTimeoutMS = function _getBufferTimeoutMS() {
|
|
|
308
308
|
if (opts.bufferTimeoutMS != null) {
|
|
309
309
|
return opts.bufferTimeoutMS;
|
|
310
310
|
}
|
|
311
|
-
if (opts
|
|
311
|
+
if (opts?.schemaUserProvidedOptions?.bufferTimeoutMS != null) {
|
|
312
312
|
return opts.schemaUserProvidedOptions.bufferTimeoutMS;
|
|
313
313
|
}
|
|
314
314
|
return conn._getBufferTimeoutMS();
|
package/lib/connection.js
CHANGED
|
@@ -630,7 +630,7 @@ Connection.prototype.createCollections = async function createCollections(option
|
|
|
630
630
|
}
|
|
631
631
|
}
|
|
632
632
|
|
|
633
|
-
if (!continueOnError &&
|
|
633
|
+
if (!continueOnError && utils.hasOwnKeys(errorsMap)) {
|
|
634
634
|
const message = Object.entries(errorsMap).map(([modelName, err]) => `${modelName}: ${err.message}`).join(', ');
|
|
635
635
|
const createCollectionsError = new CreateCollectionsError(message, errorsMap);
|
|
636
636
|
throw createCollectionsError;
|
|
@@ -883,7 +883,7 @@ Connection.prototype._getBufferTimeoutMS = function _getBufferTimeoutMS() {
|
|
|
883
883
|
if (this.config.bufferTimeoutMS != null) {
|
|
884
884
|
return this.config.bufferTimeoutMS;
|
|
885
885
|
}
|
|
886
|
-
if (this.base
|
|
886
|
+
if (this.base?.get('bufferTimeoutMS') != null) {
|
|
887
887
|
return this.base.get('bufferTimeoutMS');
|
|
888
888
|
}
|
|
889
889
|
return 10000;
|
|
@@ -1250,8 +1250,11 @@ Connection.prototype._close = async function _close(force, destroy) {
|
|
|
1250
1250
|
const conn = this;
|
|
1251
1251
|
switch (this.readyState) {
|
|
1252
1252
|
case STATES.disconnected:
|
|
1253
|
-
if (destroy
|
|
1254
|
-
this.base.connections.
|
|
1253
|
+
if (destroy) {
|
|
1254
|
+
const index = this.base.connections.indexOf(conn);
|
|
1255
|
+
if (index !== -1) {
|
|
1256
|
+
this.base.connections.splice(index, 1);
|
|
1257
|
+
}
|
|
1255
1258
|
}
|
|
1256
1259
|
if (!closeCalled) {
|
|
1257
1260
|
await this.doClose(force);
|
|
@@ -1262,8 +1265,11 @@ Connection.prototype._close = async function _close(force, destroy) {
|
|
|
1262
1265
|
case STATES.connected:
|
|
1263
1266
|
this.readyState = STATES.disconnecting;
|
|
1264
1267
|
await this.doClose(force);
|
|
1265
|
-
if (destroy
|
|
1266
|
-
|
|
1268
|
+
if (destroy) {
|
|
1269
|
+
const index = _this.base.connections.indexOf(conn);
|
|
1270
|
+
if (index !== -1) {
|
|
1271
|
+
this.base.connections.splice(index, 1);
|
|
1272
|
+
}
|
|
1267
1273
|
}
|
|
1268
1274
|
this.onClose(force);
|
|
1269
1275
|
|
|
@@ -1349,13 +1355,13 @@ Connection.prototype.onClose = function onClose(force) {
|
|
|
1349
1355
|
|
|
1350
1356
|
Connection.prototype.collection = function(name, options) {
|
|
1351
1357
|
const defaultOptions = {
|
|
1352
|
-
autoIndex: this.config.autoIndex
|
|
1353
|
-
autoCreate: this.config.autoCreate
|
|
1354
|
-
autoSearchIndex: this.config.autoSearchIndex
|
|
1358
|
+
autoIndex: this.config.autoIndex ?? this.base.options.autoIndex,
|
|
1359
|
+
autoCreate: this.config.autoCreate ?? this.base.options.autoCreate,
|
|
1360
|
+
autoSearchIndex: this.config.autoSearchIndex ?? this.base.options.autoSearchIndex
|
|
1355
1361
|
};
|
|
1356
1362
|
options = Object.assign({}, defaultOptions, options ? clone(options) : {});
|
|
1357
1363
|
options.$wasForceClosed = this.$wasForceClosed;
|
|
1358
|
-
const Collection = this.base
|
|
1364
|
+
const Collection = this.base?.__driver?.Collection || driver.get().Collection;
|
|
1359
1365
|
if (!(name in this.collections)) {
|
|
1360
1366
|
this.collections[name] = new Collection(name, this, options);
|
|
1361
1367
|
}
|
|
@@ -1456,7 +1462,7 @@ Connection.prototype.model = function model(name, schema, collection, options) {
|
|
|
1456
1462
|
const opts = Object.assign(defaultOptions, options, { connection: this });
|
|
1457
1463
|
if (this.models[name] && !collection && opts.overwriteModels !== true) {
|
|
1458
1464
|
// model exists but we are not subclassing with custom collection
|
|
1459
|
-
if (schema
|
|
1465
|
+
if (schema?.instanceOfSchema && schema !== this.models[name].schema) {
|
|
1460
1466
|
throw new MongooseError.OverwriteModelError(name);
|
|
1461
1467
|
}
|
|
1462
1468
|
return this.models[name];
|
|
@@ -1464,7 +1470,7 @@ Connection.prototype.model = function model(name, schema, collection, options) {
|
|
|
1464
1470
|
|
|
1465
1471
|
let model;
|
|
1466
1472
|
|
|
1467
|
-
if (schema
|
|
1473
|
+
if (schema?.instanceOfSchema) {
|
|
1468
1474
|
applyPlugins(schema, this.plugins, null, '$connectionPluginsApplied');
|
|
1469
1475
|
|
|
1470
1476
|
// compile a model
|
|
@@ -1672,7 +1678,7 @@ Connection.prototype.shouldAuthenticate = function shouldAuthenticate() {
|
|
|
1672
1678
|
* a password, otherwise false.
|
|
1673
1679
|
*/
|
|
1674
1680
|
Connection.prototype.authMechanismDoesNotRequirePassword = function authMechanismDoesNotRequirePassword() {
|
|
1675
|
-
if (this.options
|
|
1681
|
+
if (this.options?.auth) {
|
|
1676
1682
|
return noPasswordAuthMechanisms.indexOf(this.options.auth.authMechanism) >= 0;
|
|
1677
1683
|
}
|
|
1678
1684
|
return true;
|
|
@@ -1772,7 +1778,7 @@ Connection.prototype.syncIndexes = async function syncIndexes(options = {}) {
|
|
|
1772
1778
|
}
|
|
1773
1779
|
}
|
|
1774
1780
|
|
|
1775
|
-
if (!continueOnError &&
|
|
1781
|
+
if (!continueOnError && utils.hasOwnKeys(errorsMap)) {
|
|
1776
1782
|
const message = Object.entries(errorsMap).map(([modelName, err]) => `${modelName}: ${err.message}`).join(', ');
|
|
1777
1783
|
const syncIndexesError = new SyncIndexesError(message, errorsMap);
|
|
1778
1784
|
throw syncIndexesError;
|
|
@@ -28,7 +28,7 @@ class ChangeStream extends EventEmitter {
|
|
|
28
28
|
this.options = options;
|
|
29
29
|
this.errored = false;
|
|
30
30
|
|
|
31
|
-
if (options
|
|
31
|
+
if (options?.hydrate && !options.model) {
|
|
32
32
|
throw new Error(
|
|
33
33
|
'Cannot create change stream with `hydrate: true` ' +
|
|
34
34
|
'unless calling `Model.watch()`'
|
|
@@ -83,7 +83,7 @@ class ChangeStream extends EventEmitter {
|
|
|
83
83
|
|
|
84
84
|
driverChangeStreamEvents.forEach(ev => {
|
|
85
85
|
this.driverChangeStream.on(ev, data => {
|
|
86
|
-
if (data
|
|
86
|
+
if (data?.fullDocument != null && this.options?.hydrate) {
|
|
87
87
|
data.fullDocument = this.options.model.hydrate(data.fullDocument);
|
|
88
88
|
}
|
|
89
89
|
this.emit(ev, data);
|
|
@@ -102,7 +102,7 @@ class ChangeStream extends EventEmitter {
|
|
|
102
102
|
|
|
103
103
|
driverChangeStreamEvents.forEach(ev => {
|
|
104
104
|
this.driverChangeStream.on(ev, data => {
|
|
105
|
-
if (data
|
|
105
|
+
if (data?.fullDocument != null && this.options?.hydrate) {
|
|
106
106
|
data.fullDocument = this.options.model.hydrate(data.fullDocument);
|
|
107
107
|
}
|
|
108
108
|
this.emit(ev, data);
|
|
@@ -121,7 +121,7 @@ class ChangeStream extends EventEmitter {
|
|
|
121
121
|
if (this.errored) {
|
|
122
122
|
throw new MongooseError('Cannot call next() on errored ChangeStream');
|
|
123
123
|
}
|
|
124
|
-
if (this.options
|
|
124
|
+
if (this.options?.hydrate) {
|
|
125
125
|
if (cb != null) {
|
|
126
126
|
const originalCb = cb;
|
|
127
127
|
cb = (err, data) => {
|
|
@@ -136,7 +136,7 @@ class ChangeStream extends EventEmitter {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
let maybePromise = this.driverChangeStream.next(cb);
|
|
139
|
-
if (
|
|
139
|
+
if (typeof maybePromise?.then === 'function') {
|
|
140
140
|
maybePromise = maybePromise.then(data => {
|
|
141
141
|
if (data.fullDocument != null) {
|
|
142
142
|
data.fullDocument = this.options.model.hydrate(data.fullDocument);
|