mongoose 6.3.9 → 6.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +65 -36
- package/dist/browser.umd.js +68579 -2
- package/lib/cast/date.js +1 -1
- package/lib/cast/decimal128.js +1 -1
- package/lib/cast/objectid.js +1 -1
- package/lib/cast.js +1 -1
- package/lib/connection.js +47 -5
- package/lib/document.js +48 -8
- package/lib/drivers/node-mongodb-native/index.js +1 -1
- package/lib/error/parallelValidate.js +1 -1
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +1 -1
- package/lib/helpers/aggregate/stringifyFunctionOperators.js +1 -1
- package/lib/helpers/arrayDepth.js +1 -1
- package/lib/helpers/cursor/eachAsync.js +1 -1
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +1 -1
- package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +1 -1
- package/lib/helpers/discriminator/getConstructor.js +1 -1
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +1 -1
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
- package/lib/helpers/document/handleSpreadDoc.js +1 -1
- package/lib/helpers/each.js +1 -1
- package/lib/helpers/get.js +1 -1
- package/lib/helpers/getConstructorName.js +1 -1
- package/lib/helpers/getDefaultBulkwriteResult.js +1 -1
- package/lib/helpers/indexes/applySchemaCollation.js +1 -1
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
- package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
- package/lib/helpers/indexes/isTextIndex.js +1 -1
- package/lib/helpers/isMongooseObject.js +1 -1
- package/lib/helpers/isObject.js +1 -1
- package/lib/helpers/isPromise.js +1 -1
- package/lib/helpers/model/applyHooks.js +1 -1
- package/lib/helpers/model/applyStaticHooks.js +1 -1
- package/lib/helpers/once.js +1 -1
- package/lib/helpers/path/flattenObjectWithDottedPaths.js +1 -1
- package/lib/helpers/path/parentPaths.js +1 -1
- package/lib/helpers/path/setDottedPath.js +1 -1
- package/lib/helpers/pluralize.js +1 -1
- package/lib/helpers/populate/SkipPopulateValue.js +1 -1
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -1
- package/lib/helpers/populate/assignVals.js +1 -1
- package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
- package/lib/helpers/populate/getModelsMapForPopulate.js +1 -1
- package/lib/helpers/populate/leanPopulateMap.js +1 -1
- package/lib/helpers/populate/lookupLocalFields.js +1 -1
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/populate/modelNamesFromRefPath.js +1 -1
- package/lib/helpers/populate/removeDeselectedForeignField.js +1 -1
- package/lib/helpers/populate/validateRef.js +1 -1
- package/lib/helpers/printJestWarning.js +1 -1
- package/lib/helpers/processConnectionOptions.js +1 -1
- package/lib/helpers/projection/applyProjection.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/projection/parseProjection.js +1 -1
- package/lib/helpers/query/applyGlobalOption.js +1 -1
- package/lib/helpers/query/applyQueryMiddleware.js +1 -1
- package/lib/helpers/query/cast$expr.js +1 -1
- package/lib/helpers/query/castFilterPath.js +1 -1
- package/lib/helpers/query/sanitizeFilter.js +1 -1
- package/lib/helpers/query/sanitizeProjection.js +1 -1
- package/lib/helpers/query/trusted.js +1 -1
- package/lib/helpers/query/validOps.js +1 -1
- package/lib/helpers/query/wrapThunk.js +1 -1
- package/lib/helpers/schema/addAutoId.js +1 -1
- package/lib/helpers/schema/applyPlugins.js +1 -1
- package/lib/helpers/schema/cleanPositionalOperators.js +1 -1
- package/lib/helpers/schema/getKeysInSchemaOrder.js +1 -1
- package/lib/helpers/schema/getPath.js +1 -1
- package/lib/helpers/schema/handleIdOption.js +1 -1
- package/lib/helpers/schema/handleTimestampOption.js +1 -1
- package/lib/helpers/specialProperties.js +1 -1
- package/lib/helpers/symbols.js +1 -1
- package/lib/helpers/timers.js +1 -1
- package/lib/helpers/timestamps/setupTimestamps.js +5 -2
- package/lib/helpers/topology/allServersUnknown.js +1 -1
- package/lib/helpers/topology/isAtlas.js +1 -1
- package/lib/helpers/topology/isSSLError.js +1 -1
- package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
- package/lib/helpers/update/castArrayFilters.js +1 -1
- package/lib/helpers/update/moveImmutableProperties.js +1 -1
- package/lib/helpers/update/removeUnusedArrayFilters.js +1 -1
- package/lib/helpers/update/updatedPathsByArrayFilter.js +1 -1
- package/lib/helpers/updateValidators.js +11 -18
- package/lib/index.js +108 -29
- package/lib/model.js +1 -1
- package/lib/options/PopulateOptions.js +1 -1
- package/lib/options/SchemaArrayOptions.js +1 -1
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDocumentArrayOptions.js +1 -1
- package/lib/options/SchemaMapOptions.js +1 -1
- package/lib/options/SchemaNumberOptions.js +1 -1
- package/lib/options/SchemaObjectIdOptions.js +1 -1
- package/lib/options/SchemaSubdocumentOptions.js +1 -1
- package/lib/options/SchemaTypeOptions.js +1 -1
- package/lib/options/VirtualOptions.js +1 -1
- package/lib/options/propertyOptions.js +1 -1
- package/lib/options/removeOptions.js +1 -1
- package/lib/options/saveOptions.js +1 -1
- package/lib/plugins/trackTransaction.js +1 -1
- package/lib/query.js +61 -6
- package/lib/queryhelpers.js +5 -0
- package/lib/schema/array.js +1 -1
- package/lib/schema/date.js +2 -2
- package/lib/schema/documentarray.js +10 -0
- package/lib/schema/number.js +2 -2
- package/lib/schema/string.js +2 -2
- package/lib/schema/symbols.js +1 -1
- package/lib/schema.js +6 -7
- package/lib/statemachine.js +13 -0
- package/lib/types/DocumentArray/methods/index.js +1 -1
- package/lib/utils.js +3 -0
- package/lib/validoptions.js +1 -0
- package/package.json +3 -3
- package/tsconfig.json +1 -0
- package/types/collection.d.ts +1 -1
- package/types/connection.d.ts +5 -0
- package/types/document.d.ts +15 -6
- package/types/expressions.d.ts +2882 -0
- package/types/index.d.ts +46 -17
- package/types/{indizes.d.ts → indexes.d.ts} +2 -2
- package/types/inferschematype.d.ts +173 -0
- package/types/middlewares.d.ts +1 -1
- package/types/models.d.ts +86 -71
- package/types/mongooseoptions.d.ts +9 -1
- package/types/pipelinestage.d.ts +76 -80
- package/types/query.d.ts +1 -1
- package/types/schemaoptions.d.ts +22 -4
- package/types/types.d.ts +3 -1
- package/types/utility.d.ts +9 -0
package/lib/cast/date.js
CHANGED
package/lib/cast/decimal128.js
CHANGED
package/lib/cast/objectid.js
CHANGED
package/lib/cast.js
CHANGED
package/lib/connection.js
CHANGED
|
@@ -697,6 +697,18 @@ Connection.prototype.openUri = function(uri, options, callback) {
|
|
|
697
697
|
typeof callback + '"');
|
|
698
698
|
}
|
|
699
699
|
|
|
700
|
+
if (this._destroyCalled) {
|
|
701
|
+
const error = 'Connection has been closed and destroyed, and cannot be used for re-opening the connection. ' +
|
|
702
|
+
'Please create a new connection with `mongoose.createConnection()` or `mongoose.connect()`.';
|
|
703
|
+
if (typeof callback === 'function') {
|
|
704
|
+
callback(error);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
throw new MongooseError(error);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
700
712
|
if (this.readyState === STATES.connecting || this.readyState === STATES.connected) {
|
|
701
713
|
if (this._connectionString !== uri) {
|
|
702
714
|
throw new MongooseError('Can\'t call `openUri()` on an active connection with ' +
|
|
@@ -901,6 +913,23 @@ function _setClient(conn, client, options, dbName) {
|
|
|
901
913
|
}
|
|
902
914
|
}
|
|
903
915
|
|
|
916
|
+
Connection.prototype.destroy = function(force, callback) {
|
|
917
|
+
if (typeof force === 'function') {
|
|
918
|
+
callback = force;
|
|
919
|
+
force = false;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
if (force != null && typeof force === 'object') {
|
|
923
|
+
this.$wasForceClosed = !!force.force;
|
|
924
|
+
} else {
|
|
925
|
+
this.$wasForceClosed = !!force;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
return promiseOrCallback(callback, cb => {
|
|
929
|
+
this._close(force, true, cb);
|
|
930
|
+
});
|
|
931
|
+
};
|
|
932
|
+
|
|
904
933
|
/**
|
|
905
934
|
* Closes the connection
|
|
906
935
|
*
|
|
@@ -923,7 +952,7 @@ Connection.prototype.close = function(force, callback) {
|
|
|
923
952
|
}
|
|
924
953
|
|
|
925
954
|
return promiseOrCallback(callback, cb => {
|
|
926
|
-
this._close(force, cb);
|
|
955
|
+
this._close(force, false, cb);
|
|
927
956
|
});
|
|
928
957
|
};
|
|
929
958
|
|
|
@@ -931,19 +960,26 @@ Connection.prototype.close = function(force, callback) {
|
|
|
931
960
|
* Handles closing the connection
|
|
932
961
|
*
|
|
933
962
|
* @param {Boolean} force
|
|
963
|
+
* @param {Boolean} destroy
|
|
934
964
|
* @param {Function} callback
|
|
935
965
|
* @api private
|
|
936
966
|
*/
|
|
937
|
-
Connection.prototype._close = function(force, callback) {
|
|
967
|
+
Connection.prototype._close = function(force, destroy, callback) {
|
|
938
968
|
const _this = this;
|
|
939
969
|
const closeCalled = this._closeCalled;
|
|
940
970
|
this._closeCalled = true;
|
|
971
|
+
this._destroyCalled = destroy;
|
|
941
972
|
if (this.client != null) {
|
|
942
973
|
this.client._closeCalled = true;
|
|
974
|
+
this.client._destroyCalled = destroy;
|
|
943
975
|
}
|
|
944
976
|
|
|
977
|
+
const conn = this;
|
|
945
978
|
switch (this.readyState) {
|
|
946
979
|
case STATES.disconnected:
|
|
980
|
+
if (destroy && this.base.connections.indexOf(conn) !== -1) {
|
|
981
|
+
this.base.connections.splice(this.base.connections.indexOf(conn), 1);
|
|
982
|
+
}
|
|
947
983
|
if (closeCalled) {
|
|
948
984
|
callback();
|
|
949
985
|
} else {
|
|
@@ -963,6 +999,9 @@ Connection.prototype._close = function(force, callback) {
|
|
|
963
999
|
if (err) {
|
|
964
1000
|
return callback(err);
|
|
965
1001
|
}
|
|
1002
|
+
if (destroy && _this.base.connections.indexOf(conn) !== -1) {
|
|
1003
|
+
_this.base.connections.splice(_this.base.connections.indexOf(conn), 1);
|
|
1004
|
+
}
|
|
966
1005
|
_this.onClose(force);
|
|
967
1006
|
callback(null);
|
|
968
1007
|
});
|
|
@@ -970,12 +1009,15 @@ Connection.prototype._close = function(force, callback) {
|
|
|
970
1009
|
break;
|
|
971
1010
|
case STATES.connecting:
|
|
972
1011
|
this.once('open', function() {
|
|
973
|
-
_this.close(callback);
|
|
1012
|
+
destroy ? _this.destroy(force, callback) : _this.close(force, callback);
|
|
974
1013
|
});
|
|
975
1014
|
break;
|
|
976
1015
|
|
|
977
1016
|
case STATES.disconnecting:
|
|
978
1017
|
this.once('close', function() {
|
|
1018
|
+
if (destroy && _this.base.connections.indexOf(conn) !== -1) {
|
|
1019
|
+
_this.base.connections.splice(_this.base.connections.indexOf(conn), 1);
|
|
1020
|
+
}
|
|
979
1021
|
callback();
|
|
980
1022
|
});
|
|
981
1023
|
break;
|
|
@@ -1004,7 +1046,7 @@ Connection.prototype.onClose = function(force) {
|
|
|
1004
1046
|
this.emit('close', force);
|
|
1005
1047
|
|
|
1006
1048
|
for (const db of this.otherDbs) {
|
|
1007
|
-
db.close({ force: force, skipCloseClient: true });
|
|
1049
|
+
this._destroyCalled ? db.destroy({ force: force, skipCloseClient: true }) : db.close({ force: force, skipCloseClient: true });
|
|
1008
1050
|
}
|
|
1009
1051
|
};
|
|
1010
1052
|
|
|
@@ -1026,7 +1068,7 @@ Connection.prototype.collection = function(name, options) {
|
|
|
1026
1068
|
};
|
|
1027
1069
|
options = Object.assign({}, defaultOptions, options ? utils.clone(options) : {});
|
|
1028
1070
|
options.$wasForceClosed = this.$wasForceClosed;
|
|
1029
|
-
const Collection = driver.get().Collection;
|
|
1071
|
+
const Collection = this.base && this.base.__driver && this.base.__driver.Collection || driver.get().Collection;
|
|
1030
1072
|
if (!(name in this.collections)) {
|
|
1031
1073
|
this.collections[name] = new Collection(name, this, options);
|
|
1032
1074
|
}
|
package/lib/document.js
CHANGED
|
@@ -1035,6 +1035,7 @@ Document.prototype.overwrite = function overwrite(obj) {
|
|
|
1035
1035
|
* @param {Any} val the value to set
|
|
1036
1036
|
* @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
|
|
1037
1037
|
* @param {Object} [options] optionally specify options that modify the behavior of the set
|
|
1038
|
+
* @param {Boolean} [options.merge=false] if true, setting a [nested path](/docs/subdocs.html#subdocuments-versus-nested-paths) will merge existing values rather than overwrite the whole object. So `doc.set('nested', { a: 1, b: 2 })` becomes `doc.set('nested.a', 1); doc.set('nested.b', 2);`
|
|
1038
1039
|
* @method $set
|
|
1039
1040
|
* @name $set
|
|
1040
1041
|
* @memberOf Document
|
|
@@ -2486,7 +2487,7 @@ function _getPathsToValidate(doc) {
|
|
|
2486
2487
|
const fullPathToSubdoc = subdoc.$__fullPathWithIndexes();
|
|
2487
2488
|
|
|
2488
2489
|
for (const p of paths) {
|
|
2489
|
-
if (p
|
|
2490
|
+
if (p == null || p.startsWith(fullPathToSubdoc + '.')) {
|
|
2490
2491
|
paths.delete(p);
|
|
2491
2492
|
}
|
|
2492
2493
|
}
|
|
@@ -2507,6 +2508,14 @@ function _getPathsToValidate(doc) {
|
|
|
2507
2508
|
continue;
|
|
2508
2509
|
}
|
|
2509
2510
|
|
|
2511
|
+
if (_pathType.$isMongooseDocumentArray) {
|
|
2512
|
+
for (const p of paths) {
|
|
2513
|
+
if (p == null || p.startsWith(_pathType.path + '.')) {
|
|
2514
|
+
paths.delete(p);
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2510
2519
|
// Optimization: if primitive path with no validators, or array of primitives
|
|
2511
2520
|
// with no validators, skip validating this path entirely.
|
|
2512
2521
|
if (!_pathType.caster && _pathType.validators.length === 0) {
|
|
@@ -3145,8 +3154,7 @@ Document.prototype.$__reset = function reset() {
|
|
|
3145
3154
|
if (subdoc.$isDocumentArrayElement) {
|
|
3146
3155
|
if (!resetArrays.has(subdoc.parentArray())) {
|
|
3147
3156
|
const array = subdoc.parentArray();
|
|
3148
|
-
|
|
3149
|
-
this.$__.activePaths.init(fullPathWithIndexes.replace(/\.\d+$/, '').slice(-subdoc.$basePath - 1));
|
|
3157
|
+
this.$__.activePaths.clearPath(fullPathWithIndexes.replace(/\.\d+$/, '').slice(-subdoc.$basePath - 1));
|
|
3150
3158
|
array[arrayAtomicsBackupSymbol] = array[arrayAtomicsSymbol];
|
|
3151
3159
|
array[arrayAtomicsSymbol] = {};
|
|
3152
3160
|
|
|
@@ -3154,7 +3162,7 @@ Document.prototype.$__reset = function reset() {
|
|
|
3154
3162
|
}
|
|
3155
3163
|
} else {
|
|
3156
3164
|
if (subdoc.$parent() === this) {
|
|
3157
|
-
this.$__.activePaths.
|
|
3165
|
+
this.$__.activePaths.clearPath(subdoc.$basePath);
|
|
3158
3166
|
} else if (subdoc.$parent() != null && subdoc.$parent().$isSubdocument) {
|
|
3159
3167
|
// If map path underneath subdocument, may end up with a case where
|
|
3160
3168
|
// map path is modified but parent still needs to be reset. See gh-10295
|
|
@@ -4262,10 +4270,10 @@ Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() {
|
|
|
4262
4270
|
*
|
|
4263
4271
|
* #### Example:
|
|
4264
4272
|
*
|
|
4265
|
-
* Model.findOne().populate('author')
|
|
4266
|
-
*
|
|
4267
|
-
*
|
|
4268
|
-
*
|
|
4273
|
+
* const doc = await Model.findOne().populate('author');
|
|
4274
|
+
*
|
|
4275
|
+
* console.log(doc.author.name); // Dr.Seuss
|
|
4276
|
+
* console.log(doc.populated('author')); // '5144cf8050f071d979c118a7'
|
|
4269
4277
|
*
|
|
4270
4278
|
* If the path was not populated, returns `undefined`.
|
|
4271
4279
|
*
|
|
@@ -4319,6 +4327,38 @@ Document.prototype.populated = function(path, val, options) {
|
|
|
4319
4327
|
|
|
4320
4328
|
Document.prototype.$populated = Document.prototype.populated;
|
|
4321
4329
|
|
|
4330
|
+
/**
|
|
4331
|
+
* Throws an error if a given path is not populated
|
|
4332
|
+
*
|
|
4333
|
+
* #### Example:
|
|
4334
|
+
*
|
|
4335
|
+
* const doc = await Model.findOne().populate('author');
|
|
4336
|
+
*
|
|
4337
|
+
* doc.$assertPopulated('author'); // does not throw
|
|
4338
|
+
* doc.$assertPopulated('other path'); // throws an error
|
|
4339
|
+
*
|
|
4340
|
+
*
|
|
4341
|
+
* @param {String|Array<String>} path
|
|
4342
|
+
* @return {Document} this
|
|
4343
|
+
* @memberOf Document
|
|
4344
|
+
* @method $assertPopulated
|
|
4345
|
+
* @instance
|
|
4346
|
+
* @api public
|
|
4347
|
+
*/
|
|
4348
|
+
|
|
4349
|
+
Document.prototype.$assertPopulated = function $assertPopulated(paths) {
|
|
4350
|
+
if (Array.isArray(paths)) {
|
|
4351
|
+
paths.forEach(path => this.$assertPopulated(path));
|
|
4352
|
+
return this;
|
|
4353
|
+
}
|
|
4354
|
+
|
|
4355
|
+
if (!this.$populated(paths)) {
|
|
4356
|
+
throw new MongooseError(`Expected path "${paths}" to be populated`);
|
|
4357
|
+
}
|
|
4358
|
+
|
|
4359
|
+
return this;
|
|
4360
|
+
};
|
|
4361
|
+
|
|
4322
4362
|
/**
|
|
4323
4363
|
* Takes a populated field and returns it to its unpopulated state.
|
|
4324
4364
|
*
|
|
@@ -9,4 +9,4 @@ exports.Collection = require('./collection');
|
|
|
9
9
|
exports.Decimal128 = require('./decimal128');
|
|
10
10
|
exports.ObjectId = require('./objectid');
|
|
11
11
|
exports.ReadPreference = require('./ReadPreference');
|
|
12
|
-
exports.getConnection = () => require('./connection');
|
|
12
|
+
exports.getConnection = () => require('./connection');
|
package/lib/helpers/each.js
CHANGED
package/lib/helpers/get.js
CHANGED
package/lib/helpers/isObject.js
CHANGED
package/lib/helpers/isPromise.js
CHANGED
package/lib/helpers/once.js
CHANGED
package/lib/helpers/pluralize.js
CHANGED
|
@@ -14,4 +14,4 @@ if (typeof jest !== 'undefined' && process.nextTick.toString().indexOf('nextTick
|
|
|
14
14
|
'with Jest\'s mock timers enabled. Please make sure you read ' +
|
|
15
15
|
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
|
|
16
16
|
'https://mongoosejs.com/docs/jest.html');
|
|
17
|
-
}
|
|
17
|
+
}
|
|
@@ -9,7 +9,7 @@ function processConnectionOptions(uri, options) {
|
|
|
9
9
|
? opts.readPreference
|
|
10
10
|
: getUriReadPreference(uri);
|
|
11
11
|
|
|
12
|
-
const resolvedOpts = (readPreference && readPreference !== 'primary')
|
|
12
|
+
const resolvedOpts = (readPreference && readPreference !== 'primary' && readPreference !== 'primaryPreferred')
|
|
13
13
|
? resolveOptsConflicts(readPreference, opts)
|
|
14
14
|
: opts;
|
|
15
15
|
|