mongoose 8.16.3 → 8.16.5

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
@@ -1324,8 +1324,10 @@ Connection.prototype.onClose = function onClose(force) {
1324
1324
 
1325
1325
  this.emit('close', force);
1326
1326
 
1327
+ const wasForceClosed = typeof force === 'object' && force !== null ? force.force : force;
1328
+
1327
1329
  for (const db of this.otherDbs) {
1328
- this._destroyCalled ? db.destroy({ force: force, skipCloseClient: true }) : db.close({ force: force, skipCloseClient: true });
1330
+ this._destroyCalled ? db.destroy({ force: wasForceClosed, skipCloseClient: true }) : db.close({ force: wasForceClosed, skipCloseClient: true });
1329
1331
  }
1330
1332
  };
1331
1333
 
package/lib/document.js CHANGED
@@ -2703,6 +2703,10 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip, isNestedValidate
2703
2703
  if (!doc.$__isSelected(path) && !doc.$isModified(path)) {
2704
2704
  return false;
2705
2705
  }
2706
+ if (path.endsWith('.$*')) {
2707
+ // Skip $* paths - they represent map schemas, not actual document paths
2708
+ return false;
2709
+ }
2706
2710
  if (doc.$__.cachedRequired != null && path in doc.$__.cachedRequired) {
2707
2711
  return doc.$__.cachedRequired[path];
2708
2712
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.16.3",
4
+ "version": "8.16.5",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -72,11 +72,15 @@ declare module 'mongoose' {
72
72
  }[keyof SchemaMap];
73
73
 
74
74
  class Connection extends events.EventEmitter implements SessionStarter {
75
+ /** Runs a [db-level aggregate()](https://www.mongodb.com/docs/manual/reference/method/db.aggregate/) on this connection's underlying `db` */
75
76
  aggregate<ResultType = unknown>(pipeline?: PipelineStage[] | null, options?: AggregateOptions): Aggregate<Array<ResultType>>;
76
77
 
77
78
  /** Returns a promise that resolves when this connection successfully connects to MongoDB */
78
79
  asPromise(): Promise<this>;
79
80
 
81
+ /** The Mongoose instance this connection is associated with */
82
+ base: Mongoose;
83
+
80
84
  bulkWrite<TSchemaMap extends Record<string, AnyObject>>(
81
85
  ops: Array<ConnectionBulkWriteModel<TSchemaMap>>,
82
86
  options: mongodb.ClientBulkWriteOptions & { ordered: false }
@@ -45,8 +45,8 @@ declare module 'mongoose' {
45
45
  /** Don't run validation on this path or persist changes to this path. */
46
46
  $ignore(path: string): void;
47
47
 
48
- /** Checks if a path is set to its default. */
49
- $isDefault(path: string): boolean;
48
+ /** Checks if a path is set to its default. If no path set, checks if any path is set to its default. */
49
+ $isDefault(path?: string): boolean;
50
50
 
51
51
  /** Getter/setter, determines whether the document was removed or not. */
52
52
  $isDeleted(val?: boolean): boolean;
package/types/index.d.ts CHANGED
@@ -83,17 +83,17 @@ declare module 'mongoose' {
83
83
  collection?: string,
84
84
  options?: CompileModelOptions
85
85
  ): Model<
86
- InferSchemaType<TSchema>,
87
- ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
88
- ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
89
- ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
90
- HydratedDocument<
91
- InferSchemaType<TSchema>,
92
- ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
93
- ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
94
- ObtainSchemaGeneric<TSchema, 'TVirtuals'>
95
- >,
96
- TSchema
86
+ InferSchemaType<TSchema>,
87
+ ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
88
+ ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
89
+ ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
90
+ HydratedDocument<
91
+ InferSchemaType<TSchema>,
92
+ ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
93
+ ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
94
+ ObtainSchemaGeneric<TSchema, 'TVirtuals'>
95
+ >,
96
+ TSchema
97
97
  > & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
98
98
 
99
99
  export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
@@ -278,7 +278,23 @@ declare module 'mongoose' {
278
278
  /**
279
279
  * Create a new schema
280
280
  */
281
- constructor(definition?: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType> | DocType, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
281
+ constructor(
282
+ definition?: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType> | DocType,
283
+ options?: SchemaOptions<
284
+ FlatRecord<DocType>,
285
+ TInstanceMethods,
286
+ TQueryHelpers,
287
+ TStaticMethods,
288
+ TVirtuals,
289
+ THydratedDocumentType,
290
+ IfEquals<
291
+ TModelType,
292
+ Model<any, any, any, any>,
293
+ Model<DocType, TQueryHelpers, TInstanceMethods, TVirtuals, THydratedDocumentType>,
294
+ TModelType
295
+ >
296
+ > | ResolveSchemaOptions<TSchemaOptions>
297
+ );
282
298
 
283
299
  /** Adds key path / schema type pairs to this schema. */
284
300
  add(obj: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType> | Schema, prefix?: string): this;
@@ -56,8 +56,8 @@ declare module 'mongoose' {
56
56
  * @param {TSchema} TSchema A generic of schema type instance.
57
57
  * @param {alias} alias Targeted generic alias.
58
58
  */
59
- type ObtainSchemaGeneric<TSchema, alias extends 'EnforcedDocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'TVirtuals' | 'TStaticMethods' | 'TSchemaOptions' | 'DocType'> =
60
- TSchema extends Schema<infer EnforcedDocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer TVirtuals, infer TStaticMethods, infer TSchemaOptions, infer DocType>
59
+ type ObtainSchemaGeneric<TSchema, alias extends 'EnforcedDocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'TVirtuals' | 'TStaticMethods' | 'TSchemaOptions' | 'DocType' | 'THydratedDocumentType'> =
60
+ TSchema extends Schema<infer EnforcedDocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer TVirtuals, infer TStaticMethods, infer TSchemaOptions, infer DocType, infer THydratedDocumentType>
61
61
  ? {
62
62
  EnforcedDocType: EnforcedDocType;
63
63
  M: M;
@@ -67,6 +67,7 @@ declare module 'mongoose' {
67
67
  TStaticMethods: TStaticMethods;
68
68
  TSchemaOptions: TSchemaOptions;
69
69
  DocType: DocType;
70
+ THydratedDocumentType: THydratedDocumentType;
70
71
  }[alias]
71
72
  : unknown;
72
73
 
@@ -107,7 +108,7 @@ type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined }
107
108
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
108
109
  */
109
110
  type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
110
- P extends { required: true | [true, string | undefined] | { isRequired: true } } | ArrayConstructor | any[]
111
+ P extends { required: true | string | [true, string | undefined] | { isRequired: true } } | ArrayConstructor | any[]
111
112
  ? true
112
113
  : P extends { required: boolean }
113
114
  ? P extends { required: false }
@@ -203,7 +204,7 @@ TypeHint<PathValueType>
203
204
  * @param {T} T A generic refers to string path enums.
204
205
  * @returns Path enum values type as literal strings or string.
205
206
  */
206
- type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ReadonlyArray<infer E> ? E : T extends { values: any } ? PathEnumOrString<T['values']> : string;
207
+ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ReadonlyArray<infer E> ? E : T extends { values: any } ? PathEnumOrString<T['values']> : T extends Record<string, infer V> ? V : string;
207
208
 
208
209
  type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
209
210
  ? true
package/types/models.d.ts CHANGED
@@ -23,7 +23,14 @@ declare module 'mongoose' {
23
23
  ): U;
24
24
  }
25
25
 
26
- interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions {
26
+ export type MongooseBulkWriteResult = mongodb.BulkWriteResult & {
27
+ mongoose?: {
28
+ validationErrors: Error[],
29
+ results: Array<Error | mongodb.WriteError | null>
30
+ }
31
+ };
32
+
33
+ export interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions {
27
34
  session?: ClientSession;
28
35
  skipValidation?: boolean;
29
36
  throwOnValidationError?: boolean;
@@ -308,18 +315,18 @@ declare module 'mongoose' {
308
315
  bulkWrite<DocContents = TRawDocType>(
309
316
  writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
310
317
  options: MongooseBulkWriteOptions & { ordered: false }
311
- ): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[], results: Array<Error | mongodb.WriteError | null> } }>;
318
+ ): Promise<MongooseBulkWriteResult>;
312
319
  bulkWrite<DocContents = TRawDocType>(
313
320
  writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
314
321
  options?: MongooseBulkWriteOptions
315
- ): Promise<mongodb.BulkWriteResult>;
322
+ ): Promise<MongooseBulkWriteResult>;
316
323
 
317
324
  /**
318
325
  * Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than
319
326
  * sending multiple `save()` calls because with `bulkSave()` there is only one
320
327
  * network round trip to the MongoDB server.
321
328
  */
322
- bulkSave(documents: Array<Document>, options?: MongooseBulkSaveOptions): Promise<mongodb.BulkWriteResult>;
329
+ bulkSave(documents: Array<Document>, options?: MongooseBulkSaveOptions): Promise<MongooseBulkWriteResult>;
323
330
 
324
331
  /** Collection the model uses. */
325
332
  collection: Collection;
@@ -16,7 +16,8 @@ declare module 'mongoose' {
16
16
  QueryHelpers = {},
17
17
  TStaticMethods = {},
18
18
  TVirtuals = {},
19
- THydratedDocumentType = HydratedDocument<DocType, TInstanceMethods, QueryHelpers>
19
+ THydratedDocumentType = HydratedDocument<DocType, TInstanceMethods, QueryHelpers>,
20
+ TModelType = Model<DocType, QueryHelpers, TInstanceMethods, TVirtuals, THydratedDocumentType>
20
21
  > {
21
22
  /**
22
23
  * By default, Mongoose's init() function creates all the indexes defined in your model's schema by
@@ -219,8 +220,8 @@ declare module 'mongoose' {
219
220
  statics?: IfEquals<
220
221
  TStaticMethods,
221
222
  {},
222
- { [name: string]: (this: Model<DocType>, ...args: any[]) => unknown },
223
- AddThisParameter<TStaticMethods, Model<DocType>>
223
+ { [name: string]: (this: TModelType, ...args: any[]) => unknown },
224
+ AddThisParameter<TStaticMethods, TModelType>
224
225
  >
225
226
 
226
227
  /**