mongoose 9.10.4 → 9.11.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/lib/cast.js CHANGED
@@ -11,6 +11,7 @@ const cast$expr = require('./helpers/query/cast$expr');
11
11
  const castString = require('./cast/string');
12
12
  const castTextSearch = require('./schema/operators/text');
13
13
  const get = require('./helpers/get');
14
+ const getDiscriminatorValueFromFilter = require('./helpers/discriminator/getDiscriminatorValueFromFilter');
14
15
  const getSchemaDiscriminatorByValue = require('./helpers/discriminator/getSchemaDiscriminatorByValue');
15
16
  const isOperator = require('./helpers/query/isOperator');
16
17
  const util = require('util');
@@ -43,7 +44,8 @@ module.exports = function cast(schema, obj, options, context) {
43
44
  }
44
45
 
45
46
  if (schema?.discriminators != null && obj[schema.options.discriminatorKey] != null) {
46
- schema = getSchemaDiscriminatorByValue(schema, obj[schema.options.discriminatorKey]) || schema;
47
+ const discriminatorValue = getDiscriminatorValueFromFilter(obj[schema.options.discriminatorKey]);
48
+ schema = getSchemaDiscriminatorByValue(schema, discriminatorValue) || schema;
47
49
  }
48
50
 
49
51
  const paths = Object.keys(obj);
@@ -134,18 +136,10 @@ module.exports = function cast(schema, obj, options, context) {
134
136
  if (_schematype?.schema?.discriminators != null &&
135
137
  discriminatorKey != null &&
136
138
  pathLastHalf !== discriminatorKey) {
137
- const discriminatorVal = get(obj, pathFirstHalf + '.' + discriminatorKey);
139
+ const discriminatorVal = getDiscriminatorValueFromFilter(get(obj, pathFirstHalf + '.' + discriminatorKey));
138
140
  const discriminators = _schematype.schema.discriminators;
139
141
  if (typeof discriminatorVal === 'string' && discriminators[discriminatorVal] != null) {
140
-
141
142
  schematype = discriminators[discriminatorVal].path(pathLastHalf);
142
- } else if (discriminatorVal != null &&
143
- Object.keys(discriminatorVal).length === 1 &&
144
- Array.isArray(discriminatorVal.$in) &&
145
- discriminatorVal.$in.length === 1 &&
146
- typeof discriminatorVal.$in[0] === 'string' &&
147
- discriminators[discriminatorVal.$in[0]] != null) {
148
- schematype = discriminators[discriminatorVal.$in[0]].path(pathLastHalf);
149
143
  }
150
144
  }
151
145
  }
@@ -329,15 +323,11 @@ module.exports = function cast(schema, obj, options, context) {
329
323
  if (nested && schematype?.schema != null) {
330
324
  cast(schematype.schema, nested, options, context);
331
325
  } else if (nested && schematype?.$isMongooseArray) {
332
- if (utils.isPOJO(nested) && nested.$not != null) {
333
- cast(schema, nested, options, context);
334
- } else {
335
- val[$cond] = schematype.castForQuery(
336
- $cond,
337
- nested,
338
- context
339
- );
340
- }
326
+ val[$cond] = schematype.castForQuery(
327
+ $cond,
328
+ nested,
329
+ context
330
+ );
341
331
  }
342
332
  } else if ($cond === '$not') {
343
333
  if (nested && schematype) {
@@ -170,6 +170,9 @@ QueryCursor.prototype.getDriverCursor = async function getDriverCursor() {
170
170
  if (this.cursor) {
171
171
  return this.cursor;
172
172
  }
173
+ if (this._error) {
174
+ throw this._error;
175
+ }
173
176
 
174
177
  await once(this, 'cursor');
175
178
  return this.cursor;
@@ -278,7 +281,7 @@ QueryCursor.prototype._destroy = function _destroy(_err, callback) {
278
281
 
279
282
  waitForCursor
280
283
  .then(() => {
281
- this.cursor.close();
284
+ return this.cursor.close();
282
285
  })
283
286
  .then(() => {
284
287
  this._closed = true;