mongoose 8.18.1 → 8.18.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.
package/lib/connection.js CHANGED
@@ -1025,7 +1025,7 @@ Connection.prototype.onOpen = function() {
1025
1025
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
1026
1026
  * @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
1027
1027
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
1028
- * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
1028
+ * @param {String} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
1029
1029
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
1030
1030
  * @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
1031
1031
  * @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
@@ -118,7 +118,7 @@ NativeConnection.prototype.useDb = function(name, options) {
118
118
 
119
119
  newConn.name = name;
120
120
 
121
- // push onto the otherDbs stack, this is used when state changes
121
+ // push onto the otherDbs stack, this is used when state changes and when heartbeat is received
122
122
  if (options.noListener !== true) {
123
123
  this.otherDbs.push(newConn);
124
124
  }
@@ -501,6 +501,9 @@ function _setClient(conn, client, options, dbName) {
501
501
 
502
502
  client.on('serverHeartbeatSucceeded', () => {
503
503
  conn._lastHeartbeatAt = Date.now();
504
+ for (const otherDb of conn.otherDbs) {
505
+ otherDb._lastHeartbeatAt = conn._lastHeartbeatAt;
506
+ }
504
507
  });
505
508
 
506
509
  if (options.monitorCommands) {
@@ -40,7 +40,7 @@ function clone(obj, options, isArrayChild) {
40
40
  }
41
41
 
42
42
  if (Array.isArray(obj)) {
43
- return cloneArray(isMongooseArray(obj) ? obj.__array : obj, options);
43
+ return cloneArray(obj, options);
44
44
  }
45
45
 
46
46
  if (isMongooseObject(obj)) {
@@ -172,7 +172,21 @@ function cloneObject(obj, options, isArrayChild) {
172
172
  function cloneArray(arr, options) {
173
173
  let i = 0;
174
174
  const len = arr.length;
175
- const ret = new Array(len);
175
+
176
+ let ret = null;
177
+ if (options?.retainDocuments) {
178
+ if (arr.isMongooseDocumentArray) {
179
+ ret = new (arr.$schemaType().schema.base.Types.DocumentArray)([], arr.$path(), arr.$parent(), arr.$schemaType());
180
+ } else if (arr.isMongooseArray) {
181
+ ret = new (arr.$parent().schema.base.Types.Array)([], arr.$path(), arr.$parent(), arr.$schemaType());
182
+ } else {
183
+ ret = new Array(len);
184
+ }
185
+ } else {
186
+ ret = new Array(len);
187
+ }
188
+
189
+ arr = isMongooseArray(arr) ? arr.__array : arr;
176
190
  for (i = 0; i < len; ++i) {
177
191
  ret[i] = clone(arr[i], options, true);
178
192
  }
@@ -213,9 +213,9 @@ function castPipelineOperator(op, val) {
213
213
  * @api private
214
214
  */
215
215
 
216
- function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
216
+ function walkUpdatePath(schema, obj, op, options, context, filter, prefix) {
217
217
  const strict = options.strict;
218
- const prefix = pref ? pref + '.' : '';
218
+ prefix = prefix ? prefix + '.' : '';
219
219
  const keys = Object.keys(obj);
220
220
  let i = keys.length;
221
221
  let hasKeys = false;
@@ -380,7 +380,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
380
380
  }
381
381
  } else {
382
382
  const checkPath = (key === '$each' || key === '$or' || key === '$and' || key === '$in') ?
383
- pref : prefix + key;
383
+ prefix : prefix + key;
384
384
  schematype = schema._getSchema(checkPath);
385
385
 
386
386
  // You can use `$setOnInsert` with immutable keys
package/lib/model.js CHANGED
@@ -192,7 +192,7 @@ Model.useConnection = function useConnection(connection) {
192
192
  }
193
193
 
194
194
  this.db = connection;
195
- const collection = connection.collection(this.modelName, connection.options);
195
+ const collection = connection.collection(this.collection.collectionName, connection.options);
196
196
  this.prototype.collection = collection;
197
197
  this.prototype.$collection = collection;
198
198
  this.prototype[modelCollectionSymbol] = collection;
package/lib/mongoose.js CHANGED
@@ -368,7 +368,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
368
368
  * @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below.
369
369
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
370
370
  * @param {String} [options.dbName] The name of the database you want to use. If not provided, Mongoose uses the database name from connection string.
371
- * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
371
+ * @param {String} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
372
372
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
373
373
  * @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
374
374
  * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html#promiseLibrary).
@@ -420,7 +420,7 @@ Mongoose.prototype.createConnection = function createConnection(uri, options) {
420
420
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
421
421
  * @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
422
422
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
423
- * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
423
+ * @param {String} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
424
424
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
425
425
  * @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
426
426
  * @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection.
@@ -99,6 +99,13 @@ const methods = {
99
99
  return this[arrayPathSymbol];
100
100
  },
101
101
 
102
+ /*!
103
+ * ignore
104
+ */
105
+ $schemaType() {
106
+ return this[arraySchemaSymbol];
107
+ },
108
+
102
109
  /**
103
110
  * Atomically shifts the array at most one time per document `save()`.
104
111
  *
@@ -41,6 +41,13 @@ const methods = {
41
41
  return this[arrayParentSymbol];
42
42
  },
43
43
 
44
+ /*!
45
+ * ignore
46
+ */
47
+ $schemaType() {
48
+ return this[arraySchemaSymbol];
49
+ },
50
+
44
51
  /**
45
52
  * Overrides MongooseArray#cast
46
53
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.18.1",
4
+ "version": "8.18.3",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/types/models.d.ts CHANGED
@@ -532,10 +532,6 @@ declare module 'mongoose' {
532
532
  insertMany(
533
533
  docs: Array<TRawDocType>
534
534
  ): Promise<Array<THydratedDocumentType>>;
535
- insertMany(
536
- docs: Array<TRawDocType>,
537
- options: InsertManyOptions & { lean: true; }
538
- ): Promise<Array<Require_id<TRawDocType>>>;
539
535
  insertMany(
540
536
  doc: Array<TRawDocType>,
541
537
  options: InsertManyOptions & { ordered: false; rawResult: true; }
@@ -553,22 +549,6 @@ declare module 'mongoose' {
553
549
  docs: Array<TRawDocType>,
554
550
  options: InsertManyOptions & { lean: true, rawResult: true; }
555
551
  ): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>>>;
556
- insertMany(
557
- docs: Array<TRawDocType>,
558
- options: InsertManyOptions & { rawResult: true; }
559
- ): Promise<mongodb.InsertManyResult<Require_id<THydratedDocumentType>>>;
560
- insertMany(
561
- doc: Array<TRawDocType>,
562
- options: InsertManyOptions
563
- ): Promise<Array<THydratedDocumentType>>;
564
- insertMany<DocContents = TRawDocType>(
565
- docs: Array<DocContents | TRawDocType>,
566
- options: InsertManyOptions & { lean: true; }
567
- ): Promise<Array<Require_id<DocContents>>>;
568
- insertMany<DocContents = TRawDocType>(
569
- docs: DocContents | TRawDocType,
570
- options: InsertManyOptions & { lean: true; }
571
- ): Promise<Array<Require_id<DocContents>>>;
572
552
  insertMany<DocContents = TRawDocType>(
573
553
  doc: DocContents | TRawDocType,
574
554
  options: InsertManyOptions & { ordered: false; rawResult: true; }
@@ -582,13 +562,26 @@ declare module 'mongoose' {
582
562
  >
583
563
  }
584
564
  }>;
565
+ insertMany(
566
+ docs: Array<TRawDocType>,
567
+ options: InsertManyOptions & { lean: true; }
568
+ ): Promise<Array<Require_id<TRawDocType>>>;
569
+ insertMany(
570
+ docs: Array<TRawDocType>,
571
+ options: InsertManyOptions & { rawResult: true; }
572
+ ): Promise<mongodb.InsertManyResult<Require_id<THydratedDocumentType>>>;
573
+ insertMany<DocContents = TRawDocType>(
574
+ docs: Array<DocContents | TRawDocType>,
575
+ options: InsertManyOptions & { lean: true; }
576
+ ): Promise<Array<Require_id<DocContents>>>;
577
+ insertMany<DocContents = TRawDocType>(
578
+ docs: DocContents | TRawDocType,
579
+ options: InsertManyOptions & { lean: true; }
580
+ ): Promise<Array<Require_id<DocContents>>>;
585
581
  insertMany<DocContents = TRawDocType>(
586
582
  docs: Array<DocContents | TRawDocType>,
587
583
  options: InsertManyOptions & { rawResult: true; }
588
584
  ): Promise<mongodb.InsertManyResult<Require_id<DocContents>>>;
589
- insertMany<DocContents = TRawDocType>(
590
- docs: Array<DocContents | TRawDocType>
591
- ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
592
585
  insertMany<DocContents = TRawDocType>(
593
586
  doc: DocContents,
594
587
  options: InsertManyOptions & { lean: true; }
@@ -597,6 +590,13 @@ declare module 'mongoose' {
597
590
  doc: DocContents,
598
591
  options: InsertManyOptions & { rawResult: true; }
599
592
  ): Promise<mongodb.InsertManyResult<Require_id<DocContents>>>;
593
+ insertMany(
594
+ doc: Array<TRawDocType>,
595
+ options: InsertManyOptions
596
+ ): Promise<Array<THydratedDocumentType>>;
597
+ insertMany<DocContents = TRawDocType>(
598
+ docs: Array<DocContents | TRawDocType>
599
+ ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
600
600
  insertMany<DocContents = TRawDocType>(
601
601
  doc: DocContents,
602
602
  options: InsertManyOptions
package/types/query.d.ts CHANGED
@@ -31,7 +31,9 @@ declare module 'mongoose' {
31
31
  | 'strictQuery'
32
32
  | 'translateAliases';
33
33
 
34
- type MongooseBaseQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, MongooseBaseQueryOptionKeys>;
34
+ type MongooseBaseQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, MongooseBaseQueryOptionKeys | 'timestamps' | 'lean'> & {
35
+ [other: string]: any;
36
+ };
35
37
 
36
38
  type MongooseUpdateQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, MongooseBaseQueryOptionKeys | 'timestamps'>;
37
39
 
@@ -322,8 +322,8 @@ declare module 'mongoose' {
322
322
  */
323
323
  required(required: boolean, message?: string): this;
324
324
 
325
- /** The schema this SchemaType instance is part of */
326
- schema: Schema<any>;
325
+ /** If the SchemaType is a subdocument or document array, this is the schema of that subdocument */
326
+ schema?: Schema<any>;
327
327
 
328
328
  /** Sets default select() behavior for this path. */
329
329
  select(val: boolean): this;