mongoose 6.3.4 → 6.3.7

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 (53) hide show
  1. package/.eslintrc.json +1 -1
  2. package/dist/browser.umd.js +1 -1
  3. package/lgtm.yml +12 -0
  4. package/lib/cast/objectid.js +3 -2
  5. package/lib/connection.js +3 -2
  6. package/lib/document.js +37 -24
  7. package/lib/error/index.js +2 -2
  8. package/lib/helpers/clone.js +7 -1
  9. package/lib/helpers/common.js +3 -4
  10. package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +2 -2
  11. package/lib/helpers/isBsonType.js +5 -3
  12. package/lib/helpers/path/setDottedPath.js +14 -1
  13. package/lib/helpers/schematype/handleImmutable.js +2 -1
  14. package/lib/helpers/topology/isAtlas.js +15 -2
  15. package/lib/helpers/update/applyTimestampsToChildren.js +4 -0
  16. package/lib/helpers/update/castArrayFilters.js +3 -0
  17. package/lib/index.js +4 -3
  18. package/lib/options/SchemaTypeOptions.js +13 -0
  19. package/lib/query.js +4 -1
  20. package/lib/schema/SubdocumentPath.js +8 -1
  21. package/lib/schema/decimal128.js +4 -4
  22. package/lib/schema/documentarray.js +2 -4
  23. package/lib/schema/map.js +8 -0
  24. package/lib/schema/objectid.js +4 -3
  25. package/lib/schema/string.js +2 -2
  26. package/lib/schema.js +33 -2
  27. package/lib/schematype.js +1 -0
  28. package/lib/types/DocumentArray/methods/index.js +3 -3
  29. package/lib/types/array/methods/index.js +3 -3
  30. package/lib/types/map.js +4 -4
  31. package/lib/utils.js +3 -4
  32. package/package.json +17 -15
  33. package/types/aggregate.d.ts +3 -4
  34. package/types/callback.d.ts +8 -0
  35. package/types/collection.d.ts +46 -0
  36. package/types/connection.d.ts +91 -71
  37. package/types/document.d.ts +1 -1
  38. package/types/error.d.ts +5 -1
  39. package/types/helpers.d.ts +32 -0
  40. package/types/index.d.ts +32 -1859
  41. package/types/indizes.d.ts +98 -0
  42. package/types/middlewares.d.ts +14 -0
  43. package/types/models.d.ts +419 -0
  44. package/types/populate.d.ts +40 -0
  45. package/types/query.d.ts +637 -0
  46. package/types/schematypes.d.ts +427 -0
  47. package/types/session.d.ts +36 -0
  48. package/types/types.d.ts +102 -0
  49. package/types/utility.d.ts +13 -0
  50. package/types/validation.d.ts +32 -0
  51. package/.lgtm.yml +0 -3
  52. package/CHANGELOG.md +0 -7300
  53. package/History.md +0 -1
@@ -9,6 +9,7 @@ const SchemaType = require('../schematype');
9
9
  const castObjectId = require('../cast/objectid');
10
10
  const getConstructorName = require('../helpers/getConstructorName');
11
11
  const oid = require('../types/objectid');
12
+ const isBsonType = require('../helpers/isBsonType');
12
13
  const utils = require('../utils');
13
14
 
14
15
  const CastError = SchemaType.CastError;
