mongoose 8.9.0 → 8.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/dist/browser.umd.js +1 -1
- package/index.js +0 -1
- package/lib/connection.js +40 -18
- package/lib/cursor/aggregationCursor.js +1 -2
- package/lib/document.js +7 -4
- package/lib/drivers/node-mongodb-native/collection.js +0 -1
- package/lib/helpers/model/discriminator.js +1 -0
- package/lib/model.js +7 -8
- package/lib/mongoose.js +18 -20
- package/lib/query.js +0 -18
- package/lib/schema/string.js +10 -10
- package/lib/schema.js +25 -7
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -46,7 +46,6 @@ module.exports.Decimal128 = mongoose.Decimal128;
|
|
|
46
46
|
module.exports.Mixed = mongoose.Mixed;
|
|
47
47
|
module.exports.Date = mongoose.Date;
|
|
48
48
|
module.exports.Number = mongoose.Number;
|
|
49
|
-
module.exports.Double = mongoose.Double;
|
|
50
49
|
module.exports.Error = mongoose.Error;
|
|
51
50
|
module.exports.MongooseError = mongoose.MongooseError;
|
|
52
51
|
module.exports.now = mongoose.now;
|
package/lib/connection.js
CHANGED
|
@@ -158,7 +158,7 @@ Object.defineProperty(Connection.prototype, 'readyState', {
|
|
|
158
158
|
* @api public
|
|
159
159
|
*/
|
|
160
160
|
|
|
161
|
-
Connection.prototype.get = function(key) {
|
|
161
|
+
Connection.prototype.get = function getOption(key) {
|
|
162
162
|
if (this.config.hasOwnProperty(key)) {
|
|
163
163
|
return this.config[key];
|
|
164
164
|
}
|
|
@@ -186,7 +186,7 @@ Connection.prototype.get = function(key) {
|
|
|
186
186
|
* @api public
|
|
187
187
|
*/
|
|
188
188
|
|
|
189
|
-
Connection.prototype.set = function(key, val) {
|
|
189
|
+
Connection.prototype.set = function setOption(key, val) {
|
|
190
190
|
if (this.config.hasOwnProperty(key)) {
|
|
191
191
|
this.config[key] = val;
|
|
192
192
|
return val;
|
|
@@ -925,7 +925,7 @@ Connection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
|
|
|
925
925
|
* @api private
|
|
926
926
|
*/
|
|
927
927
|
|
|
928
|
-
Connection.prototype.error = function(err, callback) {
|
|
928
|
+
Connection.prototype.error = function error(err, callback) {
|
|
929
929
|
if (callback) {
|
|
930
930
|
callback(err);
|
|
931
931
|
return null;
|
|
@@ -939,6 +939,7 @@ Connection.prototype.error = function(err, callback) {
|
|
|
939
939
|
/**
|
|
940
940
|
* Called when the connection is opened
|
|
941
941
|
*
|
|
942
|
+
* @emits "open"
|
|
942
943
|
* @api private
|
|
943
944
|
*/
|
|
944
945
|
|
|
@@ -1043,11 +1044,21 @@ Connection.prototype.openUri = async function openUri(uri, options) {
|
|
|
1043
1044
|
return this;
|
|
1044
1045
|
};
|
|
1045
1046
|
|
|
1046
|
-
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1047
|
+
/**
|
|
1048
|
+
* Listen to events in the Connection
|
|
1049
|
+
*
|
|
1050
|
+
* @param {String} event The event to listen on
|
|
1051
|
+
* @param {Function} callback
|
|
1052
|
+
* @see Connection#readyState https://mongoosejs.com/docs/api/connection.html#Connection.prototype.readyState
|
|
1053
|
+
*
|
|
1054
|
+
* @method on
|
|
1055
|
+
* @instance
|
|
1056
|
+
* @memberOf Connection
|
|
1057
|
+
* @api public
|
|
1049
1058
|
*/
|
|
1050
1059
|
|
|
1060
|
+
// Treat `on('error')` handlers as handling the initialConnection promise
|
|
1061
|
+
// to avoid uncaught exceptions when using `on('error')`. See gh-14377.
|
|
1051
1062
|
Connection.prototype.on = function on(event, callback) {
|
|
1052
1063
|
if (event === 'error' && this.$initialConnection) {
|
|
1053
1064
|
this.$initialConnection.catch(() => {});
|
|
@@ -1055,11 +1066,21 @@ Connection.prototype.on = function on(event, callback) {
|
|
|
1055
1066
|
return EventEmitter.prototype.on.call(this, event, callback);
|
|
1056
1067
|
};
|
|
1057
1068
|
|
|
1058
|
-
|
|
1059
|
-
*
|
|
1060
|
-
*
|
|
1069
|
+
/**
|
|
1070
|
+
* Listen to a event once in the Connection
|
|
1071
|
+
*
|
|
1072
|
+
* @param {String} event The event to listen on
|
|
1073
|
+
* @param {Function} callback
|
|
1074
|
+
* @see Connection#readyState https://mongoosejs.com/docs/api/connection.html#Connection.prototype.readyState
|
|
1075
|
+
*
|
|
1076
|
+
* @method once
|
|
1077
|
+
* @instance
|
|
1078
|
+
* @memberOf Connection
|
|
1079
|
+
* @api public
|
|
1061
1080
|
*/
|
|
1062
1081
|
|
|
1082
|
+
// Treat `on('error')` handlers as handling the initialConnection promise
|
|
1083
|
+
// to avoid uncaught exceptions when using `on('error')`. See gh-14377.
|
|
1063
1084
|
Connection.prototype.once = function on(event, callback) {
|
|
1064
1085
|
if (event === 'error' && this.$initialConnection) {
|
|
1065
1086
|
this.$initialConnection.catch(() => {});
|
|
@@ -1220,17 +1241,18 @@ Connection.prototype._close = async function _close(force, destroy) {
|
|
|
1220
1241
|
* @api private
|
|
1221
1242
|
*/
|
|
1222
1243
|
|
|
1223
|
-
Connection.prototype.doClose = function() {
|
|
1244
|
+
Connection.prototype.doClose = function doClose() {
|
|
1224
1245
|
throw new Error('Connection#doClose unimplemented by driver');
|
|
1225
1246
|
};
|
|
1226
1247
|
|
|
1227
1248
|
/**
|
|
1228
1249
|
* Called when the connection closes
|
|
1229
1250
|
*
|
|
1251
|
+
* @emits "close"
|
|
1230
1252
|
* @api private
|
|
1231
1253
|
*/
|
|
1232
1254
|
|
|
1233
|
-
Connection.prototype.onClose = function(force) {
|
|
1255
|
+
Connection.prototype.onClose = function onClose(force) {
|
|
1234
1256
|
this.readyState = STATES.disconnected;
|
|
1235
1257
|
|
|
1236
1258
|
// avoid having the collection subscribe to our event emitter
|
|
@@ -1334,7 +1356,7 @@ Connection.prototype.plugin = function(fn, opts) {
|
|
|
1334
1356
|
* @api public
|
|
1335
1357
|
*/
|
|
1336
1358
|
|
|
1337
|
-
Connection.prototype.model = function(name, schema, collection, options) {
|
|
1359
|
+
Connection.prototype.model = function model(name, schema, collection, options) {
|
|
1338
1360
|
if (!(this instanceof Connection)) {
|
|
1339
1361
|
throw new MongooseError('`connection.model()` should not be run with ' +
|
|
1340
1362
|
'`new`. If you are doing `new db.model(foo)(bar)`, use ' +
|
|
@@ -1454,7 +1476,7 @@ Connection.prototype.model = function(name, schema, collection, options) {
|
|
|
1454
1476
|
* @return {Connection} this
|
|
1455
1477
|
*/
|
|
1456
1478
|
|
|
1457
|
-
Connection.prototype.deleteModel = function(name) {
|
|
1479
|
+
Connection.prototype.deleteModel = function deleteModel(name) {
|
|
1458
1480
|
if (typeof name === 'string') {
|
|
1459
1481
|
const model = this.model(name);
|
|
1460
1482
|
if (model == null) {
|
|
@@ -1510,7 +1532,7 @@ Connection.prototype.deleteModel = function(name) {
|
|
|
1510
1532
|
* @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
|
|
1511
1533
|
*/
|
|
1512
1534
|
|
|
1513
|
-
Connection.prototype.watch = function(pipeline, options) {
|
|
1535
|
+
Connection.prototype.watch = function watch(pipeline, options) {
|
|
1514
1536
|
const changeStreamThunk = cb => {
|
|
1515
1537
|
immediate(() => {
|
|
1516
1538
|
if (this.readyState === STATES.connecting) {
|
|
@@ -1559,7 +1581,7 @@ Connection.prototype.asPromise = async function asPromise() {
|
|
|
1559
1581
|
* @return {String[]}
|
|
1560
1582
|
*/
|
|
1561
1583
|
|
|
1562
|
-
Connection.prototype.modelNames = function() {
|
|
1584
|
+
Connection.prototype.modelNames = function modelNames() {
|
|
1563
1585
|
return Object.keys(this.models);
|
|
1564
1586
|
};
|
|
1565
1587
|
|
|
@@ -1571,7 +1593,7 @@ Connection.prototype.modelNames = function() {
|
|
|
1571
1593
|
* @api private
|
|
1572
1594
|
* @return {Boolean} true if the connection should be authenticated after it is opened, otherwise false.
|
|
1573
1595
|
*/
|
|
1574
|
-
Connection.prototype.shouldAuthenticate = function() {
|
|
1596
|
+
Connection.prototype.shouldAuthenticate = function shouldAuthenticate() {
|
|
1575
1597
|
return this.user != null &&
|
|
1576
1598
|
(this.pass != null || this.authMechanismDoesNotRequirePassword());
|
|
1577
1599
|
};
|
|
@@ -1584,7 +1606,7 @@ Connection.prototype.shouldAuthenticate = function() {
|
|
|
1584
1606
|
* @return {Boolean} true if the authentication mechanism specified in the options object requires
|
|
1585
1607
|
* a password, otherwise false.
|
|
1586
1608
|
*/
|
|
1587
|
-
Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
|
1609
|
+
Connection.prototype.authMechanismDoesNotRequirePassword = function authMechanismDoesNotRequirePassword() {
|
|
1588
1610
|
if (this.options && this.options.auth) {
|
|
1589
1611
|
return noPasswordAuthMechanisms.indexOf(this.options.auth.authMechanism) >= 0;
|
|
1590
1612
|
}
|
|
@@ -1602,7 +1624,7 @@ Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
|
|
1602
1624
|
* @return {Boolean} true if the provided options object provides enough data to authenticate with,
|
|
1603
1625
|
* otherwise false.
|
|
1604
1626
|
*/
|
|
1605
|
-
Connection.prototype.optionsProvideAuthenticationData = function(options) {
|
|
1627
|
+
Connection.prototype.optionsProvideAuthenticationData = function optionsProvideAuthenticationData(options) {
|
|
1606
1628
|
return (options) &&
|
|
1607
1629
|
(options.user) &&
|
|
1608
1630
|
((options.pass) || this.authMechanismDoesNotRequirePassword());
|
|
@@ -175,11 +175,10 @@ AggregationCursor.prototype._markError = function(error) {
|
|
|
175
175
|
* Marks this cursor as closed. Will stop streaming and subsequent calls to
|
|
176
176
|
* `next()` will error.
|
|
177
177
|
*
|
|
178
|
-
* @param {Function} callback
|
|
179
178
|
* @return {Promise}
|
|
180
179
|
* @api public
|
|
181
180
|
* @method close
|
|
182
|
-
* @emits close
|
|
181
|
+
* @emits "close"
|
|
183
182
|
* @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
|
|
184
183
|
*/
|
|
185
184
|
|
package/lib/document.js
CHANGED
|
@@ -831,7 +831,7 @@ function init(self, obj, doc, opts, prefix) {
|
|
|
831
831
|
*
|
|
832
832
|
* #### Example:
|
|
833
833
|
*
|
|
834
|
-
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 }
|
|
834
|
+
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 });
|
|
835
835
|
*
|
|
836
836
|
* #### Valid options:
|
|
837
837
|
*
|
|
@@ -843,7 +843,6 @@ function init(self, obj, doc, opts, prefix) {
|
|
|
843
843
|
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.lean()) and the [Mongoose lean tutorial](https://mongoosejs.com/docs/tutorials/lean.html).
|
|
844
844
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
845
845
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
|
|
846
|
-
* @param {Function} [callback]
|
|
847
846
|
* @return {Query}
|
|
848
847
|
* @api public
|
|
849
848
|
* @memberOf Document
|
|
@@ -1777,6 +1776,11 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
|
|
|
1777
1776
|
*/
|
|
1778
1777
|
|
|
1779
1778
|
Document.prototype.$__getValue = function(path) {
|
|
1779
|
+
if (typeof path !== 'string' && !Array.isArray(path)) {
|
|
1780
|
+
throw new TypeError(
|
|
1781
|
+
`Invalid \`path\`. Must be either string or array. Got "${path}" (type ${typeof path})`
|
|
1782
|
+
);
|
|
1783
|
+
}
|
|
1780
1784
|
return utils.getValue(path, this._doc);
|
|
1781
1785
|
};
|
|
1782
1786
|
|
|
@@ -3439,12 +3443,11 @@ function _checkImmutableSubpaths(subdoc, schematype, priorVal) {
|
|
|
3439
3443
|
* @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](https://mongoosejs.com/docs/guide.html#writeConcern).
|
|
3440
3444
|
* @param {Boolean} [options.checkKeys=true] the MongoDB driver prevents you from saving keys that start with '$' or contain '.' by default. Set this option to `false` to skip that check. See [restrictions on field names](https://www.mongodb.com/docs/manual/reference/limits/#Restrictions-on-Field-Names)
|
|
3441
3445
|
* @param {Boolean} [options.timestamps=true] if `false` and [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this `save()`.
|
|
3442
|
-
* @param {Function} [fn] optional callback
|
|
3443
3446
|
* @method save
|
|
3444
3447
|
* @memberOf Document
|
|
3445
3448
|
* @instance
|
|
3446
3449
|
* @throws {DocumentNotFoundError} if this [save updates an existing document](https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew()) but the document doesn't exist in the database. For example, you will get this error if the document is [deleted between when you retrieved the document and when you saved it](documents.html#updating).
|
|
3447
|
-
* @return {Promise
|
|
3450
|
+
* @return {Promise}
|
|
3448
3451
|
* @api public
|
|
3449
3452
|
* @see middleware https://mongoosejs.com/docs/middleware.html
|
|
3450
3453
|
*/
|
|
@@ -119,6 +119,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
119
119
|
// schema. `Schema.prototype.clone()` copies `obj` by reference, no cloning.
|
|
120
120
|
schema.obj = { ...schema.obj };
|
|
121
121
|
mergeDiscriminatorSchema(schema, baseSchema);
|
|
122
|
+
schema._gatherChildSchemas();
|
|
122
123
|
|
|
123
124
|
// Clean up conflicting paths _after_ merging re: gh-6076
|
|
124
125
|
for (const conflictingPath of conflictingPaths) {
|
package/lib/model.js
CHANGED
|
@@ -2231,7 +2231,7 @@ Model.$where = function $where() {
|
|
|
2231
2231
|
/**
|
|
2232
2232
|
* Issues a mongodb findOneAndUpdate command.
|
|
2233
2233
|
*
|
|
2234
|
-
* Finds a matching document, updates it according to the `update` arg, passing any `options
|
|
2234
|
+
* Finds a matching document, updates it according to the `update` arg, passing any `options`. A Query object is returned.
|
|
2235
2235
|
*
|
|
2236
2236
|
* #### Example:
|
|
2237
2237
|
*
|
|
@@ -3643,7 +3643,11 @@ Model.castObject = function castObject(obj, options) {
|
|
|
3643
3643
|
options = options || {};
|
|
3644
3644
|
const ret = {};
|
|
3645
3645
|
|
|
3646
|
-
|
|
3646
|
+
let schema = this.schema;
|
|
3647
|
+
const discriminatorKey = schema.options.discriminatorKey;
|
|
3648
|
+
if (schema.discriminators != null && obj != null && obj[discriminatorKey] != null) {
|
|
3649
|
+
schema = getSchemaDiscriminatorByValue(schema, obj[discriminatorKey]) || schema;
|
|
3650
|
+
}
|
|
3647
3651
|
const paths = Object.keys(schema.paths);
|
|
3648
3652
|
|
|
3649
3653
|
for (const path of paths) {
|
|
@@ -3992,7 +3996,7 @@ function _update(model, op, conditions, doc, options) {
|
|
|
3992
3996
|
/**
|
|
3993
3997
|
* Performs [aggregations](https://www.mongodb.com/docs/manual/aggregation/) on the models collection.
|
|
3994
3998
|
*
|
|
3995
|
-
*
|
|
3999
|
+
* The `aggregate` itself is returned.
|
|
3996
4000
|
*
|
|
3997
4001
|
* This function triggers the following middleware.
|
|
3998
4002
|
*
|
|
@@ -4047,10 +4051,6 @@ Model.aggregate = function aggregate(pipeline, options) {
|
|
|
4047
4051
|
aggregate.option(options);
|
|
4048
4052
|
}
|
|
4049
4053
|
|
|
4050
|
-
if (typeof callback === 'undefined') {
|
|
4051
|
-
return aggregate;
|
|
4052
|
-
}
|
|
4053
|
-
|
|
4054
4054
|
return aggregate;
|
|
4055
4055
|
};
|
|
4056
4056
|
|
|
@@ -4238,7 +4238,6 @@ Model.validate = async function validate(obj, pathsOrOptions, context) {
|
|
|
4238
4238
|
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
|
|
4239
4239
|
* @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
|
|
4240
4240
|
* @param {Boolean} [options.forceRepopulate=true] Set to `false` to prevent Mongoose from repopulating paths that are already populated
|
|
4241
|
-
* @param {Function} [callback(err,doc)] Optional callback, executed upon completion. Receives `err` and the `doc(s)`.
|
|
4242
4241
|
* @return {Promise}
|
|
4243
4242
|
* @api public
|
|
4244
4243
|
*/
|
package/lib/mongoose.js
CHANGED
|
@@ -245,7 +245,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
|
|
|
245
245
|
* @api public
|
|
246
246
|
*/
|
|
247
247
|
|
|
248
|
-
Mongoose.prototype.set = function(key, value) {
|
|
248
|
+
Mongoose.prototype.set = function getsetOptions(key, value) {
|
|
249
249
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
250
250
|
|
|
251
251
|
if (arguments.length === 1 && typeof key !== 'object') {
|
|
@@ -376,7 +376,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
|
|
376
376
|
* @api public
|
|
377
377
|
*/
|
|
378
378
|
|
|
379
|
-
Mongoose.prototype.createConnection = function(uri, options) {
|
|
379
|
+
Mongoose.prototype.createConnection = function createConnection(uri, options) {
|
|
380
380
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
381
381
|
|
|
382
382
|
const Connection = _mongoose.__driver.Connection;
|
|
@@ -427,7 +427,6 @@ Mongoose.prototype.createConnection = function(uri, options) {
|
|
|
427
427
|
* @param {Number} [options.socketTimeoutMS=0] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. A socket may be inactive because of either no activity or a long-running operation. `socketTimeoutMS` defaults to 0, which means Node.js will not time out the socket due to inactivity. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
|
|
428
428
|
* @param {Number} [options.family=0] Passed transparently to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. May be either `0`, `4`, or `6`. `4` means use IPv4 only, `6` means use IPv6 only, `0` means try both.
|
|
429
429
|
* @param {Boolean} [options.autoCreate=false] Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection.
|
|
430
|
-
* @param {Function} [callback]
|
|
431
430
|
* @see Mongoose#createConnection https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.createConnection()
|
|
432
431
|
* @api public
|
|
433
432
|
* @return {Promise} resolves to `this` if connection succeeded
|
|
@@ -479,12 +478,11 @@ Mongoose.prototype.disconnect = async function disconnect() {
|
|
|
479
478
|
*
|
|
480
479
|
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#startSession)
|
|
481
480
|
* @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
|
482
|
-
* @param {Function} [callback]
|
|
483
481
|
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
|
484
482
|
* @api public
|
|
485
483
|
*/
|
|
486
484
|
|
|
487
|
-
Mongoose.prototype.startSession = function() {
|
|
485
|
+
Mongoose.prototype.startSession = function startSession() {
|
|
488
486
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
489
487
|
|
|
490
488
|
return _mongoose.connection.startSession.apply(_mongoose.connection, arguments);
|
|
@@ -498,7 +496,7 @@ Mongoose.prototype.startSession = function() {
|
|
|
498
496
|
* @api public
|
|
499
497
|
*/
|
|
500
498
|
|
|
501
|
-
Mongoose.prototype.pluralize = function(fn) {
|
|
499
|
+
Mongoose.prototype.pluralize = function pluralize(fn) {
|
|
502
500
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
503
501
|
|
|
504
502
|
if (arguments.length > 0) {
|
|
@@ -561,7 +559,7 @@ Mongoose.prototype.pluralize = function(fn) {
|
|
|
561
559
|
* @api public
|
|
562
560
|
*/
|
|
563
561
|
|
|
564
|
-
Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
562
|
+
Mongoose.prototype.model = function model(name, schema, collection, options) {
|
|
565
563
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
566
564
|
|
|
567
565
|
if (typeof schema === 'string') {
|
|
@@ -572,7 +570,7 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
|
572
570
|
if (arguments.length === 1) {
|
|
573
571
|
const model = _mongoose.models[name];
|
|
574
572
|
if (!model) {
|
|
575
|
-
throw new
|
|
573
|
+
throw new _mongoose.Error.MissingSchemaError(name);
|
|
576
574
|
}
|
|
577
575
|
return model;
|
|
578
576
|
}
|
|
@@ -581,7 +579,7 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
|
581
579
|
schema = new Schema(schema);
|
|
582
580
|
}
|
|
583
581
|
if (schema && !(schema instanceof Schema)) {
|
|
584
|
-
throw new Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
|
582
|
+
throw new _mongoose.Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
|
585
583
|
'schema or a POJO');
|
|
586
584
|
}
|
|
587
585
|
|
|
@@ -632,7 +630,7 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
|
|
|
632
630
|
* ignore
|
|
633
631
|
*/
|
|
634
632
|
|
|
635
|
-
Mongoose.prototype._model = function(name, schema, collection, options) {
|
|
633
|
+
Mongoose.prototype._model = function _model(name, schema, collection, options) {
|
|
636
634
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
637
635
|
|
|
638
636
|
let model;
|
|
@@ -707,7 +705,7 @@ Mongoose.prototype._model = function(name, schema, collection, options) {
|
|
|
707
705
|
* @return {Mongoose} this
|
|
708
706
|
*/
|
|
709
707
|
|
|
710
|
-
Mongoose.prototype.deleteModel = function(name) {
|
|
708
|
+
Mongoose.prototype.deleteModel = function deleteModel(name) {
|
|
711
709
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
712
710
|
|
|
713
711
|
_mongoose.connection.deleteModel(name);
|
|
@@ -726,7 +724,7 @@ Mongoose.prototype.deleteModel = function(name) {
|
|
|
726
724
|
* @return {Array}
|
|
727
725
|
*/
|
|
728
726
|
|
|
729
|
-
Mongoose.prototype.modelNames = function() {
|
|
727
|
+
Mongoose.prototype.modelNames = function modelNames() {
|
|
730
728
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
731
729
|
|
|
732
730
|
const names = Object.keys(_mongoose.models);
|
|
@@ -740,7 +738,7 @@ Mongoose.prototype.modelNames = function() {
|
|
|
740
738
|
* @api private
|
|
741
739
|
*/
|
|
742
740
|
|
|
743
|
-
Mongoose.prototype._applyPlugins = function(schema, options) {
|
|
741
|
+
Mongoose.prototype._applyPlugins = function _applyPlugins(schema, options) {
|
|
744
742
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
745
743
|
|
|
746
744
|
options = options || {};
|
|
@@ -763,7 +761,7 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
|
|
|
763
761
|
* @api public
|
|
764
762
|
*/
|
|
765
763
|
|
|
766
|
-
Mongoose.prototype.plugin = function(fn, opts) {
|
|
764
|
+
Mongoose.prototype.plugin = function plugin(fn, opts) {
|
|
767
765
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
768
766
|
|
|
769
767
|
_mongoose.plugins.push([fn, opts]);
|
|
@@ -1073,7 +1071,7 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
|
|
|
1073
1071
|
* @api public
|
|
1074
1072
|
*/
|
|
1075
1073
|
|
|
1076
|
-
Mongoose.prototype.isValidObjectId = function(v) {
|
|
1074
|
+
Mongoose.prototype.isValidObjectId = function isValidObjectId(v) {
|
|
1077
1075
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
1078
1076
|
return _mongoose.Types.ObjectId.isValid(v);
|
|
1079
1077
|
};
|
|
@@ -1105,7 +1103,7 @@ Mongoose.prototype.isValidObjectId = function(v) {
|
|
|
1105
1103
|
* @api public
|
|
1106
1104
|
*/
|
|
1107
1105
|
|
|
1108
|
-
Mongoose.prototype.isObjectIdOrHexString = function(v) {
|
|
1106
|
+
Mongoose.prototype.isObjectIdOrHexString = function isObjectIdOrHexString(v) {
|
|
1109
1107
|
return isBsonType(v, 'ObjectId') || (typeof v === 'string' && objectIdHexRegexp.test(v));
|
|
1110
1108
|
};
|
|
1111
1109
|
|
|
@@ -1117,7 +1115,7 @@ Mongoose.prototype.isObjectIdOrHexString = function(v) {
|
|
|
1117
1115
|
* @param {Boolean} options.continueOnError `false` by default. If set to `true`, mongoose will not throw an error if one model syncing failed, and will return an object where the keys are the names of the models, and the values are the results/errors for each model.
|
|
1118
1116
|
* @return {Promise} Returns a Promise, when the Promise resolves the value is a list of the dropped indexes.
|
|
1119
1117
|
*/
|
|
1120
|
-
Mongoose.prototype.syncIndexes = function(options) {
|
|
1118
|
+
Mongoose.prototype.syncIndexes = function syncIndexes(options) {
|
|
1121
1119
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
1122
1120
|
return _mongoose.connection.syncIndexes(options);
|
|
1123
1121
|
};
|
|
@@ -1192,8 +1190,8 @@ Mongoose.prototype.Number = SchemaTypes.Number;
|
|
|
1192
1190
|
* @api public
|
|
1193
1191
|
*/
|
|
1194
1192
|
|
|
1195
|
-
Mongoose.prototype.Error =
|
|
1196
|
-
Mongoose.prototype.MongooseError =
|
|
1193
|
+
Mongoose.prototype.Error = MongooseError;
|
|
1194
|
+
Mongoose.prototype.MongooseError = MongooseError;
|
|
1197
1195
|
|
|
1198
1196
|
/**
|
|
1199
1197
|
* Mongoose uses this function to get the current time when setting
|
|
@@ -1218,7 +1216,7 @@ Mongoose.prototype.now = function now() { return new Date(); };
|
|
|
1218
1216
|
* @api public
|
|
1219
1217
|
*/
|
|
1220
1218
|
|
|
1221
|
-
Mongoose.prototype.CastError =
|
|
1219
|
+
Mongoose.prototype.CastError = MongooseError.CastError;
|
|
1222
1220
|
|
|
1223
1221
|
/**
|
|
1224
1222
|
* The constructor used for schematype options
|
package/lib/query.js
CHANGED
|
@@ -3197,7 +3197,6 @@ Query.prototype.deleteMany = function(filter, options) {
|
|
|
3197
3197
|
/**
|
|
3198
3198
|
* Execute a `deleteMany()` query
|
|
3199
3199
|
*
|
|
3200
|
-
* @param {Function} callback
|
|
3201
3200
|
* @method _deleteMany
|
|
3202
3201
|
* @instance
|
|
3203
3202
|
* @memberOf Query
|
|
@@ -3490,13 +3489,6 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
|
|
|
3490
3489
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3491
3490
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
3492
3491
|
*
|
|
3493
|
-
* #### Callback Signature
|
|
3494
|
-
*
|
|
3495
|
-
* function(error, doc) {
|
|
3496
|
-
* // error: any errors that occurred
|
|
3497
|
-
* // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
|
|
3498
|
-
* }
|
|
3499
|
-
*
|
|
3500
3492
|
* #### Example:
|
|
3501
3493
|
*
|
|
3502
3494
|
* A.where().findOneAndDelete(conditions, options) // return Query
|
|
@@ -3587,13 +3579,6 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
|
|
|
3587
3579
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
3588
3580
|
* - `includeResultMetadata`: if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html) rather than just the document
|
|
3589
3581
|
*
|
|
3590
|
-
* #### Callback Signature
|
|
3591
|
-
*
|
|
3592
|
-
* function(error, doc) {
|
|
3593
|
-
* // error: any errors that occurred
|
|
3594
|
-
* // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
|
|
3595
|
-
* }
|
|
3596
|
-
*
|
|
3597
3582
|
* #### Example:
|
|
3598
3583
|
*
|
|
3599
3584
|
* A.where().findOneAndReplace(filter, replacement, options); // return Query
|
|
@@ -4025,7 +4010,6 @@ Query.prototype._replaceOne = async function _replaceOne() {
|
|
|
4025
4010
|
* @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
4026
4011
|
* @param {Boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
|
|
4027
4012
|
* @param {Boolean} [options.overwriteImmutable=false] Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
|
|
4028
|
-
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4029
4013
|
* @return {Query} this
|
|
4030
4014
|
* @see Model.update https://mongoosejs.com/docs/api/model.html#Model.update()
|
|
4031
4015
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
@@ -4096,7 +4080,6 @@ Query.prototype.updateMany = function(conditions, doc, options, callback) {
|
|
|
4096
4080
|
* @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
4097
4081
|
* @param {Boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
|
|
4098
4082
|
* @param {Boolean} [options.overwriteImmutable=false] Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
|
|
4099
|
-
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4100
4083
|
* @return {Query} this
|
|
4101
4084
|
* @see Model.update https://mongoosejs.com/docs/api/model.html#Model.update()
|
|
4102
4085
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
@@ -4163,7 +4146,6 @@ Query.prototype.updateOne = function(conditions, doc, options, callback) {
|
|
|
4163
4146
|
* @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
|
|
4164
4147
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
|
|
4165
4148
|
* @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
4166
|
-
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4167
4149
|
* @return {Query} this
|
|
4168
4150
|
* @see Model.update https://mongoosejs.com/docs/api/model.html#Model.update()
|
|
4169
4151
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
package/lib/schema/string.js
CHANGED
|
@@ -181,11 +181,11 @@ SchemaString.checkRequired = SchemaType.checkRequired;
|
|
|
181
181
|
* const s = new Schema({ state: { type: String, enum: states }})
|
|
182
182
|
* const M = db.model('M', s)
|
|
183
183
|
* const m = new M({ state: 'invalid' })
|
|
184
|
-
* m.save(
|
|
185
|
-
* console.error(
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
184
|
+
* await m.save()
|
|
185
|
+
* .catch((err) => console.error(err)); // ValidationError: `invalid` is not a valid enum value for path `state`.
|
|
186
|
+
* m.state = 'open';
|
|
187
|
+
* await m.save();
|
|
188
|
+
* // success
|
|
189
189
|
*
|
|
190
190
|
* // or with custom error messages
|
|
191
191
|
* const enum = {
|
|
@@ -195,11 +195,11 @@ SchemaString.checkRequired = SchemaType.checkRequired;
|
|
|
195
195
|
* const s = new Schema({ state: { type: String, enum: enum })
|
|
196
196
|
* const M = db.model('M', s)
|
|
197
197
|
* const m = new M({ state: 'invalid' })
|
|
198
|
-
* m.save(
|
|
199
|
-
* console.error(
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
198
|
+
* await m.save()
|
|
199
|
+
* .catch((err) => console.error(err)); // ValidationError: enum validator failed for path `state` with value `invalid`
|
|
200
|
+
* m.state = 'open';
|
|
201
|
+
* await m.save();
|
|
202
|
+
* // success
|
|
203
203
|
*
|
|
204
204
|
* @param {...String|Object} [args] enumeration values
|
|
205
205
|
* @return {SchemaType} this
|
package/lib/schema.js
CHANGED
|
@@ -441,7 +441,7 @@ Schema.prototype._clone = function _clone(Constructor) {
|
|
|
441
441
|
}
|
|
442
442
|
}
|
|
443
443
|
}
|
|
444
|
-
s.
|
|
444
|
+
s._gatherChildSchemas();
|
|
445
445
|
|
|
446
446
|
s.virtuals = clone(this.virtuals);
|
|
447
447
|
s.$globalPluginsApplied = this.$globalPluginsApplied;
|
|
@@ -1238,20 +1238,32 @@ Schema.prototype.path = function(path, obj) {
|
|
|
1238
1238
|
* ignore
|
|
1239
1239
|
*/
|
|
1240
1240
|
|
|
1241
|
-
function
|
|
1241
|
+
Schema.prototype._gatherChildSchemas = function _gatherChildSchemas() {
|
|
1242
1242
|
const childSchemas = [];
|
|
1243
1243
|
|
|
1244
|
-
for (const path of Object.keys(
|
|
1245
|
-
|
|
1244
|
+
for (const path of Object.keys(this.paths)) {
|
|
1245
|
+
if (typeof path !== 'string') {
|
|
1246
|
+
continue;
|
|
1247
|
+
}
|
|
1248
|
+
const schematype = this.paths[path];
|
|
1246
1249
|
if (schematype.$isMongooseDocumentArray || schematype.$isSingleNested) {
|
|
1247
|
-
childSchemas.push({
|
|
1250
|
+
childSchemas.push({
|
|
1251
|
+
schema: schematype.schema,
|
|
1252
|
+
model: schematype.caster,
|
|
1253
|
+
path: path
|
|
1254
|
+
});
|
|
1248
1255
|
} else if (schematype.$isSchemaMap && schematype.$__schemaType.$isSingleNested) {
|
|
1249
|
-
childSchemas.push({
|
|
1256
|
+
childSchemas.push({
|
|
1257
|
+
schema: schematype.$__schemaType.schema,
|
|
1258
|
+
model: schematype.$__schemaType.caster,
|
|
1259
|
+
path: path
|
|
1260
|
+
});
|
|
1250
1261
|
}
|
|
1251
1262
|
}
|
|
1252
1263
|
|
|
1264
|
+
this.childSchemas = childSchemas;
|
|
1253
1265
|
return childSchemas;
|
|
1254
|
-
}
|
|
1266
|
+
};
|
|
1255
1267
|
|
|
1256
1268
|
/*!
|
|
1257
1269
|
* ignore
|
|
@@ -2134,6 +2146,12 @@ Schema.prototype.index = function(fields, options) {
|
|
|
2134
2146
|
}
|
|
2135
2147
|
}
|
|
2136
2148
|
|
|
2149
|
+
for (const existingIndex of this.indexes()) {
|
|
2150
|
+
if (util.isDeepStrictEqual(existingIndex[0], fields)) {
|
|
2151
|
+
throw new MongooseError(`Schema already has an index on ${JSON.stringify(fields)}`);
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2137
2155
|
this._indexes.push([fields, options]);
|
|
2138
2156
|
return this;
|
|
2139
2157
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "8.9.
|
|
4
|
+
"version": "8.9.1",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@babel/core": "7.26.0",
|
|
32
32
|
"@babel/preset-env": "7.26.0",
|
|
33
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
34
|
-
"@typescript-eslint/parser": "^8.
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "^8.18.0",
|
|
34
|
+
"@typescript-eslint/parser": "^8.18.0",
|
|
35
35
|
"acquit": "1.3.0",
|
|
36
36
|
"acquit-ignore": "0.2.1",
|
|
37
37
|
"acquit-require": "0.1.1",
|