mongoose 6.2.9 → 6.3.0

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 (64) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/browser.umd.js +2 -1693
  3. package/lib/aggregate.js +59 -67
  4. package/lib/browser.js +4 -4
  5. package/lib/connection.js +21 -21
  6. package/lib/cursor/AggregationCursor.js +2 -2
  7. package/lib/cursor/ChangeStream.js +42 -2
  8. package/lib/cursor/QueryCursor.js +5 -3
  9. package/lib/document.js +39 -46
  10. package/lib/error/eachAsyncMultiError.js +41 -0
  11. package/lib/error/index.js +2 -2
  12. package/lib/helpers/cursor/eachAsync.js +44 -12
  13. package/lib/helpers/indexes/applySchemaCollation.js +13 -0
  14. package/lib/helpers/indexes/isTextIndex.js +16 -0
  15. package/lib/helpers/model/discriminator.js +1 -3
  16. package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
  17. package/lib/helpers/projection/hasIncludedChildren.js +1 -1
  18. package/lib/helpers/query/applyGlobalOption.js +29 -0
  19. package/lib/helpers/query/castUpdate.js +3 -1
  20. package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
  21. package/lib/helpers/update/applyTimestampsToUpdate.js +0 -1
  22. package/lib/index.js +33 -26
  23. package/lib/model.js +88 -90
  24. package/lib/options/SchemaArrayOptions.js +2 -2
  25. package/lib/options/SchemaBufferOptions.js +1 -1
  26. package/lib/options/SchemaDateOptions.js +2 -2
  27. package/lib/options/SchemaDocumentArrayOptions.js +3 -3
  28. package/lib/options/SchemaMapOptions.js +2 -2
  29. package/lib/options/SchemaNumberOptions.js +3 -3
  30. package/lib/options/SchemaObjectIdOptions.js +2 -2
  31. package/lib/options/SchemaStringOptions.js +1 -1
  32. package/lib/options/SchemaSubdocumentOptions.js +2 -2
  33. package/lib/options/SchemaTypeOptions.js +3 -3
  34. package/lib/query.js +273 -249
  35. package/lib/schema/SubdocumentPath.js +4 -3
  36. package/lib/schema/array.js +2 -2
  37. package/lib/schema/boolean.js +4 -4
  38. package/lib/schema/buffer.js +3 -3
  39. package/lib/schema/date.js +7 -7
  40. package/lib/schema/decimal128.js +2 -2
  41. package/lib/schema/documentarray.js +3 -3
  42. package/lib/schema/mixed.js +2 -2
  43. package/lib/schema/number.js +6 -6
  44. package/lib/schema/objectid.js +4 -7
  45. package/lib/schema/string.js +38 -16
  46. package/lib/schema.js +144 -30
  47. package/lib/schematype.js +75 -68
  48. package/lib/types/ArraySubdocument.js +1 -1
  49. package/lib/types/DocumentArray/methods/index.js +2 -2
  50. package/lib/types/array/index.js +1 -1
  51. package/lib/types/array/methods/index.js +13 -13
  52. package/lib/types/buffer.js +1 -1
  53. package/lib/types/decimal128.js +1 -1
  54. package/lib/types/objectid.js +1 -1
  55. package/lib/types/subdocument.js +31 -2
  56. package/lib/validoptions.js +1 -0
  57. package/lib/virtualtype.js +3 -3
  58. package/package.json +19 -13
  59. package/tools/repl.js +2 -1
  60. package/types/aggregate.d.ts +223 -0
  61. package/types/cursor.d.ts +10 -4
  62. package/types/index.d.ts +194 -209
  63. package/types/mongooseoptions.d.ts +10 -4
  64. package/lib/helpers/query/applyGlobalMaxTimeMS.js +0 -15
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const utils = require('../../utils');
4
+
5
+ function applyGlobalMaxTimeMS(options, model) {
6
+ applyGlobalOption(options, model, 'maxTimeMS');
7
+ }
8
+
9
+ function applyGlobalDiskUse(options, model) {
10
+ applyGlobalOption(options, model, 'allowDiskUse');
11
+ }
12
+
13
+ module.exports = {
14
+ applyGlobalMaxTimeMS,
15
+ applyGlobalDiskUse
16
+ };
17
+
18
+
19
+ function applyGlobalOption(options, model, optionName) {
20
+ if (utils.hasUserDefinedProperty(options, optionName)) {
21
+ return;
22
+ }
23
+
24
+ if (utils.hasUserDefinedProperty(model.db.options, optionName)) {
25
+ options[optionName] = model.db.options[optionName];
26
+ } else if (utils.hasUserDefinedProperty(model.base.options, optionName)) {
27
+ options[optionName] = model.base.options[optionName];
28
+ }
29
+ }
@@ -47,7 +47,7 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
47
47
  } else if (!options.overwriteDiscriminatorKey) {
48
48
  delete obj[schema.options.discriminatorKey];
49
49
  }
