mongoose 6.0.7 → 6.0.8

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/index.d.ts CHANGED
@@ -1538,10 +1538,10 @@ declare module 'mongoose' {
1538
1538
  type?:
1539
1539
  T extends string | number | boolean | NativeDate | Function ? SchemaDefinitionWithBuiltInClass<T> :
1540
1540
  T extends object[] ? (AnyArray<Schema<any>> | AnyArray<SchemaDefinition<Unpacked<T>>>) :
1541
- T extends string[] ? AnyArray<SchemaDefinitionWithBuiltInClass<string>> :
1542
- T extends number[] ? AnyArray<SchemaDefinitionWithBuiltInClass<number>> :
1543
- T extends boolean[] ? AnyArray<SchemaDefinitionWithBuiltInClass<boolean>> :
1544
- T extends Function[] ? AnyArray<SchemaDefinitionWithBuiltInClass<Function>> :
1541
+ T extends string[] ? AnyArray<SchemaDefinitionWithBuiltInClass<string>> | AnyArray<SchemaTypeOptions<string>> :
1542
+ T extends number[] ? AnyArray<SchemaDefinitionWithBuiltInClass<number>> | AnyArray<SchemaTypeOptions<number>> :
1543
+ T extends boolean[] ? AnyArray<SchemaDefinitionWithBuiltInClass<boolean>> | AnyArray<SchemaTypeOptions<boolean>> :
1544
+ T extends Function[] ? AnyArray<SchemaDefinitionWithBuiltInClass<Function>> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
1545
1545
  T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T>;
1546
1546
 
1547
1547
  /** Defines a virtual with the given name that gets/sets this path. */
@@ -2606,6 +2606,7 @@ declare module 'mongoose' {
2606
2606
  type __UpdateDefProperty<T> =
2607
2607
  0 extends (1 & T) ? T : // any
2608
2608
  T extends unknown[] ? LeanArray<T> : // Array
2609
+ T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
2609
2610
  T extends Document ? LeanDocument<T> : // Subdocument
2610
2611
  [Extract<T, mongodb.ObjectId>] extends [never] ? T :
2611
2612
  T | string;
@@ -2623,7 +2624,14 @@ declare module 'mongoose' {
2623
2624
  : _AllowStringsForIds<T[K]>
2624
2625
  : T[K] | string;
2625
2626
  };
2626
- export type DocumentDefinition<T> = _AllowStringsForIds<LeanDocument<T>>;
2627
+ export type DocumentDefinition<T> = {
2628
+ [K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>>]:
2629
+ [Extract<T[K], mongodb.ObjectId>] extends [never]
2630
+ ? T[K] extends TreatAsPrimitives
2631
+ ? T[K]
2632
+ : LeanDocumentElement<T[K]>
2633
+ : T[K] | string;
2634
+ };
2627
2635
 
2628
2636
  type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
2629
2637
  type TreatAsPrimitives = actualPrimitives |
@@ -2632,6 +2640,7 @@ declare module 'mongoose' {
2632
2640
  type LeanType<T> =
2633
2641
  0 extends (1 & T) ? T : // any
2634
2642
  T extends TreatAsPrimitives ? T : // primitives
2643
+ T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> : // subdocs
2635
2644
  LeanDocument<T>; // Documents and everything else
2636
2645
 
2637
2646
  type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
@@ -18,7 +18,7 @@ class ObjectExpectedError extends MongooseError {
18
18
  constructor(path, val) {
19
19
  const typeDescription = Array.isArray(val) ? 'array' : 'primitive value';
20
20
  super('Tried to set nested object field `' + path +
21
- `\` to ${typeDescription} \`` + val + '` and strict mode is set to throw.');
21
+ `\` to ${typeDescription} \`` + val + '`');
22
22
  this.path = path;
23
23
  }
24
24
  }
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const get = require('../get');
4
+ const utils = require('../../utils');
4
5
 
5
6
  /*!
6
7
  * Register methods for this model
@@ -30,7 +31,7 @@ module.exports = function applyMethods(model, schema) {
30
31
  }
31
32
  if (schema.reserved[method] &&
32
33
  !get(schema, `methodOptions.${method}.suppressWarning`, false)) {
33
- console.warn(`mongoose: the method name "${method}" is used by mongoose ` +
34
+ utils.warn(`mongoose: the method name "${method}" is used by mongoose ` +
34
35
  'internally, overwriting it may cause bugs. If you\'re sure you know ' +
35
36
  'what you\'re doing, you can suppress this error by using ' +
36
37
  `\`schema.method('${method}', fn, { suppressWarning: true })\`.`);
@@ -1,14 +1,16 @@
1
1
  'use strict';
2
2
 
3
+ const utils = require('../utils');
4
+
3
5
  if (typeof jest !== 'undefined' && typeof window !== 'undefined') {
4
- console.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
6
+ utils.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
5
7
  'with Jest\'s default jsdom test environment. Please make sure you read ' +
6
8
  'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
7
9
  'http://mongoosejs.com/docs/jest.html');
8
10
  }
9
11
 
10
12
  if (typeof jest !== 'undefined' && process.nextTick.toString().indexOf('nextTick') === -1) {
11
- console.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
13
+ utils.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
12
14
  'with Jest\'s mock timers enabled. Please make sure you read ' +
13
15
  'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
14
16
  'http://mongoosejs.com/docs/jest.html');
@@ -31,7 +31,7 @@ module.exports = function setupTimestamps(schema, timestamps) {
31
31
  }
32
32
 
33
33
  if (createdAt && !schema.paths[createdAt]) {
34
- schemaAdditions[createdAt] = { type: Date, immutable: true };
34
+ schemaAdditions[createdAt] = { [schema.options.typeKey || 'type']: Date, immutable: true };
35
35
  }
36
36
  schema.add(schemaAdditions);
37
37
 
package/lib/model.js CHANGED
@@ -1673,7 +1673,7 @@ function _ensureIndexes(model, options, callback) {
1673
1673
 
1674
1674
  for (const index of indexes) {
1675
1675
  if (isDefaultIdIndex(index)) {
1676
- console.warn('mongoose: Cannot specify a custom index on `_id` for ' +
1676
+ utils.warn('mongoose: Cannot specify a custom index on `_id` for ' +
1677
1677
  'model name "' + model.modelName + '", ' +
1678
1678
  'MongoDB does not allow overwriting the default `_id` index. See ' +
1679
1679
  'http://bit.ly/mongodb-id-index');
@@ -1876,6 +1876,7 @@ Model.translateAliases = function translateAliases(fields) {
1876
1876
  // Alias found,
1877
1877
  translated.push(alias);
1878
1878
  } else {
1879
+ alias = name;
1879
1880
  // Alias not found, so treat as un-aliased key
1880
1881
  translated.push(name);
1881
1882
  }
@@ -3065,7 +3066,7 @@ Model.create = function create(doc, options, callback) {
3065
3066
  !this.schema.path('session')) {
3066
3067
  // Probably means the user is running into the common mistake of trying
3067
3068
  // to use a spread to specify options, see gh-7535
3068
- console.warn('WARNING: to pass a `session` to `Model.create()` in ' +
3069
+ utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +
3069
3070
  'Mongoose, you **must** pass an array as the first argument. See: ' +
3070
3071
  'https://mongoosejs.com/docs/api.html#model_Model.create');
3071
3072
  }
@@ -27,11 +27,10 @@ function ObjectId(key, options) {
27
27
  const isKeyHexStr = typeof key === 'string' && key.length === 24 && /^[a-f0-9]+$/i.test(key);
28
28
  const suppressWarning = options && options.suppressWarning;
29
29
  if ((isKeyHexStr || typeof key === 'undefined') && !suppressWarning) {
30
- console.warn('mongoose: To create a new ObjectId please try ' +
30
+ utils.warn('mongoose: To create a new ObjectId please try ' +
31
31
  '`Mongoose.Types.ObjectId` instead of using ' +
32
32
  '`Mongoose.Schema.ObjectId`. Set the `suppressWarning` option if ' +
33
33
  'you\'re trying to create a hex char path in your schema.');
34
- console.trace();
35
34
  }
36
35
  SchemaType.call(this, key, options, 'ObjectID');
37
36
  }
package/lib/schema.js CHANGED
@@ -642,7 +642,7 @@ Schema.prototype.path = function(path, obj) {
642
642
  'You are allowed to use it, but use at your own risk. ' +
643
643
  'To disable this warning pass `supressReservedKeysWarning` as a schema option.';
644
644
 
645
- console.warn(errorMessage);
645
+ utils.warn(errorMessage);
646
646
  }
647
647
 
648
648
  if (typeof obj === 'object' && utils.hasUserDefinedProperty(obj, 'ref')) {
@@ -2042,9 +2042,13 @@ Schema.prototype._getSchema = function(path) {
2042
2042
  }
2043
2043
  }
2044
2044
  } else if (foundschema.$isSchemaMap) {
2045
- if (p + 1 >= parts.length) {
2045
+ if (p >= parts.length) {
2046
2046
  return foundschema;
2047
2047
  }
2048
+ // Any path in the map will be an instance of the map's embedded schematype
2049
+ if (p + 1 >= parts.length) {
2050
+ return foundschema.$__schemaType;
2051
+ }
2048
2052
  const ret = search(parts.slice(p + 1), foundschema.$__schemaType.schema);
2049
2053
  return ret;
2050
2054
  }
package/lib/schematype.js CHANGED
@@ -386,6 +386,11 @@ SchemaType.prototype.unique = function(bool) {
386
386
  throw new Error('Path "' + this.path + '" may not have `index` set to ' +
387
387
  'false and `unique` set to true');
388
388
  }
389
+
390
+ if (!this.options.hasOwnProperty('index') && bool === false) {
391
+ return this;
392
+ }
393
+
389
394
  if (this._index == null || this._index === true) {
390
395
  this._index = {};
391
396
  } else if (typeof this._index === 'string') {
@@ -417,6 +422,10 @@ SchemaType.prototype.text = function(bool) {
417
422
  'false and `text` set to true');
418
423
  }
419
424
 
425
+ if (!this.options.hasOwnProperty('index') && bool === false) {
426
+ return this;
427
+ }
428
+
420
429
  if (this._index === null || this._index === undefined ||
421
430
  typeof this._index === 'boolean') {
422
431
  this._index = {};
@@ -450,6 +459,10 @@ SchemaType.prototype.sparse = function(bool) {
450
459
  'false and `sparse` set to true');
451
460
  }
452
461
 
462
+ if (!this.options.hasOwnProperty('index') && bool === false) {
463
+ return this;
464
+ }
465
+
453
466
  if (this._index == null || typeof this._index === 'boolean') {
454
467
  this._index = {};
455
468
  } else if (typeof this._index === 'string') {
@@ -29,7 +29,8 @@ Object.defineProperty(ObjectId.prototype, '_id', {
29
29
  * Convenience `valueOf()` to allow comparing ObjectIds using double equals re: gh-7299
30
30
  */
31
31
 
32
- if (ObjectId.prototype.valueOf === void 0) {
32
+
33
+ if (!ObjectId.prototype.hasOwnProperty('valueOf')) {
33
34
  ObjectId.prototype.valueOf = function objectIdValueOf() {
34
35
  return this.toString();
35
36
  };
@@ -5,6 +5,7 @@ const immediate = require('../helpers/immediate');
5
5
  const internalToObjectOptions = require('../options').internalToObjectOptions;
6
6
  const promiseOrCallback = require('../helpers/promiseOrCallback');
7
7
  const util = require('util');
8
+ const utils = require('../utils');
8
9
 
9
10
  module.exports = Subdocument;
10
11
 
@@ -65,7 +66,7 @@ Subdocument.prototype.save = function(options, fn) {
65
66
  options = options || {};
66
67
 
67
68
  if (!options.suppressWarning) {
68
- console.warn('mongoose: calling `save()` on a subdoc does **not** save ' +
69
+ utils.warn('mongoose: calling `save()` on a subdoc does **not** save ' +
69
70
  'the document to MongoDB, it only runs save middleware. ' +
70
71
  'Use `subdoc.save({ suppressWarning: true })` to hide this warning ' +
71
72
  'if you\'re sure this behavior is right for your app.');
package/lib/utils.js CHANGED
@@ -950,4 +950,12 @@ exports.errorToPOJO = function errorToPOJO(error) {
950
950
  ret[properyName] = error[properyName];
951
951
  }
952
952
  return ret;
953
+ };
954
+
955
+ /*!
956
+ * ignore
957
+ */
958
+
959
+ exports.warn = function warn(message) {
960
+ return process.emitWarning(message, { code: 'MONGOOSE' });
953
961
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.0.7",
4
+ "version": "6.0.8",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",