mongoose 6.2.0 → 6.2.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.
Files changed (41) hide show
  1. package/.eslintrc.json +150 -0
  2. package/CHANGELOG.md +21 -0
  3. package/dist/browser.umd.js +113 -112
  4. package/lib/aggregate.js +1 -1
  5. package/lib/document.js +83 -64
  6. package/lib/helpers/clone.js +40 -27
  7. package/lib/helpers/common.js +2 -2
  8. package/lib/helpers/getFunctionName.js +6 -4
  9. package/lib/helpers/isMongooseObject.js +9 -8
  10. package/lib/helpers/isObject.js +4 -4
  11. package/lib/helpers/path/parentPaths.js +10 -5
  12. package/lib/helpers/populate/assignRawDocsToIdStructure.js +4 -2
  13. package/lib/helpers/populate/assignVals.js +8 -4
  14. package/lib/helpers/populate/getModelsMapForPopulate.js +4 -4
  15. package/lib/helpers/populate/markArraySubdocsPopulated.js +3 -1
  16. package/lib/helpers/populate/modelNamesFromRefPath.js +4 -3
  17. package/lib/helpers/query/castUpdate.js +6 -2
  18. package/lib/helpers/schema/getPath.js +4 -2
  19. package/lib/helpers/timestamps/setupTimestamps.js +3 -8
  20. package/lib/index.js +2 -0
  21. package/lib/internal.js +1 -1
  22. package/lib/model.js +22 -8
  23. package/lib/plugins/trackTransaction.js +4 -3
  24. package/lib/query.js +3 -2
  25. package/lib/queryhelpers.js +1 -1
  26. package/lib/schema/array.js +17 -15
  27. package/lib/schema/documentarray.js +5 -8
  28. package/lib/schema/objectid.js +1 -1
  29. package/lib/schematype.js +29 -26
  30. package/lib/types/ArraySubdocument.js +2 -1
  31. package/lib/types/DocumentArray/index.js +9 -26
  32. package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
  33. package/lib/types/DocumentArray/methods/index.js +15 -3
  34. package/lib/types/array/index.js +21 -20
  35. package/lib/types/array/isMongooseArray.js +5 -0
  36. package/lib/types/array/methods/index.js +12 -12
  37. package/lib/utils.js +7 -0
  38. package/package.json +18 -147
  39. package/tools/repl.js +1 -1
  40. package/tsconfig.json +10 -0
  41. package/{index.d.ts → types/index.d.ts} +84 -75
@@ -27,9 +27,12 @@ const arraySchemaSymbol = require('../../helpers/symbols').arraySchemaSymbol;
27
27
  * @inherits Array
28
28
  * @see http://bit.ly/f6CnZU
29
29
  */
30
+ const _basePush = Array.prototype.push;
31
+ const numberRE = /^\d+$/;
30
32
 
31
33
  function MongooseArray(values, path, doc, schematype) {
32
- let arr;
34
+ let __array;
35
+
33
36
  if (Array.isArray(values)) {
34
37
  const len = values.length;
35
38
 
@@ -38,21 +41,21 @@ function MongooseArray(values, path, doc, schematype) {
38
41
  // modifying the array is faster. Seems small, but adds up when you have a document
39
42
  // with thousands of nested arrays.
40
43
  if (len === 0) {
41
- arr = new Array();
44
+ __array = new Array();
42
45
  } else if (len === 1) {
43
- arr = new Array(1);
44
- arr[0] = values[0];
46
+ __array = new Array(1);
47
+ __array[0] = values[0];
45
48
  } else if (len < 10000) {
46
- arr = new Array();
47
- arr.push.apply(arr, values);
49
+ __array = new Array();
50
+ _basePush.apply(__array, values);
48
51
  } else {
49
- arr = new Array();
52
+ __array = new Array();
50
53
  for (let i = 0; i < len; ++i) {
51
- arr.push(values[i]);
54
+ _basePush.apply(__array, values[i]);
52
55
  }
53
56
  }
54
57
  } else {
55
- arr = [];
58
+ __array = [];
56
59
  }
57
60
 
58
61
  const internals = {
@@ -63,10 +66,10 @@ function MongooseArray(values, path, doc, schematype) {
63
66
  [arrayParentSymbol]: void 0,
64
67
  isMongooseArray: true,
65
68
  isMongooseArrayProxy: true,
66
- __array: arr
69
+ __array: __array
67
70
  };
68
71
 
69
- if (values[arrayAtomicsSymbol] != null) {
72
+ if (values && values[arrayAtomicsSymbol] != null) {
70
73
  internals[arrayAtomicsSymbol] = values[arrayAtomicsSymbol];
71
74
  }
72
75
 
@@ -79,7 +82,7 @@ function MongooseArray(values, path, doc, schematype) {
79
82
  internals[arraySchemaSymbol] = schematype || doc.schema.path(path);
80
83
  }
81
84
 
82
- const proxy = new Proxy(arr, {
85
+ const proxy = new Proxy(__array, {
83
86
  get: function(target, prop) {
84
87
  if (internals.hasOwnProperty(prop)) {
85
88
  return internals[prop];
@@ -88,17 +91,15 @@ function MongooseArray(values, path, doc, schematype) {
88
91
  return mongooseArrayMethods[prop];
89
92
  }
90
93
 
91
- return arr[prop];
94
+ return __array[prop];
92
95
  },
93
- set: function(target, prop, val) {
94
- if (typeof prop === 'string' && /^\d+$/.test(prop)) {
95
- const value = mongooseArrayMethods._cast.call(proxy, val, prop);
96
- arr[prop] = value;
97
- mongooseArrayMethods._markModified.call(proxy, prop);
96
+ set: function(target, prop, value) {
97
+ if (typeof prop === 'string' && numberRE.test(prop)) {
98
+ mongooseArrayMethods.set.call(proxy, prop, value, false);
98
99
  } else if (internals.hasOwnProperty(prop)) {
99
- internals[prop] = val;
100
+ internals[prop] = value;
100
101
  } else {
101
- arr[prop] = val;
102
+ __array[prop] = value;
102
103
  }
103
104
 
104
105
  return true;
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ exports.isMongooseArray = function(mongooseArray) {
4
+ return Array.isArray(mongooseArray) && mongooseArray.isMongooseArray;
5
+ };
@@ -285,7 +285,7 @@ const methods = {
285
285
  return this;
286
286
  }
287
287
 
288
- parent.markModified(dirtyPath, arguments.length > 0 ? elem : parent);
288
+ parent.markModified(dirtyPath, arguments.length !== 0 ? elem : parent);
289
289
  }
290
290
 
291
291
  return this;
@@ -393,8 +393,8 @@ const methods = {
393
393
  type = 'date';
394
394
  }
395
395
 
396
- const rawValues = values.isMongooseArrayProxy ? values.__array : this;
397
- const rawArray = this.isMongooseArrayProxy ? this.__array : this;
396
+ const rawValues = utils.isMongooseArray(values) ? values.__array : this;
397
+ const rawArray = utils.isMongooseArray(this) ? this.__array : this;
398
398
 
399
399
  rawValues.forEach(function(v) {
400
400
  let found;
@@ -520,7 +520,7 @@ const methods = {
520
520
  *
521
521
  * ####Note:
522
522
  *
523
- * _marks the entire array as modified which will pass the entire thing to $set potentially overwritting any changes that happen between when you retrieved the object and when you save it._
523
+ * _marks the entire array as modified which will pass the entire thing to $set potentially overwriting any changes that happen between when you retrieved the object and when you save it._
524
524
  *
525
525
  * @see MongooseArray#$pop #types_array_MongooseArray-%24pop
526
526
  * @api public
@@ -638,7 +638,7 @@ const methods = {
638
638
  let atomic = values;
639
639
  const isOverwrite = values[0] != null &&
640
640
  utils.hasUserDefinedProperty(values[0], '$each');
641
- const arr = this.isMongooseArrayProxy ? this.__array : this;
641
+ const arr = utils.isMongooseArray(this) ? this.__array : this;
642
642
  if (isOverwrite) {
643
643
  atomic = values[0];
644
644
  values = values[0].$each;
@@ -761,7 +761,7 @@ const methods = {
761
761
  */
762
762
 
763
763
  shift() {
764
- const arr = this.isMongooseArrayProxy ? this.__array : this;
764
+ const arr = utils.isMongooseArray(this) ? this.__array : this;
765
765
  const ret = [].shift.call(arr);
766
766
  this._registerAtomic('$set', this);
767
767
  this._markModified();
@@ -782,7 +782,7 @@ const methods = {
782
782
  */
783
783
 
784
784
  sort() {
785
- const arr = this.isMongooseArrayProxy ? this.__array : this;
785
+ const arr = utils.isMongooseArray(this) ? this.__array : this;
786
786
  const ret = [].sort.apply(arr, arguments);
787
787
  this._registerAtomic('$set', this);
788
788
  return ret;
@@ -803,7 +803,7 @@ const methods = {
803
803
 
804
804
  splice() {
805
805
  let ret;
806
- const arr = this.isMongooseArrayProxy ? this.__array : this;
806
+ const arr = utils.isMongooseArray(this) ? this.__array : this;
807
807
 
808
808
  _checkManualPopulation(this, Array.prototype.slice.call(arguments, 2));
809
809
 
@@ -846,7 +846,7 @@ const methods = {
846
846
  */
847
847
 
848
848
  toObject(options) {
849
- const arr = this.isMongooseArrayProxy ? this.__array : this;
849
+ const arr = utils.isMongooseArray(this) ? this.__array : this;
850
850
  if (options && options.depopulate) {
851
851
  options = utils.clone(options);
852
852
  options._isNested = true;
@@ -888,7 +888,7 @@ const methods = {
888
888
  values = this[arraySchemaSymbol].applySetters(values, this[arrayParentSymbol]);
889
889
  }
890
890
 
891
- const arr = this.isMongooseArrayProxy ? this.__array : this;
891
+ const arr = utils.isMongooseArray(this) ? this.__array : this;
892
892
  [].unshift.apply(arr, values);
893
893
  this._registerAtomic('$set', this);
894
894
  this._markModified();
@@ -928,7 +928,7 @@ function _checkManualPopulation(arr, docs) {
928
928
  null :
929
929
  get(arr[arraySchemaSymbol], 'caster.options.ref', null);
930
930
  if (arr.length === 0 &&
931
- docs.length > 0) {
931
+ docs.length !== 0) {
932
932
  if (_isAllSubdocs(docs, ref)) {
933
933
  arr[arrayParentSymbol].$populated(arr[arrayPathSymbol], [], {
934
934
  [populateModelSymbol]: docs[0].constructor
@@ -950,7 +950,7 @@ for (const method of returnVanillaArrayMethods) {
950
950
  }
951
951
 
952
952
  methods[method] = function() {
953
- const _arr = this.isMongooseArrayProxy ? this.__array : this;
953
+ const _arr = utils.isMongooseArray(this) ? this.__array : this;
954
954
  const arr = [].concat(_arr);
955
955
 
956
956
  return arr[method].apply(arr, arguments);
package/lib/utils.js CHANGED
@@ -12,6 +12,8 @@ const PopulateOptions = require('./options/PopulateOptions');
12
12
  const clone = require('./helpers/clone');
13
13
  const immediate = require('./helpers/immediate');
14
14
  const isObject = require('./helpers/isObject');
15
+ const isMongooseArray = require('./types/array/isMongooseArray');
16
+ const isMongooseDocumentArray = require('./types/DocumentArray/isMongooseDocumentArray');
15
17
  const isBsonType = require('./helpers/isBsonType');
16
18
  const getFunctionName = require('./helpers/getFunctionName');
17
19
  const isMongooseObject = require('./helpers/isMongooseObject');
@@ -24,6 +26,11 @@ let Document;
24
26
 
25
27
  exports.specialProperties = specialProperties;
26
28
 
29
+ exports.isMongooseArray = isMongooseArray.isMongooseArray;
30
+ exports.isMongooseDocumentArray = isMongooseDocumentArray.isMongooseDocumentArray;
31
+ exports.registerMongooseArray = isMongooseArray.registerMongooseArray;
32
+ exports.registerMongooseDocumentArray = isMongooseDocumentArray.registerMongooseDocumentArray;
33
+
27
34
  /*!
28
35
  * Produces a collection name from model `name`. By default, just returns
29
36
  * the model name
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.2.0",
4
+ "version": "6.2.1",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -25,7 +25,6 @@
25
25
  "mpath": "0.8.4",
26
26
  "mquery": "4.0.2",
27
27
  "ms": "2.1.2",
28
- "regexp-clone": "1.0.0",
29
28
  "sift": "13.5.2"
30
29
  },
31
30
  "devDependencies": {
@@ -57,6 +56,7 @@
57
56
  "q": "1.5.1",
58
57
  "rimraf": "2.6.3",
59
58
  "serve-handler": "6.1.3",
59
+ "tsd": "0.19.1",
60
60
  "typescript": "4.5.3",
61
61
  "uuid": "8.3.2",
62
62
  "webpack": "4.44.1"
@@ -71,12 +71,14 @@
71
71
  "release": "git pull && git push origin master --tags && npm publish",
72
72
  "release-legacy": "git pull origin 5.x && git push origin 5.x --tags && npm publish --tag legacy",
73
73
  "mongo": "node ./tools/repl.js",
74
- "test": "mocha --exit ./test/*.test.js ./test/typescript/main.test.js",
74
+ "test": "mocha --exit ./test/*.test.js",
75
+ "test-tsd": "tsd",
76
+ "sym-link-mongoose": "npm link && npm link mongoose",
75
77
  "tdd": "mocha ./test/*.test.js ./test/typescript/main.test.js --inspect --watch --recursive --watch-files ./**/*.js",
76
78
  "test-coverage": "nyc --reporter=html --reporter=text npm test"
77
79
  },
78
80
  "main": "./index.js",
79
- "types": "./index.d.ts",
81
+ "types": "./types/index.d.ts",
80
82
  "engines": {
81
83
  "node": ">=12.0.0"
82
84
  },
@@ -97,149 +99,6 @@
97
99
  "test/**/*.js"
98
100
  ]
99
101
  },
100
- "eslintConfig": {
101
- "extends": [
102
- "eslint:recommended"
103
- ],
104
- "overrides": [
105
- {
106
- "files": [
107
- "**/*.{ts,tsx}"
108
- ],
109
- "extends": [
110
- "plugin:@typescript-eslint/eslint-recommended",
111
- "plugin:@typescript-eslint/recommended"
112
- ],
113
- "plugins": [
114
- "@typescript-eslint"
115
- ],
116
- "rules": {
117
- "@typescript-eslint/no-explicit-any": "off",
118
- "@typescript-eslint/ban-types": "off",
119
- "@typescript-eslint/no-unused-vars": "off",
120
- "@typescript-eslint/explicit-module-boundary-types": "off"
121
- }
122
- }
123
- ],
124
- "plugins": [
125
- "mocha-no-only"
126
- ],
127
- "parserOptions": {
128
- "ecmaVersion": 2020
129
- },
130
- "env": {
131
- "node": true,
132
- "es6": true
133
- },
134
- "rules": {
135
- "comma-style": "error",
136
- "indent": [
137
- "error",
138
- 2,
139
- {
140
- "SwitchCase": 1,
141
- "VariableDeclarator": 2
142
- }
143
- ],
144
- "keyword-spacing": "error",
145
- "no-whitespace-before-property": "error",
146
- "no-buffer-constructor": "warn",
147
- "no-console": "off",
148
- "no-constant-condition": "off",
149
- "no-multi-spaces": "error",
150
- "func-call-spacing": "error",
151
- "no-trailing-spaces": "error",
152
- "no-undef": "error",
153
- "no-unneeded-ternary": "error",
154
- "no-const-assign": "error",
155
- "no-useless-rename": "error",
156
- "no-dupe-keys": "error",
157
- "space-in-parens": [
158
- "error",
159
- "never"
160
- ],
161
- "spaced-comment": [
162
- "error",
163
- "always",
164
- {
165
- "block": {
166
- "markers": [
167
- "!"
168
- ],
169
- "balanced": true
170
- }
171
- }
172
- ],
173
- "key-spacing": [
174
- "error",
175
- {
176
- "beforeColon": false,
177
- "afterColon": true
178
- }
179
- ],
180
- "comma-spacing": [
181
- "error",
182
- {
183
- "before": false,
184
- "after": true
185
- }
186
- ],
187
- "array-bracket-spacing": 1,
188
- "arrow-spacing": [
189
- "error",
190
- {
191
- "before": true,
192
- "after": true
193
- }
194
- ],
195
- "object-curly-spacing": [
196
- "error",
197
- "always"
198
- ],
199
- "comma-dangle": [
200
- "error",
201
- "never"
202
- ],
203
- "no-unreachable": "error",
204
- "quotes": [
205
- "error",
206
- "single"
207
- ],
208
- "quote-props": [
209
- "error",
210
- "as-needed"
211
- ],
212
- "semi": "error",
213
- "no-extra-semi": "error",
214
- "semi-spacing": "error",
215
- "no-spaced-func": "error",
216
- "no-throw-literal": "error",
217
- "space-before-blocks": "error",
218
- "space-before-function-paren": [
219
- "error",
220
- "never"
221
- ],
222
- "space-infix-ops": "error",
223
- "space-unary-ops": "error",
224
- "no-var": "warn",
225
- "prefer-const": "warn",
226
- "strict": [
227
- "error",
228
- "global"
229
- ],
230
- "no-restricted-globals": [
231
- "error",
232
- {
233
- "name": "context",
234
- "message": "Don't use Mocha's global context"
235
- }
236
- ],
237
- "no-prototype-builtins": "off",
238
- "mocha-no-only/mocha-no-only": [
239
- "error"
240
- ]
241
- }
242
- },
243
102
  "config": {
244
103
  "mongodbMemoryServer": {
245
104
  "disablePostinstall": true
@@ -248,5 +107,17 @@
248
107
  "funding": {
249
108
  "type": "opencollective",
250
109
  "url": "https://opencollective.com/mongoose"
110
+ },
111
+ "tsd": {
112
+ "directory": "test/types",
113
+ "compilerOptions": {
114
+ "esModuleInterop": false,
115
+ "strict": true,
116
+ "allowSyntheticDefaultImports": true,
117
+ "strictPropertyInitialization": false,
118
+ "noImplicitAny": false,
119
+ "module": "commonjs",
120
+ "target": "ES2017"
121
+ }
251
122
  }
252
123
  }
package/tools/repl.js CHANGED
@@ -11,7 +11,7 @@ async function run () {
11
11
  // Create new instance
12
12
  const replSet = new ReplSet({
13
13
  binary: {
14
- version: process.argv[3]
14
+ version: process.argv[2]
15
15
  },
16
16
  instanceOpts: [
17
17
  // Set the expiry job in MongoDB to run every second
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowSyntheticDefaultImports": true,
4
+ "paths": {
5
+ "mongoose" : ["./types/index.d.ts"]
6
+ }
7
+ },
8
+ "esModuleInterop": true,
9
+ "strictNullChecks": true
10
+ }