50
- if (options.upsert) {
50
+ if (options.upsert && !options.overwrite) {
51
51
  moveImmutableProperties(schema, obj, context);
52
52
  }
53
53
 
@@ -217,6 +217,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
217
217
  }
218
218
 
219
219
  if (op !== '$setOnInsert' &&
220
+ !options.overwrite &&
220
221
  handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
221
222
  continue;
222
223
  }
@@ -311,6 +312,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
311
312
 
312
313
  // You can use `$setOnInsert` with immutable keys
313
314
  if (op !== '$setOnInsert' &&
315
+ !options.overwrite &&
314
316
  handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
315
317
  continue;
316
318
  }
@@ -15,7 +15,7 @@ function applyTimestampsToChildren(now, update, schema) {
15
15
  }
16
16
 
17
17
  const keys = Object.keys(update);
18
- const hasDollarKey = keys.some(key => key.startsWith('$'));
18
+ const hasDollarKey = keys.some(key => key[0] === '$');
19
19
 
20
20
  if (hasDollarKey) {
21
21
  if (update.$push) {
@@ -38,7 +38,7 @@ function applyTimestampsToChildren(now, update, schema) {
38
38
  }
39
39
  }
40
40
 
41
- const updateKeys = Object.keys(update).filter(key => !key.startsWith('$'));
41
+ const updateKeys = Object.keys(update).filter(key => key[0] !== '$');
42
42
  for (const key of updateKeys) {
43
43
  applyTimestampsToUpdateKey(schema, key, update, now);
44
44
  }
@@ -80,7 +80,6 @@ function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, optio
80
80
  }
81
81
 
