mongoose 8.10.1 → 8.10.2

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/connection.js CHANGED
@@ -23,6 +23,7 @@ const CreateCollectionsError = require('./error/createCollectionsError');
23
23
  const castBulkWrite = require('./helpers/model/castBulkWrite');
24
24
  const { modelSymbol } = require('./helpers/symbols');
25
25
  const isPromise = require('./helpers/isPromise');
26
+ const decorateBulkWriteResult = require('./helpers/model/decorateBulkWriteResult');
26
27
 
27
28
  const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
28
29
  const sessionNewDocuments = require('./helpers/symbols').sessionNewDocuments;
@@ -559,7 +560,9 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
559
560
  'bulkWrite'
560
561
  );
561
562
  }
562
- return getDefaultBulkwriteResult();
563
+ const BulkWriteResult = this.base.driver.get().BulkWriteResult;
564
+ const res = new BulkWriteResult(getDefaultBulkwriteResult(), false);
565
+ return decorateBulkWriteResult(res, validationErrors, results);
563
566
  }
564
567
 
565
568
  let error;
@@ -567,16 +570,17 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
567
570
  then(res => ([res, null])).
568
571
  catch(err => ([null, err]));
569
572
 
573
+ for (let i = 0; i < validOpIndexes.length; ++i) {
574
+ results[validOpIndexes[i]] = null;
575
+ }
570
576
  if (error) {
571
577
  if (validationErrors.length > 0) {
578
+ decorateBulkWriteResult(error, validationErrors, results);
572
579
  error.mongoose = error.mongoose || {};
573
580
  error.mongoose.validationErrors = validationErrors;
574
581
  }
575
582
  }
576
583
 
577
- for (let i = 0; i < validOpIndexes.length; ++i) {
578
- results[validOpIndexes[i]] = null;
579
- }
580
584
  if (validationErrors.length > 0) {
581
585
  if (options.throwOnValidationError) {
582
586
  throw new MongooseBulkWriteError(
@@ -586,9 +590,7 @@ Connection.prototype.bulkWrite = async function bulkWrite(ops, options) {
586
590
  'bulkWrite'
587
591
  );
588
592
  } else {
589
- res.mongoose = res.mongoose || {};
590
- res.mongoose.validationErrors = validationErrors;
591
- res.mongoose.results = results;
593
+ decorateBulkWriteResult(res, validationErrors, results);
592
594
  }
593
595
  }
594
596
  }
