mongoose 6.1.10 → 6.2.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/.eslintrc.json +154 -0
- package/CHANGELOG.md +73 -0
- package/dist/browser.umd.js +233 -222
- package/index.js +5 -1
- package/lib/aggregate.js +23 -28
- package/lib/browserDocument.js +1 -1
- package/lib/cast/number.js +2 -3
- package/lib/cast.js +9 -7
- package/lib/connection.js +76 -24
- package/lib/cursor/AggregationCursor.js +12 -7
- package/lib/cursor/QueryCursor.js +11 -6
- package/lib/document.js +131 -122
- package/lib/drivers/node-mongodb-native/collection.js +12 -4
- package/lib/drivers/node-mongodb-native/connection.js +11 -0
- package/lib/error/cast.js +3 -2
- package/lib/error/index.js +11 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/helpers/clone.js +51 -29
- package/lib/helpers/common.js +2 -2
- package/lib/helpers/cursor/eachAsync.js +18 -15
- package/lib/helpers/document/compile.js +7 -4
- package/lib/helpers/getFunctionName.js +6 -4
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
- package/lib/helpers/indexes/getRelatedIndexes.js +59 -0
- package/lib/helpers/isMongooseObject.js +9 -8
- package/lib/helpers/isObject.js +4 -4
- package/lib/helpers/model/discriminator.js +2 -1
- package/lib/helpers/path/parentPaths.js +10 -5
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +4 -2
- package/lib/helpers/populate/assignVals.js +12 -4
- package/lib/helpers/populate/getModelsMapForPopulate.js +4 -4
- package/lib/helpers/populate/markArraySubdocsPopulated.js +3 -1
- package/lib/helpers/populate/modelNamesFromRefPath.js +4 -3
- package/lib/helpers/printJestWarning.js +2 -2
- package/lib/helpers/projection/applyProjection.js +77 -0
- package/lib/helpers/projection/hasIncludedChildren.js +36 -0
- package/lib/helpers/projection/isExclusive.js +5 -2
- package/lib/helpers/projection/isInclusive.js +5 -1
- package/lib/helpers/query/cast$expr.js +279 -0
- package/lib/helpers/query/castUpdate.js +6 -2
- package/lib/helpers/query/hasDollarKeys.js +7 -3
- package/lib/helpers/query/isOperator.js +5 -2
- package/lib/helpers/schema/applyPlugins.js +11 -0
- package/lib/helpers/schema/getIndexes.js +6 -2
- package/lib/helpers/schema/getPath.js +4 -2
- package/lib/helpers/timestamps/setupTimestamps.js +3 -8
- package/lib/index.js +26 -19
- package/lib/internal.js +10 -2
- package/lib/model.js +196 -171
- package/lib/options/SchemaTypeOptions.js +1 -1
- package/lib/plugins/trackTransaction.js +5 -4
- package/lib/query.js +159 -146
- package/lib/queryhelpers.js +10 -10
- package/lib/schema/SubdocumentPath.js +4 -3
- package/lib/schema/array.js +30 -21
- package/lib/schema/buffer.js +1 -1
- package/lib/schema/date.js +1 -1
- package/lib/schema/decimal128.js +1 -1
- package/lib/schema/documentarray.js +9 -11
- package/lib/schema/number.js +1 -1
- package/lib/schema/objectid.js +2 -2
- package/lib/schema/string.js +4 -4
- package/lib/schema.js +13 -8
- package/lib/schematype.js +86 -40
- package/lib/types/ArraySubdocument.js +2 -1
- package/lib/types/DocumentArray/index.js +10 -27
- package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/DocumentArray/methods/index.js +15 -3
- package/lib/types/array/index.js +22 -21
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +22 -23
- package/lib/types/buffer.js +3 -3
- package/lib/types/map.js +3 -4
- package/lib/utils.js +19 -10
- package/package.json +34 -168
- package/tools/repl.js +1 -1
- package/tsconfig.json +8 -0
- package/types/Error.d.ts +129 -0
- package/types/PipelineStage.d.ts +272 -0
- package/{index.d.ts → types/index.d.ts} +169 -481
- package/lib/types/array/ArrayWrapper.js +0 -981
package/lib/index.js
CHANGED
|
@@ -19,7 +19,6 @@ const Query = require('./query');
|
|
|
19
19
|
const Model = require('./model');
|
|
20
20
|
const applyPlugins = require('./helpers/schema/applyPlugins');
|
|
21
21
|
const driver = require('./driver');
|
|
22
|
-
const get = require('./helpers/get');
|
|
23
22
|
const promiseOrCallback = require('./helpers/promiseOrCallback');
|
|
24
23
|
const legacyPluralize = require('./helpers/pluralize');
|
|
25
24
|
const utils = require('./utils');
|
|
@@ -252,17 +251,17 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
|
|
252
251
|
* db.openUri('localhost', 'database', port, [opts]);
|
|
253
252
|
*
|
|
254
253
|
* @param {String} [uri] a mongodb:// URI
|
|
255
|
-
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](
|
|
256
|
-
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](
|
|
254
|
+
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html), except for 4 mongoose-specific options explained below.
|
|
255
|
+
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
|
|
257
256
|
* @param {String} [options.dbName] The name of the database you want to use. If not provided, Mongoose uses the database name from connection string.
|
|
258
257
|
* @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
|
|
259
258
|
* @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
|
|
260
259
|
* @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
|
|
261
260
|
* @param {Number} [options.reconnectTries=30] If you're connected to a single server or mongos proxy (as opposed to a replica set), the MongoDB driver will try to reconnect every `reconnectInterval` milliseconds for `reconnectTries` times, and give up afterward. When the driver gives up, the mongoose connection emits a `reconnectFailed` event. This option does nothing for replica set connections.
|
|
262
261
|
* @param {Number} [options.reconnectInterval=1000] See `reconnectTries` option above.
|
|
263
|
-
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](
|
|
264
|
-
* @param {Number} [options.maxPoolSize=5] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](
|
|
265
|
-
* @param {Number} [options.minPoolSize=1] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](
|
|
262
|
+
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
|
|
263
|
+
* @param {Number} [options.maxPoolSize=5] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
|
|
264
|
+
* @param {Number} [options.minPoolSize=1] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
|
|
266
265
|
* @param {Number} [options.connectTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _during initial connection_. Defaults to 30000. This option is passed transparently to [Node.js' `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback).
|
|
267
266
|
* @param {Number} [options.socketTimeoutMS=30000] 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. This is set to `30000` by default, you should set this to 2-3x your longest running operation if you expect some of your database operations to run longer than 20 seconds. 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.
|
|
268
267
|
* @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.
|
|
@@ -309,20 +308,20 @@ Mongoose.prototype.createConnection = function(uri, options, callback) {
|
|
|
309
308
|
* })
|
|
310
309
|
*
|
|
311
310
|
* @param {String} uri(s)
|
|
312
|
-
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](
|
|
313
|
-
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](
|
|
311
|
+
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html), except for 4 mongoose-specific options explained below.
|
|
312
|
+
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
|
|
314
313
|
* @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
|
|
315
314
|
* @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
|
|
316
315
|
* @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
|
|
317
316
|
* @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
|
|
318
|
-
* @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](
|
|
317
|
+
* @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
|
|
319
318
|
* @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection.
|
|
320
319
|
* @param {Number} [options.serverSelectionTimeoutMS] If `useUnifiedTopology = true`, the MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds before erroring out. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
|
|
321
320
|
* @param {Number} [options.heartbeatFrequencyMS] If `useUnifiedTopology = true`, the MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
|
|
322
321
|
* @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
|
|
323
322
|
* @param {Number} [options.reconnectTries=30] If you're connected to a single server or mongos proxy (as opposed to a replica set), the MongoDB driver will try to reconnect every `reconnectInterval` milliseconds for `reconnectTries` times, and give up afterward. When the driver gives up, the mongoose connection emits a `reconnectFailed` event. This option does nothing for replica set connections.
|
|
324
323
|
* @param {Number} [options.reconnectInterval=1000] See `reconnectTries` option above.
|
|
325
|
-
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](
|
|
324
|
+
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
|
|
326
325
|
* @param {Number} [options.connectTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _during initial connection_. Defaults to 30000. This option is passed transparently to [Node.js' `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback).
|
|
327
326
|
* @param {Number} [options.socketTimeoutMS=30000] 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. This is set to `30000` by default, you should set this to 2-3x your longest running operation if you expect some of your database operations to run longer than 20 seconds. 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.
|
|
328
327
|
* @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.
|
|
@@ -379,13 +378,13 @@ Mongoose.prototype.disconnect = function(callback) {
|
|
|
379
378
|
/**
|
|
380
379
|
* _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
|
|
381
380
|
* for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
|
|
382
|
-
* and [transactions](
|
|
381
|
+
* and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
383
382
|
*
|
|
384
383
|
* Calling `mongoose.startSession()` is equivalent to calling `mongoose.connection.startSession()`.
|
|
385
384
|
* Sessions are scoped to a connection, so calling `mongoose.startSession()`
|
|
386
385
|
* starts a session on the [default mongoose connection](/docs/api.html#mongoose_Mongoose-connection).
|
|
387
386
|
*
|
|
388
|
-
* @param {Object} [options] see the [mongodb driver options](
|
|
387
|
+
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
|
|
389
388
|
* @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
|
390
389
|
* @param {Function} [callback]
|
|
391
390
|
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
|
@@ -632,10 +631,8 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
|
|
|
632
631
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
633
632
|
|
|
634
633
|
options = options || {};
|
|
635
|
-
options.applyPluginsToDiscriminators =
|
|
636
|
-
|
|
637
|
-
options.applyPluginsToChildSchemas = get(_mongoose,
|
|
638
|
-
'options.applyPluginsToChildSchemas', true);
|
|
634
|
+
options.applyPluginsToDiscriminators = _mongoose.options && _mongoose.options.applyPluginsToDiscriminators || false;
|
|
635
|
+
options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToDiscriminators) === 'boolean' ? _mongoose.options.applyPluginsToDiscriminators : true;
|
|
639
636
|
applyPlugins(schema, _mongoose.plugins, options, '$globalPluginsApplied');
|
|
640
637
|
};
|
|
641
638
|
|
|
@@ -951,6 +948,8 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
|
|
|
951
948
|
* mongoose.isValidObjectId({ test: 42 }); // false
|
|
952
949
|
*
|
|
953
950
|
* @method isValidObjectId
|
|
951
|
+
* @param {Any} value
|
|
952
|
+
* @returns {boolean} true if it is a valid ObjectId
|
|
954
953
|
* @api public
|
|
955
954
|
*/
|
|
956
955
|
|
|
@@ -959,15 +958,23 @@ Mongoose.prototype.isValidObjectId = function(v) {
|
|
|
959
958
|
return _mongoose.Types.ObjectId.isValid(v);
|
|
960
959
|
};
|
|
961
960
|
|
|
962
|
-
|
|
961
|
+
/**
|
|
962
|
+
*
|
|
963
|
+
* Syncs all the indexes for the models registered with this connection.
|
|
964
|
+
*
|
|
965
|
+
* @param {Object} options
|
|
966
|
+
* @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.
|
|
967
|
+
* @returns
|
|
968
|
+
*/
|
|
969
|
+
Mongoose.prototype.syncIndexes = function(options) {
|
|
963
970
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
964
|
-
return _mongoose.connection.syncIndexes();
|
|
971
|
+
return _mongoose.connection.syncIndexes(options);
|
|
965
972
|
};
|
|
966
973
|
|
|
967
974
|
/**
|
|
968
975
|
* The Mongoose Decimal128 [SchemaType](/docs/schematypes.html). Used for
|
|
969
976
|
* declaring paths in your schema that should be
|
|
970
|
-
* [128-bit decimal floating points](
|
|
977
|
+
* [128-bit decimal floating points](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-decimal.html).
|
|
971
978
|
* Do not use this to create a new Decimal128 instance, use `mongoose.Types.Decimal128`
|
|
972
979
|
* instead.
|
|
973
980
|
*
|
package/lib/internal.js
CHANGED
|
@@ -10,7 +10,7 @@ const ActiveRoster = StateMachine.ctor('require', 'modify', 'init', 'default', '
|
|
|
10
10
|
module.exports = exports = InternalCache;
|
|
11
11
|
|
|
12
12
|
function InternalCache() {
|
|
13
|
-
this.activePaths = new ActiveRoster;
|
|
13
|
+
this.activePaths = new ActiveRoster();
|
|
14
14
|
this.strictMode = undefined;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -29,7 +29,15 @@ InternalCache.prototype._id = undefined;
|
|
|
29
29
|
InternalCache.prototype.ownerDocument = undefined;
|
|
30
30
|
InternalCache.prototype.populate = undefined; // what we want to populate in this doc
|
|
31
31
|
InternalCache.prototype.populated = undefined;// the _ids that have been populated
|
|
32
|
-
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* If `false`, this document was not the result of population.
|
|
35
|
+
* If `true`, this document is a populated doc underneath another doc
|
|
36
|
+
* If an object, this document is a populated doc and the `value` property of the
|
|
37
|
+
* object contains the original depopulated value.
|
|
38
|
+
*/
|
|
39
|
+
InternalCache.prototype.wasPopulated = false;
|
|
40
|
+
|
|
33
41
|
InternalCache.prototype.scope = undefined;
|
|
34
42
|
|
|
35
43
|
InternalCache.prototype.session = null;
|