82
82
  if (!skipCreatedAt && createdAt) {
83
-
84
83
  if (currentUpdate[createdAt]) {
85
84
  delete currentUpdate[createdAt];
86
85
  }
package/lib/index.js CHANGED
@@ -47,7 +47,7 @@ const objectIdHexRegexp = /^[0-9A-Fa-f]{24}$/;
47
47
  * The exports object of the `mongoose` module is an instance of this class.
48
48
  * Most apps will only use this one instance.
49
49
  *
50
- * ####Example:
50
+ * #### Example:
51
51
  * const mongoose = require('mongoose');
52
52
  * mongoose instanceof mongoose.Mongoose; // true
53
53
  *
@@ -144,7 +144,7 @@ Mongoose.prototype.driver = driver;
144
144
  /**
145
145
  * Sets mongoose options
146
146
  *
147
- * ####Example:
147
+ * #### Example:
148
148
  *
149
149
  * mongoose.set('test', value) // sets the 'test' option to `value`
150
150
  *
@@ -179,7 +179,9 @@ Mongoose.prototype.driver = driver;
179
179
  Mongoose.prototype.set = function(key, value) {
180
180
  const _mongoose = this instanceof Mongoose ? this : mongoose;
181
181
 
182
- if (VALID_OPTIONS.indexOf(key) === -1) throw new Error(`\`${key}\` is an invalid option.`);
182
+ if (VALID_OPTIONS.indexOf(key) === -1) {
183
+ throw new Error(`\`${key}\` is an invalid option.`);
184
+ }
183
185
 
184
186
  if (arguments.length === 1) {
185
187
  return _mongoose.options[key];
@@ -207,7 +209,7 @@ Mongoose.prototype.set = function(key, value) {
207
209
  /**
208
210
  * Gets mongoose options
209
211
  *
210
- * ####Example:
212
+ * #### Example:
211
213
  *
212
214
  * mongoose.get('test') // returns the 'test' value
213
215
  *
@@ -226,7 +228,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
226
228
  *
227
229
  * _Options passed take precedence over options included in connection strings._
228
230
  *
229
- * ####Example:
231
+ * #### Example:
230
232
  *
231
233
  * // with mongodb:// URI
232
234
  * db = mongoose.createConnection('mongodb://user:pass@localhost:port/database');
@@ -290,7 +292,7 @@ Mongoose.prototype.createConnection = function(uri, options, callback) {
290
292
  /**
291
293
  * Opens the default mongoose connection.
292
294
  *
293
- * ####Example:
295
+ * #### Example:
294
296
  *
295
297
  * mongoose.connect('mongodb://user:pass@localhost:port/database');
296
298
  *
@@ -424,7 +426,7 @@ Mongoose.prototype.pluralize = function(fn) {
424
426
  * you will get an `OverwriteModelError`. If you call `mongoose.model()` with
425
427
  * the same name and same schema, you'll get the same schema back.
426
428
  *
427
- * ####Example:
429
+ * #### Example:
428
430
  *
429
431
  * const mongoose = require('mongoose');
430
432
  *
@@ -446,7 +448,7 @@ Mongoose.prototype.pluralize = function(fn) {
446
448
  *
447
449
  * _When no `collection` argument is passed, Mongoose uses the model name. If you don't like this behavior, either pass a collection name, use `mongoose.pluralize()`, or set your schemas collection name option._
448
450
  *
449
- * ####Example:
451
+ * #### Example:
450
452
  *
451
453
  * const schema = new Schema({ name: String }, { collection: 'actor' });
452
454
  *
@@ -467,6 +469,7 @@ Mongoose.prototype.pluralize = function(fn) {
467
469
  */
468
470
 
469
471
  Mongoose.prototype.model = function(name, schema, collection, options) {
472
+
470
473
  const _mongoose = this instanceof Mongoose ? this : mongoose;
471
474
 
472
475
  if (typeof schema === 'string') {
@@ -519,7 +522,6 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
519
522
  }
520
523
 
521
524
  const model = _mongoose._model(name, schema, collection, options);
522
-
523
525
  _mongoose.connection.models[name] = model;
524
526
  _mongoose.models[name] = model;
525
527
 
@@ -561,12 +563,17 @@ Mongoose.prototype._model = function(name, schema, collection, options) {
561
563
 
562
564
  const connection = options.connection || _mongoose.connection;
563
565
  model = _mongoose.Model.compile(model || name, schema, collection, connection, _mongoose);
564
-
565
566
  // Errors handled internally, so safe to ignore error
566
567
  model.init(function $modelInitNoop() {});
567
568
 
568
569
  connection.emit('model', model);
569
570
 
571
+ if (schema._applyDiscriminators != null) {
572
+ for (const disc of Object.keys(schema._applyDiscriminators)) {
573
+ model.discriminator(disc, schema._applyDiscriminators[disc]);
574
+ }
575
+ }
576
+
570
577
  return model;
571
578
  };
572
579
 
@@ -577,7 +584,7 @@ Mongoose.prototype._model = function(name, schema, collection, options) {
577
584
  *
578
585
  * Equivalent to `mongoose.connection.deleteModel(name)`.
579
586
  *
580
- * ####Example:
587
+ * #### Example:
581
588
  *
582
589
  * mongoose.model('User', new Schema({ name: String }));
583
590
  * console.log(mongoose.model('User')); // Model object
@@ -605,7 +612,7 @@ Mongoose.prototype.deleteModel = function(name) {
605
612
  /**
606
613
  * Returns an array of model names created on this instance of Mongoose.
607
614
  *
608
- * ####Note:
615
+ * #### Note:
609
616
  *
610
617
  * _Does not include names of models created using `connection.model()`._
611
618
  *
@@ -658,7 +665,7 @@ Mongoose.prototype.plugin = function(fn, opts) {
658
665
  /**
659
666
  * The Mongoose module's default connection. Equivalent to `mongoose.connections[0]`, see [`connections`](#mongoose_Mongoose-connections).
660
667
  *
661
- * ####Example:
668
+ * #### Example:
662
669
  *
663
670
  * const mongoose = require('mongoose');
664
671
  * mongoose.connect(...);
@@ -691,7 +698,7 @@ Mongoose.prototype.__defineSetter__('connection', function(v) {
691
698
  * [`createConnection()`](#mongoose_Mongoose-createConnection) adds a connection
692
699
  * to this array.
693
700
  *
694
- * ####Example:
701
+ * #### Example:
695
702
  *
696
703
  * const mongoose = require('mongoose');
697
704
  * mongoose.connections.length; // 1, just the default connection
@@ -767,7 +774,7 @@ Mongoose.prototype.version = pkg.version;
767
774
  *
768
775
  * The exports of the mongoose module is an instance of this class.
769
776
  *
770
- * ####Example:
777
+ * #### Example:
771
778
  *
772
779
  * const mongoose = require('mongoose');
773
780
  * const mongoose2 = new mongoose.Mongoose();
@@ -781,7 +788,7 @@ Mongoose.prototype.Mongoose = Mongoose;
781
788
  /**
782
789
  * The Mongoose [Schema](#schema_Schema) constructor
783
790
  *
784
- * ####Example:
791
+ * #### Example:
785
792
  *
786
793
  * const mongoose = require('mongoose');
787
794
  * const Schema = mongoose.Schema;
@@ -805,7 +812,7 @@ Mongoose.prototype.SchemaType = SchemaType;
805
812
  /**
806
813
  * The various Mongoose SchemaTypes.
807
814
  *
808
- * ####Note:
815
+ * #### Note:
809
816
  *
810
817
  * _Alias of mongoose.Schema.Types for backwards compatibility._
811
818
  *
@@ -828,12 +835,12 @@ Mongoose.prototype.VirtualType = VirtualType;
828
835
  /**
829
836
  * The various Mongoose Types.
830
837
  *
831
- * ####Example:
838
+ * #### Example:
832
839
  *
833
840
  * const mongoose = require('mongoose');
834
841
  * const array = mongoose.Types.Array;
835
842
  *
836
- * ####Types:
843
+ * #### Types:
837
844
  *
838
845
  * - [Array](/docs/schematypes.html#arrays)
839
846
  * - [Buffer](/docs/schematypes.html#buffers)
@@ -926,7 +933,7 @@ Mongoose.prototype.DocumentProvider = require('./document_provider');
926
933
  * Do not use this to create a new ObjectId instance, use `mongoose.Types.ObjectId`
927
934
  * instead.
928
935
  *
929
- * ####Example:
936
+ * #### Example:
930
937
  *
931
938
  * const childSchema = new Schema({ parentId: mongoose.ObjectId });
932
939
  *
@@ -940,7 +947,7 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
940
947
  * Returns true if Mongoose can cast the given value to an ObjectId, or
941
948
  * false otherwise.
942
949
  *
943
- * ####Example:
950
+ * #### Example:
944
951
  *
945
952
  * mongoose.isValidObjectId(new mongoose.Types.ObjectId()); // true
946
953
  * mongoose.isValidObjectId('0123456789ab'); // true
@@ -971,7 +978,7 @@ Mongoose.prototype.isValidObjectId = function(v) {
971
978
  * `isObjectIdOrHexString()` returns true only for `ObjectId` instances or 24 character hex
972
979
  * strings, and will return false for numbers, documents, and strings of length 12.
973
980
  *
974
- * ####Example:
981
+ * #### Example:
975
982
  *
976
983
  * mongoose.isObjectIdOrHexString(new mongoose.Types.ObjectId()); // true
977
984
  * mongoose.isObjectIdOrHexString('62261a65d66c6be0a63c051f'); // true
@@ -1012,7 +1019,7 @@ Mongoose.prototype.syncIndexes = function(options) {
1012
1019
  * Do not use this to create a new Decimal128 instance, use `mongoose.Types.Decimal128`
1013
1020
  * instead.
1014
1021
  *
1015
- * ####Example:
1022
+ * #### Example:
1016
1023
  *
1017
1024
  * const vehicleSchema = new Schema({ fuelLevel: mongoose.Decimal128 });
1018
1025
  *
@@ -1027,7 +1034,7 @@ Mongoose.prototype.Decimal128 = SchemaTypes.Decimal128;
1027
1034
  * declaring paths in your schema that Mongoose's change tracking, casting,
1028
1035
  * and validation should ignore.
1029
1036
  *
1030
- * ####Example:
1037
+ * #### Example:
1031
1038
  *
1032
1039
  * const schema = new Schema({ arbitrary: mongoose.Mixed });
1033
1040
  *
@@ -1040,7 +1047,7 @@ Mongoose.prototype.Mixed = SchemaTypes.Mixed;
1040
1047
  /**
1041
1048
  * The Mongoose Date [SchemaType](/docs/schematypes.html).
1042
1049
  *
1043
- * ####Example:
1050
+ * #### Example:
1044
1051
  *
1045
1052
  * const schema = new Schema({ test: Date });
1046
1053
  * schema.path('test') instanceof mongoose.Date; // true
@@ -1055,7 +1062,7 @@ Mongoose.prototype.Date = SchemaTypes.Date;
1055
1062
  * The Mongoose Number [SchemaType](/docs/schematypes.html). Used for
1056
1063
  * declaring paths in your schema that Mongoose should cast to numbers.
1057
1064
  *
1058
- * ####Example:
1065
+ * #### Example:
1059
1066
  *
1060
1067
  * const schema = new Schema({ num: mongoose.Number });
1061
1068
  * // Equivalent to: