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.
- package/.eslintrc.json +150 -0
- package/CHANGELOG.md +21 -0
- package/dist/browser.umd.js +113 -112
- package/lib/aggregate.js +1 -1
- package/lib/document.js +83 -64
- package/lib/helpers/clone.js +40 -27
- package/lib/helpers/common.js +2 -2
- package/lib/helpers/getFunctionName.js +6 -4
- package/lib/helpers/isMongooseObject.js +9 -8
- package/lib/helpers/isObject.js +4 -4
- package/lib/helpers/path/parentPaths.js +10 -5
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +4 -2
- package/lib/helpers/populate/assignVals.js +8 -4
- package/lib/helpers/populate/getModelsMapForPopulate.js +4 -4
- package/lib/helpers/populate/markArraySubdocsPopulated.js +3 -1
- package/lib/helpers/populate/modelNamesFromRefPath.js +4 -3
- package/lib/helpers/query/castUpdate.js +6 -2
- package/lib/helpers/schema/getPath.js +4 -2
- package/lib/helpers/timestamps/setupTimestamps.js +3 -8
- package/lib/index.js +2 -0
- package/lib/internal.js +1 -1
- package/lib/model.js +22 -8
- package/lib/plugins/trackTransaction.js +4 -3
- package/lib/query.js +3 -2
- package/lib/queryhelpers.js +1 -1
- package/lib/schema/array.js +17 -15
- package/lib/schema/documentarray.js +5 -8
- package/lib/schema/objectid.js +1 -1
- package/lib/schematype.js +29 -26
- package/lib/types/ArraySubdocument.js +2 -1
- package/lib/types/DocumentArray/index.js +9 -26
- package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/DocumentArray/methods/index.js +15 -3
- package/lib/types/array/index.js +21 -20
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +12 -12
- package/lib/utils.js +7 -0
- package/package.json +18 -147
- package/tools/repl.js +1 -1
- package/tsconfig.json +10 -0
- package/{index.d.ts → types/index.d.ts} +84 -75
package/lib/types/array/index.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
44
|
+
__array = new Array();
|
|
42
45
|
} else if (len === 1) {
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
__array = new Array(1);
|
|
47
|
+
__array[0] = values[0];
|
|
45
48
|
} else if (len < 10000) {
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
__array = new Array();
|
|
50
|
+
_basePush.apply(__array, values);
|
|
48
51
|
} else {
|
|
49
|
-
|
|
52
|
+
__array = new Array();
|
|
50
53
|
for (let i = 0; i < len; ++i) {
|
|
51
|
-
|
|
54
|
+
_basePush.apply(__array, values[i]);
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
} else {
|
|
55
|
-
|
|
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:
|
|
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(
|
|
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
|
|
94
|
+
return __array[prop];
|
|
92
95
|
},
|
|
93
|
-
set: function(target, prop,
|
|
94
|
-
if (typeof prop === 'string' &&
|
|
95
|
-
|
|
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] =
|
|
100
|
+
internals[prop] = value;
|
|
100
101
|
} else {
|
|
101
|
-
|
|
102
|
+
__array[prop] = value;
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
return true;
|
|
@@ -285,7 +285,7 @@ const methods = {
|
|
|
285
285
|
return this;
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
-
parent.markModified(dirtyPath, arguments.length
|
|
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
|
|
397
|
-
const rawArray = 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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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