mongoose 7.5.2 → 7.5.3

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.
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const assert = require('assert');
4
+ const { Long } = require('bson');
4
5
 
5
6
  /**
6
7
  * Given a value, cast it to a BigInt, or throw an `Error` if the value
@@ -23,6 +24,10 @@ module.exports = function castBigInt(val) {
23
24
  return val;
24
25
  }
25
26
 
27
+ if (val instanceof Long) {
28
+ return val.toBigInt();
29
+ }
30
+
26
31
  if (typeof val === 'string' || typeof val === 'number') {
27
32
  return BigInt(val);
28
33
  }
package/lib/cast.js CHANGED
@@ -309,8 +309,21 @@ module.exports = function cast(schema, obj, options, context) {
309
309
  while (k--) {
310
310
  $cond = ks[k];
311
311
  nested = val[$cond];
312
-
313
- if ($cond === '$not') {
312
+ if ($cond === '$elemMatch') {
313
+ if (nested && schematype != null && schematype.schema != null) {
314
+ cast(schematype.schema, nested, options, context);
315
+ } else if (nested && schematype != null && schematype.$isMongooseArray) {
316
+ if (utils.isPOJO(nested) && nested.$not != null) {
317
+ cast(schema, nested, options, context);
318
+ } else {
319
+ val[$cond] = schematype.castForQuery(
320
+ $cond,
321
+ nested,
322
+ context
323
+ );
324
+ }
325
+ }
326
+ } else if ($cond === '$not') {
314
327
  if (nested && schematype) {
315
328
  _keys = Object.keys(nested);
316
329
  if (_keys.length && isOperator(_keys[0])) {
@@ -337,6 +350,7 @@ module.exports = function cast(schema, obj, options, context) {
337
350
  context
338
351
  );
339
352
  }
353
+
340
354
  }
341
355
  }
342
356
  } else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
package/lib/document.js CHANGED
@@ -1670,7 +1670,11 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
1670
1670
  val[arrayAtomicsSymbol] = priorVal[arrayAtomicsSymbol];
1671
1671
  val[arrayAtomicsBackupSymbol] = priorVal[arrayAtomicsBackupSymbol];
1672
1672
  if (utils.isMongooseDocumentArray(val)) {
1673
- val.forEach(doc => { doc.isNew = false; });
1673
+ val.forEach(doc => {
1674
+ if (doc != null) {
1675
+ doc.$isNew = false;
1676
+ }
1677
+ });
1674
1678
  }
1675
1679
  }
1676
1680
 
@@ -349,7 +349,7 @@ function _setClient(conn, client, options, dbName) {
349
349
  client.s.options.hosts &&
350
350
  client.s.options.hosts[0] &&
351
351
  client.s.options.hosts[0].port || void 0;
352
- conn.name = dbName != null ? dbName : client && client.s && client.s.options && client.s.options.dbName || void 0;
352
+ conn.name = dbName != null ? dbName : db.databaseName;
353
353
  conn._closeCalled = client._closeCalled;
354
354
 
355
355
  const _handleReconnect = () => {
@@ -7,15 +7,18 @@ const getDiscriminatorByValue = require('./getDiscriminatorByValue');
7
7
  * @api private
8
8
  */
9
9
 
