mongoose 7.4.0 → 7.4.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/lib/document.js CHANGED
@@ -2521,21 +2521,17 @@ Document.prototype.isDirectSelected = function isDirectSelected(path) {
2521
2521
  *
2522
2522
  * #### Note:
2523
2523
  *
2524
- * This method is called `pre` save and if a validation rule is violated, [save](https://mongoosejs.com/docs/api/model.html#Model.prototype.save()) is aborted and the error is returned to your `callback`.
2524
+ * This method is called `pre` save and if a validation rule is violated, [save](https://mongoosejs.com/docs/api/model.html#Model.prototype.save()) is aborted and the error is thrown.
2525
2525
  *
2526
2526
  * #### Example:
2527
2527
  *
2528
- * doc.validate(function (err) {
2529
- * if (err) handleError(err);
2530
- * else // validation passed
2531
- * });
2528
+ * await doc.validate({ validateModifiedOnly: false, pathsToSkip: ['name', 'email']});
2532
2529
  *
2533
2530
  * @param {Array|String} [pathsToValidate] list of paths to validate. If set, Mongoose will validate only the modified paths that are in the given list.
2534
2531
  * @param {Object} [options] internal options
2535
2532
  * @param {Boolean} [options.validateModifiedOnly=false] if `true` mongoose validates only modified paths.
2536
2533
  * @param {Array|string} [options.pathsToSkip] list of paths to skip. If set, Mongoose will validate every modified path that is not in this list.
2537
- * @param {Function} [callback] optional callback called after validation completes, passing an error if one occurred
2538
- * @return {Promise} Returns a Promise if no `callback` is given.
2534
+ * @return {Promise} Returns a Promise.
2539
2535
  * @api public
2540
2536
  */
2541
2537
 
@@ -3325,8 +3321,8 @@ Document.prototype.$__reset = function reset() {
3325
3321
  const resetArrays = new Set();
3326
3322
  for (const subdoc of subdocs) {
3327
3323
  const fullPathWithIndexes = subdoc.$__fullPathWithIndexes();
3324
+ subdoc.$__reset();
3328
3325
  if (this.isModified(fullPathWithIndexes) || isParentInit(fullPathWithIndexes)) {
3329
- subdoc.$__reset();
3330
3326
  if (subdoc.$isDocumentArrayElement) {
3331
3327
  resetArrays.add(subdoc.parentArray());
3332
3328
  } else {
@@ -3,6 +3,9 @@
3
3
  const get = require('../get');
4
4
 
5
5
  module.exports = function applyWriteConcern(schema, options) {
6
+ if (options.writeConcern != null) {
7
+ return;
8
+ }
6
9
  const writeConcern = get(schema, 'options.writeConcern', {});
7
10
  if (Object.keys(writeConcern).length != 0) {
8
11
  options.writeConcern = {};
package/lib/model.js CHANGED
@@ -3045,8 +3045,10 @@ Model.startSession = function() {
3045
3045
  *
3046
3046
  * #### Example:
3047
3047
  *
3048
- * const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
3049
- * Movies.insertMany(arr, function(error, docs) {});
3048
+ * await Movies.insertMany([
3049
+ * { name: 'Star Wars' },
3050
+ * { name: 'The Empire Strikes Back' }
3051
+ * ]);
3050
3052
  *
3051
3053
  * @param {Array|Object|*} doc(s)
3052
3054
  * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#insertMany)
package/lib/query.js CHANGED
@@ -1968,7 +1968,6 @@ Query.prototype._optionsForExec = function(model) {
1968
1968
  if (!model) {
1969
1969
  return options;
1970
1970
  }
1971
-
1972
1971
  // Apply schema-level `writeConcern` option
1973
1972
  applyWriteConcern(model.schema, options);
1974
1973
 
package/lib/schema.js CHANGED
@@ -748,10 +748,10 @@ Schema.prototype.add = function add(obj, prefix) {
748
748
  childSchemaOptions.strict = this._userProvidedOptions.strict;
749
749
  }
750
750
  if (this._userProvidedOptions.toObject != null) {
751
- childSchemaOptions.toObject = this._userProvidedOptions.toObject;
751
+ childSchemaOptions.toObject = utils.omit(this._userProvidedOptions.toObject, ['transform']);
752
752
  }
753
753
  if (this._userProvidedOptions.toJSON != null) {
754
- childSchemaOptions.toJSON = this._userProvidedOptions.toJSON;
754
+ childSchemaOptions.toJSON = utils.omit(this._userProvidedOptions.toJSON, ['transform']);
755
755
  }
756
756
 
757
757
  const _schema = new Schema(_typeDef, childSchemaOptions);
@@ -1346,10 +1346,10 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1346
1346
  childSchemaOptions.strictQuery = options.strictQuery;
1347
1347
  }
1348
1348
  if (options.hasOwnProperty('toObject')) {
1349
- childSchemaOptions.toObject = options.toObject;
1349
+ childSchemaOptions.toObject = utils.omit(options.toObject, ['transform']);
1350
1350
  }
1351
1351
  if (options.hasOwnProperty('toJSON')) {
1352
- childSchemaOptions.toJSON = options.toJSON;
1352
+ childSchemaOptions.toJSON = utils.omit(options.toJSON, ['transform']);
1353
1353
  }
1354
1354
 
1355
1355
  if (this._userProvidedOptions.hasOwnProperty('_id')) {
@@ -33,7 +33,15 @@ function ArraySubdocument(obj, parentArr, skipId, fields, index) {
33
33
  this.$setIndex(index);
34
34
  this.$__parent = this[documentArrayParent];
35
35
 
36
- Subdocument.call(this, obj, fields, this[documentArrayParent], skipId, { isNew: true });
36
+ let options;
37
+ if (typeof skipId === 'object' && skipId != null) {
38
+ options = { isNew: true, ...skipId };
39
+ skipId = undefined;
40
+ } else {
41
+ options = { isNew: true };
42
+ }
43
+
44
+ Subdocument.call(this, obj, fields, this[documentArrayParent], skipId, options);
37
45
  }
38
46
 
39
47
  /*!
@@ -38,7 +38,7 @@ const methods = {
38
38
  * @memberOf MongooseDocumentArray
39
39
  */
40
40
 
41
- _cast(value, index) {
41
+ _cast(value, index, options) {
42
42
  if (this[arraySchemaSymbol] == null) {
43
43
  return value;
44
44
  }
@@ -89,7 +89,7 @@ const methods = {
89
89
  if (Constructor.$isMongooseDocumentArray) {
90
90
  return Constructor.cast(value, this, undefined, undefined, index);
91
91
  }
92
- const ret = new Constructor(value, this, undefined, undefined, index);
92
+ const ret = new Constructor(value, this, options, undefined, index);
93
93
  ret.isNew = true;
94
94
  return ret;
95
95
  },
@@ -595,7 +595,7 @@ const methods = {
595
595
  */
596
596
 
597
597
  pull() {
598
- const values = [].map.call(arguments, this._cast, this);
598
+ const values = [].map.call(arguments, (v, i) => this._cast(v, i, { defaults: false }), this);
599
599
  const cur = this[arrayParentSymbol].get(this[arrayPathSymbol]);
600
600
  let i = cur.length;
601
601
  let mem;
@@ -16,6 +16,10 @@ module.exports = Subdocument;
16
16
  */
17
17
 
18
18
  function Subdocument(value, fields, parent, skipId, options) {
19
+ if (typeof skipId === 'object' && skipId != null && options == null) {
20
+ options = skipId;
21
+ skipId = undefined;
22
+ }
19
23
  if (parent != null) {
20
24
  // If setting a nested path, should copy isNew from parent re: gh-7048
21
25
  const parentOptions = { isNew: parent.isNew };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "7.4.0",
4
+ "version": "7.4.1",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/types/index.d.ts CHANGED
@@ -378,11 +378,22 @@ declare module 'mongoose' {
378
378
  pre<T = never>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: false }, fn: PreMiddlewareFunction<T>): this;
379
379
  pre<T = never>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPreOptions & { document: boolean, query: false }, fn: PreMiddlewareFunction<T>): this;
380
380
  pre<T = never>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: boolean }, fn: PreMiddlewareFunction<T>): this;
381
+ // this = Union of Document and Query, could be called with any of them
382
+ pre<T = THydratedDocumentType | Query<any, any>>(
383
+ method: MongooseQueryAndDocumentMiddleware | MongooseQueryAndDocumentMiddleware[] | RegExp,
384
+ options: SchemaPreOptions & { document: true, query: true },
385
+ fn: PreMiddlewareFunction<T>
386
+ ): this;
381
387
  // this = Document
382
388
  pre<T = THydratedDocumentType>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
383
389
  pre<T = THydratedDocumentType>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
384
390
  pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction<T>): this;
385
391
  pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
392
+ pre<T = THydratedDocumentType>(
393
+ method: MongooseQueryAndDocumentMiddleware | MongooseQueryAndDocumentMiddleware[] | RegExp,
394
+ options: SchemaPreOptions & { document: true },
395
+ fn: PreMiddlewareFunction<T>
396
+ ): this;
386
397
  pre<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: true, query: false }, fn: PreMiddlewareFunction<T>): this;
387
398
  // this = Query
388
399
  pre<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PreMiddlewareFunction<T>): this;
@@ -218,10 +218,11 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
218
218
  PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
219
219
  IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
220
220
  PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
221
- PathValueType extends ArrayConstructor ? any[] :
222
- PathValueType extends typeof Schema.Types.Mixed ? any:
223
- IfEquals<PathValueType, ObjectConstructor> extends true ? any:
224
- IfEquals<PathValueType, {}> extends true ? any:
225
- PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
226
- PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
227
- unknown;
221
+ IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
222
+ PathValueType extends ArrayConstructor ? any[] :
223
+ PathValueType extends typeof Schema.Types.Mixed ? any:
224
+ IfEquals<PathValueType, ObjectConstructor> extends true ? any:
225
+ IfEquals<PathValueType, {}> extends true ? any:
226
+ PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
227
+ PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
228
+ unknown;
package/types/query.d.ts CHANGED
@@ -136,6 +136,10 @@ declare module 'mongoose' {
136
136
  * Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
137
137
  */
138
138
  returnDocument?: 'before' | 'after';
139
+ /**
140
+ * Set to true to enable `update validators`
141
+ * (https://mongoosejs.com/docs/validation.html#update-validators). Defaults to false.
142
+ */
139
143
  runValidators?: boolean;
140
144
  /* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
141
145
  sanitizeProjection?: boolean;