mongoose 6.0.4 → 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.
Files changed (37) hide show
  1. package/dist/browser.umd.js +301 -284
  2. package/index.d.ts +138 -108
  3. package/lib/aggregate.js +20 -25
  4. package/lib/cast.js +6 -2
  5. package/lib/connection.js +3 -5
  6. package/lib/document.js +62 -29
  7. package/lib/drivers/node-mongodb-native/collection.js +1 -1
  8. package/lib/error/objectExpected.js +1 -1
  9. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +15 -12
  10. package/lib/helpers/model/applyMethods.js +2 -1
  11. package/lib/helpers/populate/assignVals.js +3 -0
  12. package/lib/helpers/populate/createPopulateQueryFilter.js +3 -2
  13. package/lib/helpers/populate/lookupLocalFields.js +3 -0
  14. package/lib/helpers/printJestWarning.js +4 -2
  15. package/lib/{plugins → helpers/schema}/idGetter.js +0 -0
  16. package/lib/helpers/setDefaultsOnInsert.js +10 -29
  17. package/lib/helpers/timestamps/setupTimestamps.js +2 -2
  18. package/lib/helpers/update/castArrayFilters.js +17 -5
  19. package/lib/helpers/update/removeUnusedArrayFilters.js +21 -7
  20. package/lib/helpers/update/updatedPathsByArrayFilter.js +3 -0
  21. package/lib/index.js +0 -2
  22. package/lib/internal.js +21 -19
  23. package/lib/model.js +11 -3
  24. package/lib/schema/SubdocumentPath.js +2 -0
  25. package/lib/schema/documentarray.js +2 -0
  26. package/lib/schema/number.js +11 -4
  27. package/lib/schema/objectid.js +1 -2
  28. package/lib/schema.js +16 -2
  29. package/lib/schematype.js +15 -1
  30. package/lib/types/ArraySubdocument.js +13 -13
  31. package/lib/types/array/index.js +1 -1
  32. package/lib/types/map.js +1 -1
  33. package/lib/types/objectid.js +2 -1
  34. package/lib/types/subdocument.js +8 -2
  35. package/lib/utils.js +8 -0
  36. package/package.json +22 -20
  37. package/tools/repl.js +0 -1
package/lib/model.js CHANGED
@@ -323,7 +323,6 @@ Model.prototype.$__handleSave = function(options, callback) {
323
323
  }
324
324
 
325
325
  _applyCustomWhere(this, where);
