mongoose 8.19.4 → 9.0.0-rc0
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/eslint.config.mjs +198 -0
- package/lib/aggregate.js +17 -73
- package/lib/cast/bigint.js +1 -1
- package/lib/cast/double.js +1 -1
- package/lib/cast/uuid.js +5 -48
- package/lib/cast.js +3 -3
- package/lib/connection.js +0 -1
- package/lib/cursor/aggregationCursor.js +14 -24
- package/lib/cursor/queryCursor.js +7 -14
- package/lib/document.js +125 -121
- package/lib/drivers/node-mongodb-native/connection.js +3 -10
- package/lib/error/divergentArray.js +2 -2
- package/lib/error/objectParameter.js +1 -2
- package/lib/error/validation.js +0 -8
- package/lib/helpers/clone.js +1 -1
- package/lib/helpers/common.js +1 -1
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +10 -0
- package/lib/helpers/indexes/isIndexEqual.js +0 -1
- package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
- package/lib/helpers/model/applyHooks.js +43 -53
- package/lib/helpers/model/applyMethods.js +2 -2
- package/lib/helpers/model/applyStaticHooks.js +1 -48
- package/lib/helpers/model/castBulkWrite.js +1 -1
- package/lib/helpers/parallelLimit.js +18 -36
- package/lib/helpers/pluralize.js +3 -3
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
- package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
- package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
- package/lib/helpers/populate/getSchemaTypes.js +5 -5
- package/lib/helpers/query/cast$expr.js +8 -10
- package/lib/helpers/query/castFilterPath.js +1 -1
- package/lib/helpers/query/castUpdate.js +15 -13
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
- package/lib/helpers/schema/applyPlugins.js +1 -1
- package/lib/helpers/schema/getIndexes.js +1 -7
- package/lib/helpers/timestamps/setupTimestamps.js +3 -6
- package/lib/helpers/updateValidators.js +57 -111
- package/lib/model.js +419 -607
- package/lib/mongoose.js +41 -13
- package/lib/plugins/saveSubdocs.js +24 -51
- package/lib/plugins/sharding.js +5 -4
- package/lib/plugins/validateBeforeSave.js +3 -13
- package/lib/query.js +101 -145
- package/lib/queryHelpers.js +2 -2
- package/lib/schema/array.js +46 -84
- package/lib/schema/bigint.js +4 -2
- package/lib/schema/boolean.js +4 -2
- package/lib/schema/buffer.js +4 -2
- package/lib/schema/date.js +4 -2
- package/lib/schema/decimal128.js +4 -2
- package/lib/schema/documentArray.js +66 -91
- package/lib/schema/documentArrayElement.js +18 -11
- package/lib/schema/double.js +4 -2
- package/lib/schema/int32.js +4 -2
- package/lib/schema/map.js +87 -6
- package/lib/schema/mixed.js +4 -2
- package/lib/schema/number.js +4 -2
- package/lib/schema/objectId.js +4 -2
- package/lib/schema/string.js +5 -3
- package/lib/schema/subdocument.js +27 -31
- package/lib/schema/union.js +11 -3
- package/lib/schema/uuid.js +4 -23
- package/lib/schema.js +91 -91
- package/lib/schemaType.js +67 -59
- package/lib/types/array/index.js +2 -2
- package/lib/types/array/methods/index.js +4 -4
- package/lib/types/arraySubdocument.js +1 -1
- package/lib/types/buffer.js +10 -10
- package/lib/types/decimal128.js +1 -1
- package/lib/types/documentArray/index.js +1 -1
- package/lib/types/documentArray/methods/index.js +32 -18
- package/lib/types/double.js +1 -1
- package/lib/types/map.js +1 -2
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +15 -43
- package/lib/types/uuid.js +1 -1
- package/lib/utils.js +1 -8
- package/lib/validOptions.js +3 -3
- package/package.json +11 -24
- package/types/connection.d.ts +20 -11
- package/types/document.d.ts +96 -27
- package/types/index.d.ts +143 -39
- package/types/inferhydrateddoctype.d.ts +115 -0
- package/types/inferrawdoctype.d.ts +99 -75
- package/types/inferschematype.d.ts +17 -3
- package/types/middlewares.d.ts +0 -2
- package/types/models.d.ts +131 -199
- package/types/mongooseoptions.d.ts +6 -5
- package/types/pipelinestage.d.ts +1 -1
- package/types/query.d.ts +71 -139
- package/types/schemaoptions.d.ts +1 -1
- package/types/schematypes.d.ts +14 -10
- package/types/types.d.ts +3 -4
- package/types/utility.d.ts +68 -48
- package/types/validation.d.ts +18 -14
- package/browser.js +0 -8
- package/dist/browser.umd.js +0 -2
- package/lib/browser.js +0 -141
- package/lib/browserDocument.js +0 -101
- package/lib/documentProvider.js +0 -30
- package/lib/drivers/browser/binary.js +0 -14
- package/lib/drivers/browser/decimal128.js +0 -7
- package/lib/drivers/browser/index.js +0 -13
- package/lib/drivers/browser/objectid.js +0 -29
- package/lib/helpers/promiseOrCallback.js +0 -54
|
@@ -20,7 +20,7 @@ const flatten = require('./common').flatten;
|
|
|
20
20
|
* @api private
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
module.exports = function(query, schema, castedDoc, options
|
|
23
|
+
module.exports = async function updateValidators(query, schema, castedDoc, options) {
|
|
24
24
|
const keys = Object.keys(castedDoc || {});
|
|
25
25
|
let updatedKeys = {};
|
|
26
26
|
let updatedValues = {};
|
|
@@ -30,9 +30,8 @@ module.exports = function(query, schema, castedDoc, options, callback) {
|
|
|
30
30
|
let hasDollarUpdate = false;
|
|
31
31
|
let currentUpdate;
|
|
32
32
|
let key;
|
|
33
|
-
let i;
|
|
34
33
|
|
|
35
|
-
for (i = 0; i < numKeys; ++i) {
|
|
34
|
+
for (let i = 0; i < numKeys; ++i) {
|
|
36
35
|
if (keys[i].startsWith('$')) {
|
|
37
36
|
hasDollarUpdate = true;
|
|
38
37
|
if (keys[i] === '$push' || keys[i] === '$addToSet') {
|
|
@@ -85,163 +84,110 @@ module.exports = function(query, schema, castedDoc, options, callback) {
|
|
|
85
84
|
const alreadyValidated = [];
|
|
86
85
|
|
|
87
86
|
const context = query;
|
|
88
|
-
|
|
87
|
+
for (let i = 0; i < numUpdates; ++i) {
|
|
88
|
+
const v = updatedValues[updates[i]];
|
|
89
89
|
const schemaPath = schema._getSchema(updates[i]);
|
|
90
90
|
if (schemaPath == null) {
|
|
91
|
-
|
|
91
|
+
continue;
|
|
92
92
|
}
|
|
93
93
|
if (schemaPath.instance === 'Mixed' && schemaPath.path !== updates[i]) {
|
|
94
|
-
|
|
94
|
+
continue;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
if (v && Array.isArray(v.$in)) {
|
|
98
98
|
v.$in.forEach((v, i) => {
|
|
99
|
-
validatorsToExecute.push(
|
|
100
|
-
schemaPath.doValidate(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
validationErrors.push(err);
|
|
106
|
-
}
|
|
107
|
-
callback(null);
|
|
108
|
-
},
|
|
109
|
-
context,
|
|
110
|
-
{ updateValidator: true });
|
|
111
|
-
});
|
|
99
|
+
validatorsToExecute.push(
|
|
100
|
+
schemaPath.doValidate(v, context, { updateValidator: true }).catch(err => {
|
|
101
|
+
err.path = updates[i] + '.$in.' + i;
|
|
102
|
+
validationErrors.push(err);
|
|
103
|
+
})
|
|
104
|
+
);
|
|
112
105
|
});
|
|
113
106
|
} else {
|
|
114
107
|
if (isPull[updates[i]] &&
|
|
115
108
|
schemaPath.$isMongooseArray) {
|
|
116
|
-
|
|
109
|
+
continue;
|
|
117
110
|
}
|
|
118
111
|
|
|
119
112
|
if (schemaPath.$isMongooseDocumentArrayElement && v != null && v.$__ != null) {
|
|
120
113
|
alreadyValidated.push(updates[i]);
|
|
121
|
-
validatorsToExecute.push(
|
|
122
|
-
schemaPath.doValidate(v,
|
|
123
|
-
if (err) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
validationErrors.push(_err);
|
|
129
|
-
}
|
|
130
|
-
} else {
|
|
131
|
-
err.path = updates[i];
|
|
132
|
-
validationErrors.push(err);
|
|
114
|
+
validatorsToExecute.push(
|
|
115
|
+
schemaPath.doValidate(v, context, { updateValidator: true }).catch(err => {
|
|
116
|
+
if (err.errors) {
|
|
117
|
+
for (const key of Object.keys(err.errors)) {
|
|
118
|
+
const _err = err.errors[key];
|
|
119
|
+
_err.path = updates[i] + '.' + key;
|
|
120
|
+
validationErrors.push(_err);
|
|
133
121
|
}
|
|
122
|
+
} else {
|
|
123
|
+
err.path = updates[i];
|
|
124
|
+
validationErrors.push(err);
|
|
134
125
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}, context, { updateValidator: true });
|
|
138
|
-
});
|
|
126
|
+
})
|
|
127
|
+
);
|
|
139
128
|
} else {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
schemaPath.doValidate(v, function(err) {
|
|
129
|
+
const isAlreadyValidated = alreadyValidated.find(path => updates[i].startsWith(path + '.'));
|
|
130
|
+
if (isAlreadyValidated) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (schemaPath.$isSingleNested) {
|
|
134
|
+
alreadyValidated.push(updates[i]);
|
|
135
|
+
}
|
|
136
|
+
validatorsToExecute.push(
|
|
137
|
+
schemaPath.doValidate(v, context, { updateValidator: true }).catch(err => {
|
|
151
138
|
if (schemaPath.schema != null &&
|
|
152
139
|
schemaPath.schema.options.storeSubdocValidationError === false &&
|
|
153
140
|
err instanceof ValidationError) {
|
|
154
|
-
return
|
|
141
|
+
return;
|
|
155
142
|
}
|
|
156
143
|
|
|
157
144
|
if (err) {
|
|
158
145
|
err.path = updates[i];
|
|
159
146
|
validationErrors.push(err);
|
|
160
147
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
});
|
|
148
|
+
})
|
|
149
|
+
);
|
|
164
150
|
}
|
|
165
151
|
}
|
|
166
152
|
}
|
|
167
|
-
for (i = 0; i < numUpdates; ++i) {
|
|
168
|
-
iter(i, updatedValues[updates[i]]);
|
|
169
|
-
}
|
|
170
153
|
|
|
171
154
|
const arrayUpdates = Object.keys(arrayAtomicUpdates);
|
|
172
155
|
for (const arrayUpdate of arrayUpdates) {
|
|
173
156
|
let schemaPath = schema._getSchema(arrayUpdate);
|
|
174
157
|
if (schemaPath && schemaPath.$isMongooseDocumentArray) {
|
|
175
|
-
validatorsToExecute.push(
|
|
158
|
+
validatorsToExecute.push(
|
|
176
159
|
schemaPath.doValidate(
|
|
177
160
|
arrayAtomicUpdates[arrayUpdate],
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
161
|
+
options && options.context === 'query' ? query : null
|
|
162
|
+
).catch(err => {
|
|
163
|
+
err.path = arrayUpdate;
|
|
164
|
+
validationErrors.push(err);
|
|
165
|
+
})
|
|
166
|
+
);
|
|
181
167
|
} else {
|
|
182
168
|
schemaPath = schema._getSchema(arrayUpdate + '.0');
|
|
183
169
|
for (const atomicUpdate of arrayAtomicUpdates[arrayUpdate]) {
|
|
184
|
-
validatorsToExecute.push(
|
|
170
|
+
validatorsToExecute.push(
|
|
185
171
|
schemaPath.doValidate(
|
|
186
172
|
atomicUpdate,
|
|
187
|
-
getValidationCallback(arrayUpdate, validationErrors, callback),
|
|
188
173
|
options && options.context === 'query' ? query : null,
|
|
189
|
-
{ updateValidator: true }
|
|
190
|
-
|
|
174
|
+
{ updateValidator: true }
|
|
175
|
+
).catch(err => {
|
|
176
|
+
err.path = arrayUpdate;
|
|
177
|
+
validationErrors.push(err);
|
|
178
|
+
})
|
|
179
|
+
);
|
|
191
180
|
}
|
|
192
181
|
}
|
|
193
182
|
}
|
|
194
183
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return _done(callback);
|
|
199
|
-
}
|
|
200
|
-
for (const validator of validatorsToExecute) {
|
|
201
|
-
validator(function() {
|
|
202
|
-
if (--numValidators <= 0) {
|
|
203
|
-
_done(callback);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return function(callback) {
|
|
212
|
-
let numValidators = validatorsToExecute.length;
|
|
213
|
-
if (numValidators === 0) {
|
|
214
|
-
return _done(callback);
|
|
215
|
-
}
|
|
216
|
-
for (const validator of validatorsToExecute) {
|
|
217
|
-
validator(function() {
|
|
218
|
-
if (--numValidators <= 0) {
|
|
219
|
-
_done(callback);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
function _done(callback) {
|
|
226
|
-
if (validationErrors.length) {
|
|
227
|
-
const err = new ValidationError(null);
|
|
228
|
-
|
|
229
|
-
for (const validationError of validationErrors) {
|
|
230
|
-
err.addError(validationError.path, validationError);
|
|
231
|
-
}
|
|
184
|
+
await Promise.all(validatorsToExecute);
|
|
185
|
+
if (validationErrors.length) {
|
|
186
|
+
const err = new ValidationError(null);
|
|
232
187
|
|
|
233
|
-
|
|
188
|
+
for (const validationError of validationErrors) {
|
|
189
|
+
err.addError(validationError.path, validationError);
|
|
234
190
|
}
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function getValidationCallback(arrayUpdate, validationErrors, callback) {
|
|
239
|
-
return function(err) {
|
|
240
|
-
if (err) {
|
|
241
|
-
err.path = arrayUpdate;
|
|
242
|
-
validationErrors.push(err);
|
|
243
|
-
}
|
|
244
|
-
callback(null);
|
|
245
|
-
};
|
|
191
|
+
throw err;
|
|
246
192
|
}
|
|
247
193
|
};
|