mongoose 8.18.0 → 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.0",
4
+ "version": "8.18.2",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -29,8 +29,8 @@
29
29
  "sift": "17.1.3"
30
30
  },
31
31
  "devDependencies": {
32
- "@babel/core": "7.28.0",
33
- "@babel/preset-env": "7.28.0",
32
+ "@babel/core": "7.28.3",
33
+ "@babel/preset-env": "7.28.3",
34
34
  "@mongodb-js/mongodb-downloader": "^0.4.2",
35
35
  "@typescript-eslint/eslint-plugin": "^8.19.1",
36
36
  "@typescript-eslint/parser": "^8.19.1",
@@ -56,9 +56,9 @@
56
56
  "markdownlint-cli2": "^0.18.1",
57
57
  "marked": "15.0.12",
58
58
  "mkdirp": "^3.0.1",
59
- "mocha": "11.7.1",
59
+ "mocha": "11.7.2",
60
60
  "moment": "2.30.1",
61
- "mongodb-memory-server": "10.1.4",
61
+ "mongodb-memory-server": "10.2.0",
62
62
  "mongodb-runner": "^5.8.2",
63
63
  "ncp": "^2.0.0",
64
64
  "nyc": "15.1.0",
@@ -66,10 +66,10 @@
66
66
  "q": "1.5.1",
67
67
  "sinon": "21.0.0",
68
68
  "stream-browserify": "3.0.0",
69
- "tsd": "0.32.0",
70
- "typescript": "5.8.3",
69
+ "tsd": "0.33.0",
70
+ "typescript": "5.9.2",
71
71
  "uuid": "11.1.0",
72
- "webpack": "5.101.0"
72
+ "webpack": "5.101.3"
73
73
  },
74
74
  "directories": {
75
75
  "lib": "./lib/mongoose"
@@ -111,8 +111,8 @@ declare module 'mongoose' {
111
111
  PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
112
112
  PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
113
113
  IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
114
- PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
115
- IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
114
+ PathValueType extends MapConstructor | 'Map' ? Map<string, ObtainRawDocumentPathType<Options['of']>> :
115
+ IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ObtainRawDocumentPathType<Options['of']>> :
116
116
  PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToRawPathType<Options['of']> : never :
117
117
  PathValueType extends ArrayConstructor ? any[] :
118
118
  PathValueType extends typeof Schema.Types.Mixed ? any:
@@ -272,7 +272,7 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
272
272
  // we need to call ObtainDocumentType to correctly infer its type.
273
273
  Types.DocumentArray<ObtainDocumentType<Item, any, { typeKey: TypeKey }>> :
274
274
  IsSchemaTypeFromBuiltinClass<Item> extends true ?
275
- ObtainDocumentPathType<Item, TypeKey>[] :
275
+ ResolvePathType<Item, { enum: Options['enum'] }, TypeKey>[] :
276
276
  IsItRecordAndNotAny<Item> extends true ?
277
277
  Item extends Record<string, never> ?
278
278
  ObtainDocumentPathType<Item, TypeKey>[] :
@@ -316,8 +316,8 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
316
316
  PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
317
317
  PathValueType extends 'double' | 'Double' | typeof Schema.Types.Double ? Types.Double :
318
318
  IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
319
- PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
320
- IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
319
+ PathValueType extends MapConstructor | 'Map' ? Map<string, ObtainDocumentPathType<Options['of']>> :
320
+ IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ObtainDocumentPathType<Options['of']>> :
321
321
  PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToType<Options['of']> : never :
322
322
  PathValueType extends ArrayConstructor ? any[] :
323
323
  PathValueType extends typeof Schema.Types.Mixed ? any:
package/types/models.d.ts CHANGED
@@ -435,7 +435,7 @@ declare module 'mongoose' {
435
435
  TQueryHelpers & ObtainSchemaGeneric<TDiscriminatorSchema, 'TQueryHelpers'>,
436
436
  TInstanceMethods & ObtainSchemaGeneric<TDiscriminatorSchema, 'TInstanceMethods'>,
437
437
  TVirtuals & ObtainSchemaGeneric<TDiscriminatorSchema, 'TVirtuals'>
438
- > & ObtainSchemaGeneric<TDiscriminatorSchema, 'TStaticMethods'>;
438
+ > & ObtainSchemaGeneric<TSchema, 'TStaticMethods'> & ObtainSchemaGeneric<TDiscriminatorSchema, 'TStaticMethods'>;
439
439
  discriminator<D>(
440
440
  name: string | number,
441
441
  schema: Schema,
@@ -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
@@ -180,7 +180,7 @@ declare module 'mongoose' {
180
180
  *
181
181
  * @default '__v'
182
182
  */
183
- versionKey?: string | boolean;
183
+ versionKey?: string | false;
184
184
  /**
185
185
  * By default, Mongoose will automatically select() any populated paths for you, unless you explicitly exclude them.
186
186
  *
@@ -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;