@@ -10,3 +10,4 @@ exports.Collection = function() {
10
10
  exports.Connection = function() {
11
11
  throw new Error('Cannot create a connection from browser library');
12
12
  };
13
+ exports.BulkWriteResult = function() {};
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const BulkWriteResult = require('mongodb/lib/bulk/common').BulkWriteResult;
4
+
5
+ module.exports = BulkWriteResult;
@@ -13,6 +13,8 @@ const internalToObjectOptions = require('../../options').internalToObjectOptions
13
13
  const stream = require('stream');
14
14
  const util = require('util');
15
15
 
16
+ const formatToObjectOptions = Object.freeze({ ...internalToObjectOptions, copyTrustedSymbol: false });
17
+
16
18
  /**
17
19
  * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
18
20
  *
@@ -384,7 +386,9 @@ function format(obj, sub, color, shell) {
384
386
  }
385
387
 
386
388
  const clone = require('../../helpers/clone');
387
- let x = clone(obj, internalToObjectOptions);
389
+ // `sub` indicates `format()` was called recursively, so skip cloning because we already
390
+ // did a deep clone on the top-level object.
391
+ let x = sub ? obj : clone(obj, formatToObjectOptions);
388
392
  const constructorName = getConstructorName(x);
389
393
 
390
394
  if (constructorName === 'Binary') {
@@ -4,5 +4,6 @@
4
4
 
5
5
  'use strict';
6
6
 
7
+ exports.BulkWriteResult = require('./bulkWriteResult');
7
8
  exports.Collection = require('./collection');
8
9
  exports.Connection = require('./connection');
@@ -147,7 +147,7 @@ function cloneObject(obj, options, isArrayChild) {
147
147
  } else if (seen) {
148
148
  seen.set(obj, ret);
149
149
  }
150
- if (trustedSymbol in obj) {
150
+ if (trustedSymbol in obj && options?.copyTrustedSymbol !== false) {
151
151
  ret[trustedSymbol] = obj[trustedSymbol];
152
152
  }
153
153
 
@@ -1,26 +1,17 @@
1
1
  'use strict';
2
+
2
3
  function getDefaultBulkwriteResult() {
3
4
  return {
4
- result: {
5
- ok: 1,
6
- writeErrors: [],
7
- writeConcernErrors: [],
8
- insertedIds: [],
9
- nInserted: 0,
10
- nUpserted: 0,
11
- nMatched: 0,
12
- nModified: 0,
13
- nRemoved: 0,
14
- upserted: []
15
- },
16
- insertedCount: 0,
17
- matchedCount: 0,
18
- modifiedCount: 0,
19
- deletedCount: 0,
20
- upsertedCount: 0,
21
- upsertedIds: {},
22
- insertedIds: {},
23
- n: 0
5
+ ok: 1,
6
+ nInserted: 0,
7
+ nUpserted: 0,
8
+ nMatched: 0,
9
+ nModified: 0,
10
+ nRemoved: 0,
11
+ upserted: [],
12
+ writeErrors: [],
13
+ insertedIds: [],
14
+ writeConcernErrors: []
24
15
  };
25
16
  }
26
17
 
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ module.exports = function decorateBulkWriteResult(resultOrError, validationErrors, results) {
4
+ resultOrError.mongoose = resultOrError.mongoose || {};
5
+ resultOrError.mongoose.validationErrors = validationErrors;
6
+ resultOrError.mongoose.results = results;
7
+ return resultOrError;
8
+ };
package/lib/model.js CHANGED
@@ -10,7 +10,6 @@ const Document = require('./document');
10
10
  const DocumentNotFoundError = require('./error/notFound');
11
11
  const EventEmitter = require('events').EventEmitter;
12
12
  const Kareem = require('kareem');
13
- const { MongoBulkWriteError } = require('mongodb');
14
13
  const MongooseBulkWriteError = require('./error/bulkWriteError');
15
14
  const MongooseError = require('./error/index');
16
15
  const ObjectParameterError = require('./error/objectParameter');
@@ -69,6 +68,7 @@ const utils = require('./utils');
69
68
  const minimize = require('./helpers/minimize');
70
69
  const MongooseBulkSaveIncompleteError = require('./error/bulkSaveIncompleteError');
71
70
  const ObjectExpectedError = require('./error/objectExpected');
71
+ const decorateBulkWriteResult = require('./helpers/model/decorateBulkWriteResult');
72
72
 
73
73
  const modelCollectionSymbol = Symbol('mongoose#Model#collection');
74
74
  const modelDbSymbol = Symbol('mongoose#Model#db');
@@ -3399,7 +3399,11 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3399
3399
  const ordered = options.ordered == null ? true : options.ordered;
3400
3400
 
3401
3401
  if (ops.length === 0) {
3402
- return getDefaultBulkwriteResult();
3402
+ const BulkWriteResult = this.base.driver.get().BulkWriteResult;
3403
+ const bulkWriteResult = new BulkWriteResult(getDefaultBulkwriteResult(), false);
3404
+ bulkWriteResult.n = 0;
3405
+ decorateBulkWriteResult(bulkWriteResult, [], []);
3406
+ return bulkWriteResult;
3403
3407
  }
3404
3408
 
3405
3409
  const validations = ops.map(op => castBulkWrite(this, op, options));
@@ -3470,7 +3474,11 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3470
3474
  'bulkWrite'
3471
3475
  );
3472
3476
  }
3473
- return getDefaultBulkwriteResult();
3477
+ const BulkWriteResult = this.base.driver.get().BulkWriteResult;
3478
+ const bulkWriteResult = new BulkWriteResult(getDefaultBulkwriteResult(), false);
3479
+ bulkWriteResult.result = getDefaultBulkwriteResult();
3480
+ decorateBulkWriteResult(bulkWriteResult, validationErrors, results);
3481
+ return bulkWriteResult;
3474
3482
  }
3475
3483
 
3476
3484
  let error;
@@ -3478,10 +3486,12 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3478
3486
  then(res => ([res, null])).
3479
3487
  catch(error => ([null, error]));
3480
3488
 
3489
+ for (let i = 0; i < validOpIndexes.length; ++i) {
3490
+ results[validOpIndexes[i]] = null;
3491
+ }
3481
3492
  if (error) {
3482
3493
  if (validationErrors.length > 0) {
3483
- error.mongoose = error.mongoose || {};
3484
- error.mongoose.validationErrors = validationErrors;
3494
+ decorateBulkWriteResult(error, validationErrors, results);
3485
3495
  }
3486
3496
 
3487
3497
  await new Promise((resolve, reject) => {
@@ -3495,9 +3505,6 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3495
3505
  });
3496
3506
  }
3497
3507
 
3498
- for (let i = 0; i < validOpIndexes.length; ++i) {
3499
- results[validOpIndexes[i]] = null;
3500
- }
3501
3508
  if (validationErrors.length > 0) {
3502
3509
  if (options.throwOnValidationError) {
3503
3510
  throw new MongooseBulkWriteError(
@@ -3507,9 +3514,7 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3507
3514
  'bulkWrite'
3508
3515
  );
3509
3516
  } else {
3510
- res.mongoose = res.mongoose || {};
3511
- res.mongoose.validationErrors = validationErrors;
3512
- res.mongoose.results = results;
3517
+ decorateBulkWriteResult(res, validationErrors, results);
3513
3518
  }
3514
3519
  }
3515
3520
  }
@@ -3575,7 +3580,7 @@ Model.bulkSave = async function bulkSave(documents, options) {
3575
3580
  (err) => ({ bulkWriteResult: null, bulkWriteError: err })
3576
3581
  );
3577
3582
  // If not a MongoBulkWriteError, treat this as all documents failed to save.
3578
- if (bulkWriteError != null && !(bulkWriteError instanceof MongoBulkWriteError)) {
3583
+ if (bulkWriteError != null && bulkWriteError.name !== 'MongoBulkWriteError') {
3579
3584
  throw bulkWriteError;
3580
3585
  }
3581
3586
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.10.1",
4
+ "version": "8.10.2",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/types/index.d.ts CHANGED
@@ -712,47 +712,53 @@ declare module 'mongoose' {
712
712
  [K in keyof T]: FlattenProperty<T[K]>;
713
713
  };
714
714
 
715
- export type BufferToBinaryProperty<T> = T extends Buffer
716
- ? mongodb.Binary
717
- : T extends Types.DocumentArray<infer ItemType>
718
- ? Types.DocumentArray<BufferToBinary<ItemType>>
719
- : T extends Types.Subdocument<unknown, unknown, infer SubdocType>
720
- ? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
721
- : BufferToBinary<T>;
715
+ export type BufferToBinaryProperty<T> = unknown extends Buffer
716
+ ? T
717
+ : T extends Buffer
718
+ ? mongodb.Binary
719
+ : T extends Types.DocumentArray<infer ItemType>
720
+ ? Types.DocumentArray<BufferToBinary<ItemType>>
721
+ : T extends Types.Subdocument<unknown, unknown, infer SubdocType>
722
+ ? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
723
+ : BufferToBinary<T>;
722
724
 
723
725
  /**
724
726
  * Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
725
727
  */
726
- export type BufferToBinary<T> = T extends Buffer
727
- ? mongodb.Binary
728
- : T extends Document
729
- ? T
730
- : T extends TreatAsPrimitives
731
- ? T
732
- : T extends Record<string, any>
733
- ? {
734
- [K in keyof T]: BufferToBinaryProperty<T[K]>
735
- }
736
- : T;
728
+ export type BufferToBinary<T> = unknown extends Buffer
729
+ ? T
730
+ : T extends Buffer
731
+ ? mongodb.Binary
732
+ : T extends Document
733
+ ? T
734
+ : T extends TreatAsPrimitives
735
+ ? T
736
+ : T extends Record<string, any>
737
+ ? {
738
+ [K in keyof T]: BufferToBinaryProperty<T[K]>
739
+ }
740
+ : T;
737
741
 
738
742
  /**
739
- * Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
740
- */
741
- export type BufferToJSON<T> = T extends Buffer
742
- ? { type: 'buffer', data: number[] }
743
- : T extends Document
744
- ? T
745
- : T extends TreatAsPrimitives
743
+ * Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
744
+ */
745
+ export type BufferToJSON<T> = unknown extends Buffer
746
+ ? T
747
+ : T extends Buffer
748
+ ? { type: 'buffer', data: number[] }
749
+ : T extends Document
746
750
  ? T
747
- : T extends Record<string, any> ? {
748
- [K in keyof T]: T[K] extends Buffer
749
- ? { type: 'buffer', data: number[] }
750
- : T[K] extends Types.DocumentArray<infer ItemType>
751
- ? Types.DocumentArray<BufferToBinary<ItemType>>
752
- : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
753
- ? HydratedSingleSubdocument<SubdocType>
754
- : BufferToBinary<T[K]>;
755
- } : T;
751
+ : T extends TreatAsPrimitives
752
+ ? T
753
+ : T extends Record<string, any> ? {
754
+ [K in keyof T]: T[K] extends Buffer
755
+ ? { type: 'buffer', data: number[] }
756
+ : T[K] extends Types.DocumentArray<infer ItemType>
757
+ ? Types.DocumentArray<BufferToBinary<ItemType>>
758
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
759
+ ? HydratedSingleSubdocument<SubdocType>
760
+ : BufferToBinary<T[K]>;
761
+ } : T;
756
762
 
757
763
  /**
758
764
  * Converts any ObjectId properties into strings for JSON serialization
@@ -235,13 +235,17 @@ type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
235
235
  ? true
236
236
  : T extends Types.Decimal128
237
237
  ? true
238
- : T extends Buffer
238
+ : T extends NativeDate
239
239
  ? true
240
- : T extends NativeDate
240
+ : T extends (typeof Schema.Types.Mixed)
241
241
  ? true
242
- : T extends (typeof Schema.Types.Mixed)
242
+ : IfEquals<T, Schema.Types.ObjectId, true, false> extends true
243
243
  ? true
244
- : IfEquals<T, Schema.Types.ObjectId, true, false>;
244
+ : unknown extends Buffer
245
+ ? false
246
+ : T extends Buffer
247
+ ? true
248
+ : false;
245
249
 
246
250
  /**
247
251
  * @summary Resolve path type by returning the corresponding type.
package/types/models.d.ts CHANGED
@@ -308,7 +308,7 @@ declare module 'mongoose' {
308
308
  bulkWrite<DocContents = TRawDocType>(
309
309
  writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
310
310
  options: MongooseBulkWriteOptions & { ordered: false }
311
- ): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
311
+ ): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[], results: Array<Error | null> } }>;
312
312
  bulkWrite<DocContents = TRawDocType>(
313
313
  writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
314
314
  options?: MongooseBulkWriteOptions