326
-
327
326
  this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, (err, ret) => {
328
327
  if (err) {
329
328
  this.$__undoReset();
@@ -1674,7 +1673,7 @@ function _ensureIndexes(model, options, callback) {
1674
1673
 
1675
1674
  for (const index of indexes) {
1676
1675
  if (isDefaultIdIndex(index)) {
1677
- console.warn('mongoose: Cannot specify a custom index on `_id` for ' +
1676
+ utils.warn('mongoose: Cannot specify a custom index on `_id` for ' +
1678
1677
  'model name "' + model.modelName + '", ' +
1679
1678
  'MongoDB does not allow overwriting the default `_id` index. See ' +
1680
1679
  'http://bit.ly/mongodb-id-index');
@@ -1877,6 +1876,7 @@ Model.translateAliases = function translateAliases(fields) {
1877
1876
  // Alias found,
1878
1877
  translated.push(alias);
1879
1878
  } else {
1879
+ alias = name;
1880
1880
  // Alias not found, so treat as un-aliased key
1881
1881
  translated.push(name);
1882
1882
  }
@@ -1942,6 +1942,8 @@ Model.translateAliases = function translateAliases(fields) {
1942
1942
  * To remove just the first document that matches `conditions`, set the `single`
1943
1943
  * option to true.
1944
1944
  *
1945
+ * This method is deprecated. See [Deprecation Warnings](../deprecations.html#remove) for details.
1946
+ *
1945
1947
  * ####Example:
1946
1948
  *
1947
1949
  * const res = await Character.remove({ name: 'Eddard Stark' });
@@ -1953,6 +1955,7 @@ Model.translateAliases = function translateAliases(fields) {
1953
1955
  * are involved. Because no Mongoose documents are involved, Mongoose does
1954
1956
  * not execute [document middleware](/docs/middleware.html#types-of-middleware).
1955
1957
  *
1958
+ * @deprecated
1956
1959
  * @param {Object} conditions
1957
1960
  * @param {Object} [options]
1958
1961
  * @param {Session} [options.session=null] the [session](https://docs.mongodb.com/manual/reference/server-sessions/) associated with this operation.
@@ -3063,7 +3066,7 @@ Model.create = function create(doc, options, callback) {
3063
3066
  !this.schema.path('session')) {
3064
3067
  // Probably means the user is running into the common mistake of trying
3065
3068
  // to use a spread to specify options, see gh-7535
3066
- console.warn('WARNING: to pass a `session` to `Model.create()` in ' +
3069
+ utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +
3067
3070
  'Mongoose, you **must** pass an array as the first argument. See: ' +
3068
3071
  'https://mongoosejs.com/docs/api.html#model_Model.create');
3069
3072
  }
@@ -3752,6 +3755,8 @@ Model.hydrate = function(obj, projection) {
3752
3755
  *
3753
3756
  * - `update()`
3754
3757
  *
3758
+ * This method is deprecated. See [Deprecation Warnings](../deprecations.html#update) for details.
3759
+ *
3755
3760
  * ####Examples:
3756
3761
  *
3757
3762
  * MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
@@ -3799,6 +3804,7 @@ Model.hydrate = function(obj, projection) {
3799
3804
  *
3800
3805
  * Be careful to not use an existing model instance for the update clause (this won't work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a "Mod on _id not allowed" error.
3801
3806
  *
3807
+ * @deprecated
3802
3808
  * @see strict http://mongoosejs.com/docs/guide.html#strict
3803
3809
  * @see response http://docs.mongodb.org/v2.6/reference/command/update/#output
3804
3810
  * @param {Object} filter
@@ -4777,6 +4783,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
4777
4783
  model[modelSymbol] = true;
4778
4784
  model.events = new EventEmitter();
4779
4785
 
4786
+ schema._preCompile();
4787
+
4780
4788
  model.prototype.$__setSchema(schema);
4781
4789
 
4782
4790
  const _userProvidedOptions = schema._userProvidedOptions || {};
@@ -82,6 +82,8 @@ function _createConstructor(schema, baseClass) {
82
82
  }
83
83
  };
84
84
 
85
+ schema._preCompile();
86
+
85
87
  const proto = baseClass != null ? baseClass.prototype : Subdocument.prototype;
86
88
  _embedded.prototype = Object.create(proto);
87
89
  _embedded.prototype.$__setSchema(schema);
@@ -117,6 +117,8 @@ function _createConstructor(schema, options, baseClass) {
117
117
  this.$session(this.ownerDocument().$session());
118
118
  }
119
119
 
120
+ schema._preCompile();
121
+
120
122
  const proto = baseClass != null ? baseClass.prototype : Subdocument.prototype;
121
123
  EmbeddedDocument.prototype = Object.create(proto);
122
124
  EmbeddedDocument.prototype.$__setSchema(schema);
@@ -315,12 +315,19 @@ SchemaNumber.prototype.enum = function(values, message) {
315
315
 
316
316
 
317
317
  if (!Array.isArray(values)) {
318
- if (utils.isObject(values)) {
319
- values = utils.object.vals(values);
320
- } else {
318
+ const isObjectSyntax = utils.isPOJO(values) && values.values != null;
319
+ if (isObjectSyntax) {
320
+ message = values.message;
321
+ values = values.values;
322
+ } else if (typeof values === 'number') {
321
323
  values = Array.prototype.slice.call(arguments);
324
+ message = null;
322
325
  }
323
- message = MongooseError.messages.Number.enum;
326
+
327
+ if (utils.isPOJO(values)) {
328
+ values = Object.values(values);
329
+ }
330
+ message = message || MongooseError.messages.Number.enum;
324
331
  }
325
332
 
326
333
  message = message == null ? MongooseError.messages.Number.enum : message;
@@ -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
@@ -15,6 +15,7 @@ const addAutoId = require('./helpers/schema/addAutoId');
15
15
  const get = require('./helpers/get');
16
16
  const getConstructorName = require('./helpers/getConstructorName');
17
17
  const getIndexes = require('./helpers/schema/getIndexes');
18
+ const idGetter = require('./helpers/schema/idGetter');
18
19
  const merge = require('./helpers/schema/merge');
19
20
  const mpath = require('mpath');
20
21
  const readPref = require('./driver').get().ReadPreference;
@@ -641,7 +642,7 @@ Schema.prototype.path = function(path, obj) {
641
642
  'You are allowed to use it, but use at your own risk. ' +
642
643
  'To disable this warning pass `supressReservedKeysWarning` as a schema option.';
643
644
 
644
- console.warn(errorMessage);
645
+ utils.warn(errorMessage);
645
646
  }
646
647
 
647
648
  if (typeof obj === 'object' && utils.hasUserDefinedProperty(obj, 'ref')) {
@@ -2041,9 +2042,13 @@ Schema.prototype._getSchema = function(path) {
2041
2042
  }
2042
2043
  }
2043
2044
  } else if (foundschema.$isSchemaMap) {
2044
- if (p + 1 >= parts.length) {
2045
+ if (p >= parts.length) {
2045
2046
  return foundschema;
2046
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
+ }
2047
2052
  const ret = search(parts.slice(p + 1), foundschema.$__schemaType.schema);
2048
2053
  return ret;
2049
2054
  }
@@ -2135,6 +2140,15 @@ function isArrayFilter(piece) {
2135
2140
  return piece.startsWith('$[') && piece.endsWith(']');
2136
2141
  }
2137
2142
 
2143
+ /*!
2144
+ * Called by `compile()` _right before_ compiling. Good for making any changes to
2145
+ * the schema that should respect options set by plugins, like `id`
2146
+ */
2147
+
2148
+ Schema.prototype._preCompile = function _preCompile() {
2149
+ idGetter(this);
2150
+ };
2151
+
2138
2152
  /*!
2139
2153
  * Module exports.
2140
2154
  */
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') {
@@ -1384,8 +1397,9 @@ SchemaType._isRef = function(self, value, doc, init) {
1384
1397
  // - this populated with adhoc model and no ref was set in schema OR
1385
1398
  // - setting / pushing values after population
1386
1399
  const path = doc.$__fullPath(self.path, true);
1400
+
1387
1401
  const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
1388
- ref = owner.$populated(path) || doc.$populated(self.path);
1402
+ ref = (path != null && owner.$populated(path)) || doc.$populated(self.path);
1389
1403
  }
1390
1404
 
1391
1405
  if (ref) {
@@ -31,29 +31,29 @@ function ArraySubdocument(obj, parentArr, skipId, fields, index) {
31
31
  this[documentArrayParent] = undefined;
32
32
  }
33
33
  this.$setIndex(index);
34
- this.$isDocumentArrayElement = true;
35
34
  this.$__parent = this[documentArrayParent];
36
35
 
37
- // Document.call(this, obj, fields, skipId);
38
36
  Subdocument.call(this, obj, fields, this[documentArrayParent], void 0, { isNew: true });
39
- this.$isSingleNested = false;
40
-
41
- const _this = this;
42
- this.$on('isNew', function(val) {
43
- _this.isNew = val;
44
- });
45
-
46
- _this.$on('save', function() {
47
- _this.constructor.emit('save', _this);
48
- });
49
37
  }
50
38
 
51
39
  /*!
52
- * Inherit from Document
40
+ * Inherit from Subdocument
53
41
  */
54
42
  ArraySubdocument.prototype = Object.create(Subdocument.prototype);
55
43
  ArraySubdocument.prototype.constructor = ArraySubdocument;
56
44
 
45
+ Object.defineProperty(ArraySubdocument.prototype, '$isSingleNested', {
46
+ configurable: false,
47
+ writable: false,
48
+ value: false
49
+ });
50
+
51
+ Object.defineProperty(ArraySubdocument.prototype, '$isDocumentArrayElement', {
52
+ configurable: false,
53
+ writable: false,
54
+ value: true
55
+ });
56
+
57
57
  for (const i in EventEmitter.prototype) {
58
58
  ArraySubdocument[i] = EventEmitter.prototype[i];
59
59
  }
@@ -35,7 +35,7 @@ function MongooseArray(values, path, doc, schematype) {
35
35
  [arrayAtomicsSymbol]: {},
36
36
  [arrayAtomicsBackupSymbol]: void 0,
37
37
  [arrayPathSymbol]: path,
38
- [arraySchemaSymbol]: void 0,
38
+ [arraySchemaSymbol]: schematype,
39
39
  [arrayParentSymbol]: void 0,
40
40
  isMongooseArray: true,
41
41
  isMongooseArrayProxy: true,
package/lib/types/map.js CHANGED
@@ -135,7 +135,7 @@ class MongooseMap extends Map {
135
135
  const ret = {};
136
136
  const keys = this.keys();
137
137
  for (const key of keys) {
138
- ret[key] = clone(this.get(key));
138
+ ret[key] = clone(this.get(key), options);
139
139
  }
140
140
  return ret;
141
141
  }
@@ -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
 
@@ -16,7 +17,6 @@ module.exports = Subdocument;
16
17
  */
17
18
 
18
19
  function Subdocument(value, fields, parent, skipId, options) {
19
- this.$isSingleNested = true;
20
20
  if (parent != null) {
21
21
  // If setting a nested path, should copy isNew from parent re: gh-7048
22
22
  const parentOptions = { isNew: parent.isNew, defaults: parent.$__.$options.defaults };
@@ -32,6 +32,12 @@ function Subdocument(value, fields, parent, skipId, options) {
32
32
 
33
33
  Subdocument.prototype = Object.create(Document.prototype);
34
34
 
35
+ Object.defineProperty(Subdocument.prototype, '$isSingleNested', {
36
+ configurable: false,
37
+ writable: false,
38
+ value: true
39
+ });
40
+
35
41
  /*!
36
42
  * ignore
37
43
  */
@@ -60,7 +66,7 @@ Subdocument.prototype.save = function(options, fn) {
60
66
  options = options || {};
61
67
 
62
68
  if (!options.suppressWarning) {
63
- console.warn('mongoose: calling `save()` on a subdoc does **not** save ' +
69
+ utils.warn('mongoose: calling `save()` on a subdoc does **not** save ' +
64
70
  'the document to MongoDB, it only runs save middleware. ' +
65
71
  'Use `subdoc.save({ suppressWarning: true })` to hide this warning ' +
66
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.4",
4
+ "version": "6.0.8",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -73,7 +73,7 @@
73
73
  "build-browser": "node build-browser.js",
74
74
  "prepublishOnly": "npm run build-browser",
75
75
  "release": "git pull && git push origin master --tags && npm publish",
76
- "release-legacy": "git pull origin 4.x && git push origin 4.x --tags && npm publish --tag legacy",
76
+ "release-legacy": "git pull origin 5.x && git push origin 5.x --tags && npm publish --tag legacy",
77
77
  "test": "mocha --exit ./test/*.test.js ./test/typescript/main.test.js",
78
78
  "tdd": "mocha ./test/*.test.js ./test/typescript/main.test.js --inspect --watch --recursive --watch-files ./**/*.js",
79
79
  "test-cov": "nyc --reporter=html --reporter=text npm test"
@@ -104,24 +104,26 @@
104
104
  "extends": [
105
105
  "eslint:recommended"
106
106
  ],
107
- "overrides": [{
108
- "files": [
109
- "**/*.{ts,tsx}"
110
- ],
111
- "extends": [
112
- "plugin:@typescript-eslint/eslint-recommended",
113
- "plugin:@typescript-eslint/recommended"
114
- ],
115
- "plugins": [
116
- "@typescript-eslint"
117
- ],
118
- "rules": {
119
- "@typescript-eslint/no-explicit-any": "off",
120
- "@typescript-eslint/ban-types": "off",
121
- "@typescript-eslint/no-unused-vars": "off",
122
- "@typescript-eslint/explicit-module-boundary-types": "off"
107
+ "overrides": [
108
+ {
109
+ "files": [
110
+ "**/*.{ts,tsx}"
111
+ ],
112
+ "extends": [
113
+ "plugin:@typescript-eslint/eslint-recommended",
114
+ "plugin:@typescript-eslint/recommended"
115
+ ],
116
+ "plugins": [
117
+ "@typescript-eslint"
118
+ ],
119
+ "rules": {
120
+ "@typescript-eslint/no-explicit-any": "off",
121
+ "@typescript-eslint/ban-types": "off",
122
+ "@typescript-eslint/no-unused-vars": "off",
123
+ "@typescript-eslint/explicit-module-boundary-types": "off"
124
+ }
123
125
  }
124
- }],
126
+ ],
125
127
  "plugins": [
126
128
  "mocha-no-only"
127
129
  ],
@@ -245,4 +247,4 @@
245
247
  "type": "opencollective",
246
248
  "url": "https://opencollective.com/mongoose"
247
249
  }
248
- }
250
+ }
package/tools/repl.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const co = require('co');
4
3
  run().catch(error => {
5
4
  console.error(error);
6
5
  process.exit(-1);