mongoose 8.20.0 → 9.0.0-rc1

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 (90) hide show
  1. package/eslint.config.mjs +198 -0
  2. package/lib/aggregate.js +17 -73
  3. package/lib/cast/bigint.js +1 -1
  4. package/lib/cast/double.js +1 -1
  5. package/lib/cast/uuid.js +5 -48
  6. package/lib/cast.js +2 -2
  7. package/lib/connection.js +0 -1
  8. package/lib/cursor/aggregationCursor.js +14 -24
  9. package/lib/cursor/queryCursor.js +7 -14
  10. package/lib/document.js +125 -121
  11. package/lib/drivers/node-mongodb-native/connection.js +3 -10
  12. package/lib/error/objectParameter.js +1 -2
  13. package/lib/error/validation.js +0 -8
  14. package/lib/helpers/clone.js +1 -1
  15. package/lib/helpers/common.js +1 -1
  16. package/lib/helpers/indexes/isIndexEqual.js +0 -1
  17. package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
  18. package/lib/helpers/model/applyHooks.js +43 -53
  19. package/lib/helpers/model/applyMethods.js +2 -2
  20. package/lib/helpers/model/applyStaticHooks.js +1 -48
  21. package/lib/helpers/model/castBulkWrite.js +1 -1
  22. package/lib/helpers/parallelLimit.js +18 -36
  23. package/lib/helpers/pluralize.js +3 -3
  24. package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
  25. package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
  26. package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
  27. package/lib/helpers/populate/getSchemaTypes.js +5 -5
  28. package/lib/helpers/query/cast$expr.js +8 -10
  29. package/lib/helpers/query/castFilterPath.js +1 -1
  30. package/lib/helpers/query/castUpdate.js +14 -12
  31. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
  32. package/lib/helpers/schema/applyPlugins.js +1 -1
  33. package/lib/helpers/schema/getIndexes.js +1 -7
  34. package/lib/helpers/timestamps/setupTimestamps.js +3 -6
  35. package/lib/helpers/updateValidators.js +57 -111
  36. package/lib/model.js +419 -607
  37. package/lib/mongoose.js +41 -13
  38. package/lib/plugins/saveSubdocs.js +24 -51
  39. package/lib/plugins/sharding.js +5 -4
  40. package/lib/plugins/validateBeforeSave.js +3 -13
  41. package/lib/query.js +101 -145
  42. package/lib/queryHelpers.js +2 -2
  43. package/lib/schema/array.js +41 -84
  44. package/lib/schema/documentArray.js +57 -94
  45. package/lib/schema/documentArrayElement.js +16 -11
  46. package/lib/schema/string.js +1 -1
  47. package/lib/schema/subdocument.js +22 -28
  48. package/lib/schema/uuid.js +0 -21
  49. package/lib/schema.js +81 -39
  50. package/lib/schemaType.js +39 -57
  51. package/lib/types/array/index.js +2 -2
  52. package/lib/types/array/methods/index.js +4 -4
  53. package/lib/types/arraySubdocument.js +1 -1
  54. package/lib/types/buffer.js +10 -10
  55. package/lib/types/decimal128.js +1 -1
  56. package/lib/types/documentArray/index.js +1 -1
  57. package/lib/types/documentArray/methods/index.js +5 -3
  58. package/lib/types/double.js +1 -1
  59. package/lib/types/objectid.js +1 -1
  60. package/lib/types/subdocument.js +15 -43
  61. package/lib/types/uuid.js +1 -1
  62. package/lib/utils.js +1 -8
  63. package/lib/validOptions.js +3 -3
  64. package/package.json +11 -24
  65. package/types/connection.d.ts +20 -11
  66. package/types/document.d.ts +95 -26
  67. package/types/index.d.ts +143 -39
  68. package/types/inferhydrateddoctype.d.ts +115 -0
  69. package/types/inferrawdoctype.d.ts +99 -75
  70. package/types/inferschematype.d.ts +17 -3
  71. package/types/middlewares.d.ts +0 -2
  72. package/types/models.d.ts +131 -199
  73. package/types/mongooseoptions.d.ts +6 -5
  74. package/types/pipelinestage.d.ts +1 -1
  75. package/types/query.d.ts +71 -139
  76. package/types/schemaoptions.d.ts +1 -1
  77. package/types/schematypes.d.ts +14 -10
  78. package/types/types.d.ts +3 -4
  79. package/types/utility.d.ts +68 -48
  80. package/types/validation.d.ts +18 -14
  81. package/browser.js +0 -8
  82. package/dist/browser.umd.js +0 -2
  83. package/lib/browser.js +0 -141
  84. package/lib/browserDocument.js +0 -101
  85. package/lib/documentProvider.js +0 -30
  86. package/lib/drivers/browser/binary.js +0 -14
  87. package/lib/drivers/browser/decimal128.js +0 -7
  88. package/lib/drivers/browser/index.js +0 -13
  89. package/lib/drivers/browser/objectid.js +0 -29
  90. 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, callback) {
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
- function iter(i, v) {
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
- return;
91
+ continue;
92
92
  }
93
93
  if (schemaPath.instance === 'Mixed' && schemaPath.path !== updates[i]) {
94
- return;
94
+ continue;
95
95
  }
96
96
 
97
97
  if (v && Array.isArray(v.$in)) {
98
98
  v.$in.forEach((v, i) => {
99
- validatorsToExecute.push(function(callback) {
100
- schemaPath.doValidate(
101
- v,
102
- function(err) {
103
- if (err) {
104
- err.path = updates[i] + '.$in.' + i;
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
- return;
109
+ continue;
117
110
  }
118
111
 
119
112
  if (schemaPath.$isMongooseDocumentArrayElement && v != null && v.$__ != null) {
120
113
  alreadyValidated.push(updates[i]);
121
- validatorsToExecute.push(function(callback) {
122
- schemaPath.doValidate(v, function(err) {
123
- if (err) {
124
- if (err.errors) {
125
- for (const key of Object.keys(err.errors)) {
126
- const _err = err.errors[key];
127
- _err.path = updates[i] + '.' + key;
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
- return callback(null);
137
- }, context, { updateValidator: true });
138
- });
126
+ })
127
+ );
139
128
  } else {
140
- validatorsToExecute.push(function(callback) {
141
- for (const path of alreadyValidated) {
142
- if (updates[i].startsWith(path + '.')) {
143
- return callback(null);
144
- }
145
- }
146
- if (schemaPath.$isSingleNested) {
147
- alreadyValidated.push(updates[i]);
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 callback(null);
141
+ return;
155
142
  }
156
143
 
157
144
  if (err) {
158
145
  err.path = updates[i];
159
146
  validationErrors.push(err);
160
147
  }
161
- callback(null);
162
- }, context, { updateValidator: true });
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(function(callback) {
158
+ validatorsToExecute.push(
176
159
  schemaPath.doValidate(
177
160
  arrayAtomicUpdates[arrayUpdate],
178
- getValidationCallback(arrayUpdate, validationErrors, callback),
179
- options && options.context === 'query' ? query : null);
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(function(callback) {
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
- if (callback != null) {
196
- let numValidators = validatorsToExecute.length;
197
- if (numValidators === 0) {
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
- return callback(err);
188
+ for (const validationError of validationErrors) {
189
+ err.addError(validationError.path, validationError);
234
190
  }
235
- callback(null);
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
  };