10
- module.exports = function getConstructor(Constructor, value) {
10
+ module.exports = function getConstructor(Constructor, value, defaultDiscriminatorValue) {
11
11
  const discriminatorKey = Constructor.schema.options.discriminatorKey;
12
- if (value != null &&
13
- Constructor.discriminators &&
14
- value[discriminatorKey] != null) {
15
- if (Constructor.discriminators[value[discriminatorKey]]) {
16
- Constructor = Constructor.discriminators[value[discriminatorKey]];
12
+ let discriminatorValue = (value != null && value[discriminatorKey]);
13
+ if (discriminatorValue == null) {
14
+ discriminatorValue = defaultDiscriminatorValue;
15
+ }
16
+ if (Constructor.discriminators &&
17
+ discriminatorValue != null) {
18
+ if (Constructor.discriminators[discriminatorValue]) {
19
+ Constructor = Constructor.discriminators[discriminatorValue];
17
20
  } else {
18
- const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, value[discriminatorKey]);
21
+ const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, discriminatorValue);
19
22
  if (constructorByValue) {
20
23
  Constructor = constructorByValue;
21
24
  }
@@ -20,6 +20,8 @@ const setDefaultsOnInsert = require('../setDefaultsOnInsert');
20
20
 
21
21
  module.exports = function castBulkWrite(originalModel, op, options) {
22
22
  const now = originalModel.base.now();
23
+
24
+ const globalSetDefaultsOnInsert = originalModel.base.options.setDefaultsOnInsert;
23
25
  if (op['insertOne']) {
24
26
  return (callback) => {
25
27
  const model = decideModelByObject(originalModel, op['insertOne']['document']);
@@ -69,7 +71,10 @@ module.exports = function castBulkWrite(originalModel, op, options) {
69
71
  applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
70
72
  }
71
73
 
72
- if (op['updateOne'].setDefaultsOnInsert !== false) {
74
+ const shouldSetDefaultsOnInsert = op['updateOne'].setDefaultsOnInsert == null ?
75
+ globalSetDefaultsOnInsert :
76
+ op['updateOne'].setDefaultsOnInsert;
77
+ if (shouldSetDefaultsOnInsert !== false) {
73
78
  setDefaultsOnInsert(op['updateOne']['filter'], model.schema, op['updateOne']['update'], {
74
79
  setDefaultsOnInsert: true,
75
80
  upsert: op['updateOne'].upsert
@@ -106,7 +111,11 @@ module.exports = function castBulkWrite(originalModel, op, options) {
106
111
  const schema = model.schema;
107
112
  const strict = options.strict != null ? options.strict : model.schema.options.strict;
108
113
 
109
- if (op['updateMany'].setDefaultsOnInsert !== false) {
114
+ const shouldSetDefaultsOnInsert = op['updateMany'].setDefaultsOnInsert == null ?
115
+ globalSetDefaultsOnInsert :
116
+ op['updateMany'].setDefaultsOnInsert;
117
+
118
+ if (shouldSetDefaultsOnInsert !== false) {
110
119
  setDefaultsOnInsert(op['updateMany']['filter'], model.schema, op['updateMany']['update'], {
111
120
  setDefaultsOnInsert: true,
112
121
  upsert: op['updateMany'].upsert
package/lib/model.js CHANGED
@@ -3565,11 +3565,9 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3565
3565
  * @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
3566
3566
  *
3567
3567
  */
3568
- Model.bulkSave = async function(documents, options) {
3568
+ Model.bulkSave = async function bulkSave(documents, options) {
3569
3569
  options = options || {};
3570
3570
 
3571
- const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps });
3572
-
3573
3571
  if (options.timestamps != null) {
3574
3572
  for (const document of documents) {
3575
3573
  document.$__.saveOptions = document.$__.saveOptions || {};
@@ -3586,6 +3584,8 @@ Model.bulkSave = async function(documents, options) {
3586
3584
 
3587
3585
  await Promise.all(documents.map(buildPreSavePromise));
3588
3586
 
3587
+ const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps });
3588
+
3589
3589
  const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, options).then(
3590
3590
  (res) => ({ bulkWriteResult: res, bulkWriteError: null }),
3591
3591
  (err) => ({ bulkWriteResult: null, bulkWriteError: err })
@@ -161,7 +161,9 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
161
161
  throw new ObjectExpectedError(this.path, val);
162
162
  }
163
163
 
164
- const Constructor = getConstructor(this.caster, val);
164
+ const discriminatorKeyPath = this.schema.path(this.schema.options.discriminatorKey);
165
+ const defaultDiscriminatorValue = discriminatorKeyPath == null ? null : discriminatorKeyPath.getDefault(doc);
166
+ const Constructor = getConstructor(this.caster, val, defaultDiscriminatorValue);
165
167
 
166
168
  let subdoc;
167
169
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "7.5.2",
4
+ "version": "7.5.3",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/types/index.d.ts CHANGED
@@ -230,13 +230,13 @@ declare module 'mongoose' {
230
230
  ObtainDocumentType<any, EnforcedDocType, ResolveSchemaOptions<TSchemaOptions>>,
231
231
  ResolveSchemaOptions<TSchemaOptions>
232
232
  >,
233
- THydratedDocumentType = HydratedDocument<DocType, TVirtuals & TInstanceMethods>
233
+ THydratedDocumentType = HydratedDocument<FlatRecord<DocType>, TVirtuals & TInstanceMethods>
234
234
  >
235
235
  extends events.EventEmitter {
236
236
  /**
237
237
  * Create a new schema
238
238
  */
239
- constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType> | DocType, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
239
+ constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType> | DocType, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
240
240
 
241
241
  /** Adds key path / schema type pairs to this schema. */
242
242
  add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;