mongoose 8.18.1 → 8.18.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.
@@ -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
  }
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;
@@ -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.2",
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
@@ -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;