@@ -113,7 +114,7 @@ ObjectId.prototype.auto = function(turnOn) {
113
114
  * ignore
114
115
  */
115
116
 
116
- ObjectId._checkRequired = v => v instanceof oid;
117
+ ObjectId._checkRequired = v => isBsonType(v, 'ObjectID');
117
118
 
118
119
  /*!
119
120
  * ignore
@@ -161,7 +162,7 @@ ObjectId.cast = function cast(caster) {
161
162
  */
162
163
 
163
164
  ObjectId._defaultCaster = v => {
164
- if (!(v instanceof oid)) {
165
+ if (!(isBsonType(v, 'ObjectID'))) {
165
166
  throw new Error(v + ' is not an instance of ObjectId');
166
167
  }
167
168
  return v;
@@ -221,7 +222,7 @@ ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
221
222
  */
222
223
 
223
224
  ObjectId.prototype.cast = function(value, doc, init) {
224
- if (!(value instanceof oid) && SchemaType._isRef(this, value, doc, init)) {
225
+ if (!(isBsonType(value, 'ObjectID')) && SchemaType._isRef(this, value, doc, init)) {
225
226
  // wait! we may need to cast this to a document
226
227
  if ((getConstructorName(value) || '').toLowerCase() === 'objectid') {
227
228
  return new oid(value.toHexString());
@@ -4,12 +4,12 @@
4
4
  * Module dependencies.
5
5
  */
6
6
 
7
- const { BSONRegExp } = require('bson');
8
7
  const SchemaType = require('../schematype');
9
8
  const MongooseError = require('../error/index');
10
9
  const SchemaStringOptions = require('../options/SchemaStringOptions');
11
10
  const castString = require('../cast/string');
12
11
  const utils = require('../utils');
12
+ const isBsonType = require('../helpers/isBsonType');
13
13
 
14
14
  const CastError = SchemaType.CastError;
15
15
 
@@ -677,7 +677,7 @@ SchemaString.prototype.castForQuery = function($conditional, val) {
677
677
  return handler.call(this, val);
678
678
  }
679
679
  val = $conditional;
680
- if (Object.prototype.toString.call(val) === '[object RegExp]' || val instanceof BSONRegExp) {
680
+ if (Object.prototype.toString.call(val) === '[object RegExp]' || isBsonType(val, 'BSONRegExp')) {
681
681
  return val;
682
682
  }
683
683
 
package/lib/schema.js CHANGED
@@ -473,9 +473,39 @@ Schema.prototype.defaultOptions = function(options) {
473
473
  return options;
474
474
  };
475
475
 
476
+ /**
477
+ * Inherit a Schema by applying a discriminator on an existing Schema.
478
+ *
479
+ *
480
+ * ####Example:
481
+ *
482
+ * const options = { discriminatorKey: 'kind' };
483
+ *
484
+ * const eventSchema = new mongoose.Schema({ time: Date }, options);
485
+ * const Event = mongoose.model('Event', eventSchema);
486
+ *
487
+ * // ClickedLinkEvent is a special type of Event that has
488
+ * // a URL.
489
+ * const ClickedLinkEvent = Event.discriminator('ClickedLink',
490
+ * new mongoose.Schema({ url: String }, options));
491
+ *
492
+ * // When you create a generic event, it can't have a URL field...
493
+ * const genericEvent = new Event({ time: Date.now(), url: 'google.com' });
494
+ * assert.ok(!genericEvent.url);
495
+ * // But a ClickedLinkEvent can
496
+ * const clickedEvent = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
497
+ * assert.ok(clickedEvent.url);
498
+ *
499
+ * @param {String} name the name of the discriminator
500
+ * @param {Schema} schema the Schema of the discriminated Schema
501
+ * @return {Schema} the Schema instance
502
+ * @api public
503
+ */
504
+
476
505
  Schema.prototype.discriminator = function(name, schema) {
477
- this._applyDiscriminators = {};
478
- this._applyDiscriminators[name] = schema;
506
+ this._applyDiscriminators = Object.assign(this._applyDiscriminators || {}, { [name]: schema });
507
+
508
+ return this;
479
509
  };
480
510
 
481
511
  /**
@@ -1172,6 +1202,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1172
1202
  }
1173
1203
 
1174
1204
  if (type && type.instanceOfSchema) {
1205
+
1175
1206
  return new MongooseTypes.Subdocument(type, path, obj);
1176
1207
  }
1177
1208
 
package/lib/schematype.js CHANGED
@@ -1181,6 +1181,7 @@ SchemaType.prototype._castNullish = function _castNullish(v) {
1181
1181
 
1182
1182
  SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
1183
1183
  let v = this._applySetters(value, scope, init, priorVal, options);
1184
+
1184
1185
  if (v == null) {
1185
1186
  return this._castNullish(v);
1186
1187
  }
@@ -2,11 +2,11 @@
2
2
 
3
3
  const ArrayMethods = require('../../array/methods');
4
4
  const Document = require('../../../document');
5
- const ObjectId = require('../../objectid');
6
5
  const castObjectId = require('../../../cast/objectid');
7
6
  const getDiscriminatorByValue = require('../../../helpers/discriminator/getDiscriminatorByValue');
8
7
  const internalToObjectOptions = require('../../../options').internalToObjectOptions;
9
8
  const utils = require('../../../utils');
9
+ const isBsonType = require('../../../helpers/isBsonType');
10
10
 
11
11
  const arrayParentSymbol = require('../../../helpers/symbols').arrayParentSymbol;
12
12
  const arrayPathSymbol = require('../../../helpers/symbols').arrayPathSymbol;
@@ -66,7 +66,7 @@ const methods = {
66
66
  // only objects are permitted so we can safely assume that
67
67
  // non-objects are to be interpreted as _id
68
68
  if (Buffer.isBuffer(value) ||
69
- value instanceof ObjectId || !utils.isObject(value)) {
69
+ isBsonType(value, 'ObjectID') || !utils.isObject(value)) {
70
70
  value = { _id: value };
71
71
  }
72
72
 
@@ -134,7 +134,7 @@ const methods = {
134
134
  if (sid == _id._id) {
135
135
  return val;
136
136
  }
137
- } else if (!(id instanceof ObjectId) && !(_id instanceof ObjectId)) {
137
+ } else if (!isBsonType(id, 'ObjectID') && !isBsonType(_id, 'ObjectID')) {
138
138
  if (id == _id || utils.deepEqual(id, _id)) {
139
139
  return val;
140
140
  }
@@ -3,10 +3,10 @@
3
3
  const Document = require('../../../document');
4
4
  const ArraySubdocument = require('../../ArraySubdocument');
5
5
  const MongooseError = require('../../../error/mongooseError');
6
- const ObjectId = require('../../objectid');
7
6
  const cleanModifiedSubpaths = require('../../../helpers/document/cleanModifiedSubpaths');
8
7
  const internalToObjectOptions = require('../../../options').internalToObjectOptions;
9
8
  const utils = require('../../../utils');
9
+ const isBsonType = require('../../../helpers/isBsonType');
10
10
 
11
11
  const arrayAtomicsSymbol = require('../../../helpers/symbols').arrayAtomicsSymbol;
12
12
  const arrayParentSymbol = require('../../../helpers/symbols').arrayParentSymbol;
@@ -227,7 +227,7 @@ const methods = {
227
227
  // only objects are permitted so we can safely assume that
228
228
  // non-objects are to be interpreted as _id
229
229
  if (Buffer.isBuffer(value) ||
230
- value instanceof ObjectId || !utils.isObject(value)) {
230
+ isBsonType(value, 'ObjectID') || !utils.isObject(value)) {
231
231
  value = { _id: value };
232
232
  }
233
233
 
@@ -469,7 +469,7 @@ const methods = {
469
469
  */
470
470
 
471
471
  indexOf(obj, fromIndex) {
472
- if (obj instanceof ObjectId) {
472
+ if (isBsonType(obj, 'ObjectID')) {
473
473
  obj = obj.toString();
474
474
  }
475
475
 
package/lib/types/map.js CHANGED
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  const Mixed = require('../schema/mixed');
4
- const ObjectId = require('./objectid');
5
4
  const clone = require('../helpers/clone');
6
5
  const deepEqual = require('../utils').deepEqual;
7
6
  const getConstructorName = require('../helpers/getConstructorName');
8
7
  const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
9
8
  const util = require('util');
10
9
  const specialProperties = require('../helpers/specialProperties');
10
+ const isBsonType = require('../helpers/isBsonType');
11
11
 
12
12
  const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
13
13
 
@@ -43,7 +43,7 @@ class MongooseMap extends Map {
43
43
  }
44
44
 
45
45
  get(key, options) {
46
- if (key instanceof ObjectId) {
46
+ if (isBsonType(key, 'ObjectID')) {
47
47
  key = key.toString();
48
48
  }
49
49
 
@@ -55,7 +55,7 @@ class MongooseMap extends Map {
55
55
  }
56
56
 
57
57
  set(key, value) {
58
- if (key instanceof ObjectId) {
58
+ if (isBsonType(key, 'ObjectID')) {
59
59
  key = key.toString();
60
60
  }
61
61
 
@@ -117,7 +117,7 @@ class MongooseMap extends Map {
117
117
  }
118
118
 
119
119
  delete(key) {
120
- if (key instanceof ObjectId) {
120
+ if (isBsonType(key, 'ObjectID')) {
121
121
  key = key.toString();
122
122
  }
123
123
 
package/lib/utils.js CHANGED
@@ -6,7 +6,6 @@
6
6
 
7
7
  const ms = require('ms');
8
8
  const mpath = require('mpath');
9
- const Decimal = require('./types/decimal128');
10
9
  const ObjectId = require('./types/objectid');
11
10
  const PopulateOptions = require('./options/PopulateOptions');
12
11
  const clone = require('./helpers/clone');
@@ -307,7 +306,7 @@ exports.merge = function merge(to, from, options, path) {
307
306
  to[key] = from[key].clone();
308
307
  }
309
308
  continue;
310
- } else if (from[key] instanceof ObjectId) {
309
+ } else if (isBsonType(from[key], 'ObjectID')) {
311
310
  to[key] = new ObjectId(from[key]);
312
311
  continue;
313
312
  }
@@ -481,7 +480,7 @@ exports.tick = function tick(callback) {
481
480
  */
482
481
 
483
482
  exports.isMongooseType = function(v) {
484
- return v instanceof ObjectId || v instanceof Decimal || v instanceof Buffer;
483
+ return isBsonType(v, 'ObjectID') || isBsonType(v, 'Decimal128') || v instanceof Buffer;
485
484
  };
486
485
 
487
486
  exports.isMongooseObject = isMongooseObject;
@@ -795,7 +794,7 @@ exports.array.unique = function(arr) {
795
794
  }
796
795
  ret.push(item);
797
796
  primitives.add(item);
798
- } else if (item instanceof ObjectId) {
797
+ } else if (isBsonType(item, 'ObjectID')) {
799
798
  if (ids.has(item.toString())) {
800
799
  continue;
801
800
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.3.4",
4
+ "version": "6.3.7",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -28,10 +28,9 @@
28
28
  "sift": "16.0.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@babel/core": "7.17.10",
32
- "@babel/preset-env": "7.17.10",
33
- "@typescript-eslint/eslint-plugin": "5.21.0",
34
- "@typescript-eslint/parser": "5.21.0",
31
+ "@babel/core": "7.18.2",
32
+ "@typescript-eslint/eslint-plugin": "5.27.0",
33
+ "@typescript-eslint/parser": "5.27.0",
35
34
  "acquit": "1.2.1",
36
35
  "acquit-ignore": "0.2.0",
37
36
  "acquit-require": "0.1.1",
@@ -41,30 +40,31 @@
41
40
  "benchmark": "2.1.4",
42
41
  "bluebird": "3.7.2",
43
42
  "buffer": "^5.6.0",
44
- "cheerio": "1.0.0-rc.10",
43
+ "cheerio": "1.0.0-rc.11",
45
44
  "crypto-browserify": "3.12.0",
46
45
  "dox": "0.3.1",
47
- "eslint": "8.14.0",
46
+ "eslint": "8.16.0",
48
47
  "eslint-plugin-mocha-no-only": "1.1.1",
49
48
  "highlight.js": "11.5.1",
50
49
  "lodash.isequal": "4.5.0",
51
50
  "lodash.isequalwith": "4.4.0",
52
- "marked": "4.0.14",
51
+ "marked": "4.0.16",
53
52
  "mkdirp": "^1.0.4",
54
53
  "mocha": "10.0.0",
55
54
  "moment": "2.x",
56
- "mongodb-memory-server": "8.5.2",
55
+ "mongodb-memory-server": "8.6.0",
57
56
  "ncp": "^2.0.0",
58
57
  "nyc": "15.1.0",
59
58
  "pug": "3.0.2",
60
59
  "q": "1.5.1",
61
60
  "serve-handler": "6.1.3",
62
- "sinon": "13.0.2",
61
+ "sinon": "14.0.0",
63
62
  "stream-browserify": "3.0.0",
63
+ "ts-benchmark": "^1.0.2",
64
64
  "tsd": "0.20.0",
65
- "typescript": "4.6.4",
65
+ "typescript": "4.7.2",
66
66
  "uuid": "8.3.2",
67
- "webpack": "5.72.0"
67
+ "webpack": "5.72.1"
68
68
  },
69
69
  "directories": {
70
70
  "lib": "./lib/mongoose"
@@ -96,8 +96,10 @@
96
96
  "test": "mocha --exit ./test/*.test.js",
97
97
  "test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
98
98
  "test-tsd": "node ./test/types/check-types-filename && tsd",
99
- "tdd": "mocha ./test/*.test.js ./test/typescript/main.test.js --inspect --watch --recursive --watch-files ./**/*.js",
100
- "test-coverage": "nyc --reporter=html --reporter=text npm test"
99
+ "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
100
+ "test-coverage": "nyc --reporter=html --reporter=text npm test",
101
+ "ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17 18 29 32",
102
+ "ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17 18 29 32 -b master"
101
103
  },
102
104
  "main": "./index.js",
103
105
  "types": "./types/index.d.ts",
@@ -143,4 +145,4 @@
143
145
  "target": "ES2017"
144
146
  }
145
147
  }
146
- }
148
+ }
@@ -1,7 +1,8 @@
1
1
  declare module 'mongoose' {
2
2
  import mongodb = require('mongodb');
3
3
 
4
- interface AggregateOptions {
4
+ interface AggregateOptions extends
5
+ SessionOption {
5
6
  /**
6
7
  * If true, the MongoDB server will use the hard drive to store data during this aggregation.
7
8
  */
@@ -56,8 +57,6 @@ declare module 'mongoose' {
56
57
  * The preferred read preference.
57
58
  */
58
59
  readPreference?: mongodb.ReadPreferenceLike;
59
- /** The ClientSession for this aggregation */
60
- session?: mongodb.ClientSession;
61
60
  /**
62
61
  * Specifies the write concern.
63
62
  */
@@ -65,7 +64,7 @@ declare module 'mongoose' {
65
64
  [key: string]: any;
66
65
  }
67
66
 
68
- class Aggregate<R> {
67
+ class Aggregate<R> implements SessionOperation {
69
68
  /**
70
69
  * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js
71
70
  * You do not need to call this function explicitly, the JavaScript runtime
@@ -0,0 +1,8 @@
1
+ declare module 'mongoose' {
2
+ type CallbackError = NativeError | null;
3
+
4
+ type Callback<T = any> = (error: CallbackError, result: T) => void;
5
+
6
+ type CallbackWithoutResult = (error: CallbackError) => void;
7
+ type CallbackWithoutResultAndOptionalError = (error?: CallbackError) => void;
8
+ }
@@ -0,0 +1,46 @@
1
+ declare module 'mongoose' {
2
+ import mongodb = require('mongodb');
3
+
4
+ /*
5
+ * section collection.js
6
+ * http://mongoosejs.com/docs/api.html#collection-js
7
+ */
8
+ interface CollectionBase<T extends mongodb.Document> extends mongodb.Collection<T> {
9
+ /*
10
+ * Abstract methods. Some of these are already defined on the
11
+ * mongodb.Collection interface so they've been commented out.
12
+ */
13
+ ensureIndex(...args: any[]): any;
14
+ findAndModify(...args: any[]): any;
15
+ getIndexes(...args: any[]): any;
16
+
17
+ /** The collection name */
18
+ collectionName: string;
19
+ /** The Connection instance */
20
+ conn: Connection;
21
+ /** The collection name */
22
+ name: string;
23
+ }
24
+
25
+ /*
26
+ * section drivers/node-mongodb-native/collection.js
27
+ * http://mongoosejs.com/docs/api.html#drivers-node-mongodb-native-collection-js
28
+ */
29
+ interface Collection<T extends mongodb.Document = mongodb.Document> extends CollectionBase<T> {
30
+ /**
31
+ * Collection constructor
32
+ * @param name name of the collection
33
+ * @param conn A MongooseConnection instance
34
+ * @param opts optional collection options
35
+ */
36
+ // eslint-disable-next-line @typescript-eslint/no-misused-new
37
+ new(name: string, conn: Connection, opts?: any): Collection<T>;
38
+ /** Formatter for debug print args */
39
+ $format(arg: any, color?: boolean, shell?: boolean): string;
40
+ /** Debug print helper */
41
+ $print(name: string, i: string | number, args: any[], color?: boolean, shell?: boolean): void;
42
+ /** Retrieves information about this collections indexes. */
43
+ getIndexes(): ReturnType<mongodb.Collection<T>['indexInformation']>;
44
+ }
45
+ let Collection: Collection;
46
+ }