mongoose 9.13.0 → 9.13.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.
@@ -98,10 +98,10 @@ module.exports = async function updateValidators(query, schema, castedDoc, optio
98
98
  }
99
99
 
100
100
  if (v && Array.isArray(v.$in)) {
101
- v.$in.forEach((v, i) => {
101
+ v.$in.forEach((value, index) => {
102
102
  validatorsToExecute.push(
103
- schemaPath.doValidate(v, context, { updateValidator: true }).catch(err => {
104
- err.path = updates[i] + '.$in.' + i;
103
+ schemaPath.doValidate(value, context, { updateValidator: true }).catch(err => {
104
+ err.path = updates[i] + '.$in.' + index;
105
105
  validationErrors.push(err);
106
106
  })
107
107
  );
package/lib/model.js CHANGED
@@ -3464,9 +3464,9 @@ function _setIsNew(doc, val, hasOnlyPrimitiveValues) {
3464
3464
  * @param {boolean} [options.ordered=true] If true, execute writes in order and stop at the first error. If false, execute writes in parallel and continue until all writes have either succeeded or errored.
3465
3465
  * @param {boolean} [options.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to any operations. Can be overridden at the operation-level.
3466
3466
  * @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
3467
- * @param {string|number} [options.w=1] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
3467
+ * @param {string|number} [options.w] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). If not set, the MongoDB server's default write concern applies. See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
3468
3468
  * @param {number} [options.wtimeout=null] The [write concern timeout](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout).
3469
- * @param {boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
3469
+ * @param {boolean} [options.j] Set to true to request [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option). If not set, the MongoDB server's default write concern applies.
3470
3470
  * @param {boolean} [options.skipValidation=false] Set to true to skip Mongoose schema validation on bulk write operations. Mongoose currently runs validation on `insertOne` and `replaceOne` operations by default.
3471
3471
  * @param {boolean} [options.bypassDocumentValidation=false] If true, disable [MongoDB server-side schema validation](https://www.mongodb.com/docs/manual/core/schema-validation/) for all writes in this bulk.
3472
3472
  * @param {boolean} [options.throwOnValidationError=false] If true and `ordered: false`, throw an error if one of the operations failed validation, but all valid operations completed successfully. Note that Mongoose will still send all valid operations to the MongoDB server.
@@ -3656,9 +3656,9 @@ async function _bulkWrite(ops, options) {
3656
3656
  * @param {object} [options] options passed to the underlying `bulkWrite()`
3657
3657
  * @param {boolean} [options.timestamps] defaults to `null`, when set to false, mongoose will not add/update timestamps to the documents.
3658
3658
  * @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
3659
- * @param {string|number} [options.w=1] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
3659
+ * @param {string|number} [options.w] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). If not set, the MongoDB server's default write concern applies. See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
3660
3660
  * @param {number} [options.wtimeout=null] The [write concern timeout](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout).
3661
- * @param {boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
3661
+ * @param {boolean} [options.j] Set to true to request [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option). If not set, the MongoDB server's default write concern applies.
3662
3662
  * @param {boolean} [options.validateBeforeSave=true] set to `false` to skip Mongoose validation on all documents
3663
3663
  * @param {boolean|object} [options.middleware=true] set to `false` to skip all user-defined middleware
3664
3664
  * @param {boolean} [options.middleware.pre=true] set to `false` to skip only pre hooks
@@ -53,8 +53,13 @@ Object.defineProperty(SchemaArrayOptions.prototype, 'enum', opts);
53
53
  Object.defineProperty(SchemaArrayOptions.prototype, 'of', opts);
54
54
 
55
55
  /**
56
- * If set to `false`, will always deactivate casting non-array values to arrays.
57
- * If set to `true`, will cast non-array values to arrays if `init` and `SchemaArray.options.castNonArrays` are also `true`
56
+ * If set to `false`, Mongoose throws a `CastError` when you set a non-array value
57
+ * on this path. If set to `true`, Mongoose wraps the value in an array. Overrides
58
+ * the global `mongoose.Schema.Types.Array.options.castNonArrays` for this path.
59
+ * Values loaded from MongoDB are always wrapped, regardless of this option.
60
+ *
61
+ * This option only applies to the array it's set on, not to arrays nested
62
+ * inside it. For nested arrays, set `castNonArrays` on each level.
58
63
  *
59
64
  * #### Example:
60
65
  *
@@ -62,6 +67,12 @@ Object.defineProperty(SchemaArrayOptions.prototype, 'of', opts);
62
67
  * const doc = new Model({ x1: "some non-array value" });
63
68
  * await doc.validate(); // Errors with "CastError"
64
69
  *
70
+ * // Only the outer array: `1` is a CastError, but `[1, 2]` becomes `[[1, 2]]`
71
+ * new Schema({ matrix: { castNonArrays: false, type: [[Number]] } });
72
+ *
73
+ * // Both levels: `1` and `[1, 2]` are both CastErrors
74
+ * new Schema({ matrix: { castNonArrays: false, type: [{ type: [Number], castNonArrays: false }] } });
75
+ *
65
76
  * @api public
66
77
  * @property castNonArrays
67
78
  * @memberOf SchemaArrayOptions
@@ -296,26 +296,28 @@ SchemaArray.prototype.applyGetters = function(value, scope) {
296
296
  };
297
297
 
298
298
  SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
299
- const castNonArraysOption = this.options.castNonArrays ?? SchemaArray.options.castNonArrays;
300
- if (this.embeddedSchemaType.$isMongooseArray &&
301
- castNonArraysOption) {
302
- // Check nesting levels and wrap in array if necessary
303
- let depth = 0;
299
+ // No need to wrap empty arrays
300
+ if (this.embeddedSchemaType.$isMongooseArray && value != null && value.length !== 0) {
301
+ // This array and its nested arrays, outermost first
302
+ const levels = [];
304
303
  let arr = this;
305
304
  while (arr != null &&
306
305
  arr.$isMongooseArray &&
307
306
  !arr.$isMongooseDocumentArray) {
308
- ++depth;
307
+ levels.push(arr);
309
308
  arr = arr.embeddedSchemaType;
310
309
  }
311
310
 
312
- // No need to wrap empty arrays
313
- if (value != null && value.length !== 0) {
314
- const valueDepth = arrayDepth(value);
315
- if (valueDepth.min === valueDepth.max && valueDepth.max < depth && valueDepth.containsNonArrayItem) {
316
- for (let i = valueDepth.max; i < depth; ++i) {
317
- value = [value];
318
- }
311
+ // Check nesting levels and wrap in array if necessary. A value nested
312
+ // `valueDepth.max` levels deep puts non-array values in every level from
313
+ // `valueDepth.max` inward, so each of those levels must allow wrapping.
314
+ const valueDepth = arrayDepth(value);
315
+ if (valueDepth.min === valueDepth.max &&
316
+ valueDepth.max < levels.length &&
317
+ valueDepth.containsNonArrayItem &&
318
+ levels.slice(valueDepth.max).every(level => level.options.castNonArrays ?? SchemaArray.options.castNonArrays)) {
319
+ for (let i = valueDepth.max; i < levels.length; ++i) {
320
+ value = [value];
319
321
  }
320
322
  }
321
323
  }
package/lib/schema.js CHANGED
@@ -1750,7 +1750,9 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1750
1750
  ? cast[options.typeKey]
1751
1751
  : cast;
1752
1752
  if (Array.isArray(type)) {
1753
- return new MongooseTypes.Array(path, this.interpretAsType(path, type, options), obj, null, this);
1753
+ // Pass `cast` rather than `type` so options on a nested array, like
1754
+ // `[{ type: [Number], castNonArrays: false }]`, aren't dropped.
1755
+ return new MongooseTypes.Array(path, this.interpretAsType(path, cast, options), obj, null, this);
1754
1756
  }
1755
1757
 
1756
1758
  name = typeof type === 'string'
@@ -2855,6 +2857,17 @@ Schema.prototype.remove = function(path) {
2855
2857
  }
2856
2858
  this._defaultsCache = null;
2857
2859
  this._hasSelectedPaths = undefined;
2860
+ // Drop aliases that point to the removed path, or to a path under it, along
2861
+ // with their virtuals, so they don't keep translating keys to a missing path.
2862
+ for (const [alias, target] of Object.entries(this.aliases)) {
2863
+ if (target === name || target.startsWith(name + '.')) {
2864
+ delete this.aliases[alias];
2865
+ delete this.virtuals[alias];
2866
+ if (get(this.tree, alias) instanceof VirtualType) {
2867
+ _deletePath(this, alias);
2868
+ }
2869
+ }
2870
+ }
2858
2871
  if (this.nested[name]) {
2859
2872
  const allKeys = Object.keys(this.paths).
2860
2873
  concat(Object.keys(this.nested));
@@ -2955,6 +2968,22 @@ Schema.prototype.removeVirtual = function(path) {
2955
2968
  }
2956
2969
 
2957
2970
  for (const virtual of path) {
2971
+ if (Object.hasOwn(this.aliases, virtual)) {
2972
+ // `pick()` rebuilds aliases from the SchemaType's options, and `omit()` and
2973
+ // `add(schema)` rebuild them from the tree, so drop the alias from both or it
2974
+ // comes back on every schema derived from this one.
2975
+ const aliasedPath = this.aliases[virtual];
2976
+ delete this.aliases[virtual];
2977
+ const treeEntry = get(this.tree, aliasedPath);
2978
+ const treeOptions = treeEntry instanceof SchemaType ? treeEntry.options : treeEntry;
2979
+ for (const options of [this.paths[aliasedPath]?.options, treeOptions]) {
2980
+ if (options?.alias === virtual) {
2981
+ delete options.alias;
2982
+ } else if (Array.isArray(options?.alias)) {
2983
+ options.alias = options.alias.filter(a => a !== virtual);
2984
+ }
2985
+ }
2986
+ }
2958
2987
  delete this.paths[virtual];
2959
2988
  delete this.virtuals[virtual];
2960
2989
  if (virtual.indexOf('.') !== -1) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "9.13.0",
4
+ "version": "9.13.1",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -323,11 +323,13 @@ declare module 'mongoose' {
323
323
  exact?: boolean;
324
324
  index: string,
325
325
  path: string,
326
- queryVector: number[],
327
326
  numCandidates?: number,
328
327
  limit: number,
329
328
  filter?: Expression,
330
- }
329
+ } & (
330
+ { queryVector: number[], query?: never, model?: never } |
331
+ { query: { text: string }, queryVector?: never, model?: string }
332
+ )
331
333
  }
332
334
 
333
335
  }
package/tstyche.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "$schema": "https://tstyche.org/schemas/config.json",
3
- "testFileMatch": [
4
- "test/types/*.test.*",
5
- "test/types/**/*.test.*"
6
- ]
7
- }
@@ -1,9 +0,0 @@
1
- {
2
- "$schema": "https://tstyche.org/schemas/config.json",
3
- "checkDeclarationFiles": false,
4
- "checkSuppressedErrors": false,
5
- "testFileMatch": [
6
- "test/types/*.test.*",
7
- "test/types/**/*.test.*"
8
- ]
9
- }