mongoose 8.7.2 → 8.8.0

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/types/index.d.ts CHANGED
@@ -25,7 +25,7 @@
25
25
  /// <reference path="./virtuals.d.ts" />
26
26
  /// <reference path="./augmentations.d.ts" />
27
27
 
28
- declare class NativeDate extends global.Date { }
28
+ declare class NativeDate extends globalThis.Date { }
29
29
 
30
30
  declare module 'mongoose' {
31
31
  import Kareem = require('kareem');
@@ -140,7 +140,7 @@ declare module 'mongoose' {
140
140
 
141
141
  export type Default__v<T> = T extends { __v?: infer U }
142
142
  ? T
143
- : T & { __v?: number };
143
+ : T & { __v: number };
144
144
 
145
145
  /** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */
146
146
  export type HydratedDocument<
@@ -623,6 +623,9 @@ declare module 'mongoose' {
623
623
  /** Additional options like `limit` and `lean`. */
624
624
  options?: QueryOptions<DocType> & { match?: AnyObject };
625
625
 
626
+ /** If true and the given `name` is a direct child of an array, apply the virtual to the array rather than the elements. */
627
+ applyToArray?: boolean;
628
+
626
629
  /** Additional options for plugins */
627
630
  [extra: string]: any;
628
631
  }
@@ -706,6 +709,9 @@ declare module 'mongoose' {
706
709
  [K in keyof T]: FlattenProperty<T[K]>;
707
710
  };
708
711
 
712
+ /**
713
+ * Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
714
+ */
709
715
  export type BufferToBinary<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
710
716
  [K in keyof T]: T[K] extends Buffer
711
717
  ? mongodb.Binary
@@ -718,6 +724,76 @@ declare module 'mongoose' {
718
724
  : BufferToBinary<T[K]>;
719
725
  } : T;
720
726
 
727
+ /**
728
+ * Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
729
+ */
730
+ export type BufferToJSON<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
731
+ [K in keyof T]: T[K] extends Buffer
732
+ ? { type: 'buffer', data: number[] }
733
+ : T[K] extends (Buffer | null | undefined)
734
+ ? { type: 'buffer', data: number[] } | null | undefined
735
+ : T[K] extends Types.DocumentArray<infer ItemType>
736
+ ? Types.DocumentArray<BufferToBinary<ItemType>>
737
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
738
+ ? HydratedSingleSubdocument<SubdocType>
739
+ : BufferToBinary<T[K]>;
740
+ } : T;
741
+
742
+ /**
743
+ * Converts any ObjectId properties into strings for JSON serialization
744
+ */
745
+ export type ObjectIdToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
746
+ [K in keyof T]: T[K] extends mongodb.ObjectId
747
+ ? string
748
+ : T[K] extends (mongodb.ObjectId | null | undefined)
749
+ ? string | null | undefined
750
+ : T[K] extends Types.DocumentArray<infer ItemType>
751
+ ? Types.DocumentArray<ObjectIdToString<ItemType>>
752
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
753
+ ? HydratedSingleSubdocument<ObjectIdToString<SubdocType>>
754
+ : ObjectIdToString<T[K]>;
755
+ } : T;
756
+
757
+ /**
758
+ * Converts any Date properties into strings for JSON serialization
759
+ */
760
+ export type DateToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
761
+ [K in keyof T]: T[K] extends NativeDate
762
+ ? string
763
+ : T[K] extends (NativeDate | null | undefined)
764
+ ? string | null | undefined
765
+ : T[K] extends Types.DocumentArray<infer ItemType>
766
+ ? Types.DocumentArray<DateToString<ItemType>>
767
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
768
+ ? HydratedSingleSubdocument<DateToString<SubdocType>>
769
+ : DateToString<T[K]>;
770
+ } : T;
771
+
772
+ /**
773
+ * Converts any Mongoose subdocuments (single nested or doc arrays) into POJO equivalents
774
+ */
775
+ export type SubdocsToPOJOs<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
776
+ [K in keyof T]: T[K] extends NativeDate
777
+ ? string
778
+ : T[K] extends (NativeDate | null | undefined)
779
+ ? string | null | undefined
780
+ : T[K] extends Types.DocumentArray<infer ItemType>
781
+ ? ItemType[]
782
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
783
+ ? SubdocType
784
+ : SubdocsToPOJOs<T[K]>;
785
+ } : T;
786
+
787
+ export type JSONSerialized<T> = SubdocsToPOJOs<
788
+ FlattenMaps<
789
+ BufferToJSON<
790
+ ObjectIdToString<
791
+ DateToString<T>
792
+ >
793
+ >
794
+ >
795
+ >;
796
+
721
797
  /**
722
798
  * Separate type is needed for properties of union type (for example, Types.DocumentArray | undefined) to apply conditional check to each member of it
723
799
  * https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
@@ -57,7 +57,8 @@ declare module 'mongoose' {
57
57
  type IndexDefinition = Record<string, IndexDirection>;
58
58
 
59
59
  interface SyncIndexesOptions extends mongodb.CreateIndexesOptions {
60
- continueOnError?: boolean
60
+ continueOnError?: boolean;
61
+ hideIndexes?: boolean;
61
62
  }
62
63
  type ConnectionSyncIndexesResult = Record<string, OneCollectionSyncIndexesResult>;
63
64
  type OneCollectionSyncIndexesResult = Array<string> & mongodb.MongoServerError;
@@ -146,9 +146,10 @@ type RequiredPathKeys<T, TypeKey extends string = DefaultTypeKey> = {
146
146
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
147
147
  * @returns a record contains required paths with the corresponding type.
148
148
  */
149
- type RequiredPaths<T, TypeKey extends string = DefaultTypeKey> = {
150
- [K in RequiredPathKeys<T, TypeKey>]: T[K];
151
- };
149
+ type RequiredPaths<T, TypeKey extends string = DefaultTypeKey> = Pick<
150
+ { -readonly [K in keyof T]: T[K] },
151
+ RequiredPathKeys<T, TypeKey>
152
+ >;
152
153
 
153
154
  /**
154
155
  * @summary A Utility to obtain schema's optional path keys.
@@ -166,9 +167,10 @@ type OptionalPathKeys<T, TypeKey extends string = DefaultTypeKey> = {
166
167
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
167
168
  * @returns a record contains optional paths with the corresponding type.
168
169
  */
169
- type OptionalPaths<T, TypeKey extends string = DefaultTypeKey> = {
170
- [K in OptionalPathKeys<T, TypeKey>]?: T[K];
171
- };
170
+ type OptionalPaths<T, TypeKey extends string = DefaultTypeKey> = Pick<
171
+ { -readonly [K in keyof T]?: T[K] },
172
+ OptionalPathKeys<T, TypeKey>
173
+ >;
172
174
 
173
175
  /**
174
176
  * @summary Allows users to optionally choose their own type for a schema field for stronger typing.
package/types/models.d.ts CHANGED
@@ -64,12 +64,9 @@ declare module 'mongoose' {
64
64
  throwOnValidationError?: boolean;
65
65
  }
66
66
 
67
- type InsertManyResult<T> = mongodb.InsertManyResult<T> & {
68
- insertedIds: {
69
- [key: number]: InferId<T>;
70
- };
67
+ interface InsertManyResult<T> extends mongodb.InsertManyResult<T> {
71
68
  mongoose?: { validationErrors?: Array<Error.CastError | Error.ValidatorError> };
72
- };
69
+ }
73
70
 
74
71
  type UpdateWriteOpResult = mongodb.UpdateResult;
75
72
  type UpdateResult = mongodb.UpdateResult;
@@ -129,7 +126,7 @@ declare module 'mongoose' {
129
126
  }
130
127
 
131
128
  interface ModifyResult<T> {
132
- value: Require_id<T> | null;
129
+ value: Default__v<Require_id<T>> | null;
133
130
  /** see https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject */
134
131
  lastErrorObject?: {
135
132
  updatedExisting?: boolean;
@@ -295,6 +292,11 @@ declare module 'mongoose' {
295
292
  /* Apply virtuals to the given POJO. */
296
293
  applyVirtuals(obj: AnyObject, virtalsToApply?: string[]): AnyObject;
297
294
 
295
+ /**
296
+ * Apply this model's timestamps to a given POJO, including subdocument timestamps
297
+ */
298
+ applyTimestamps(obj: AnyObject, options?: { isUpdate?: boolean, currentTime?: () => Date }): AnyObject;
299
+
298
300
  /**
299
301
  * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`,
300
302
  * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one
package/types/query.d.ts CHANGED
@@ -24,6 +24,7 @@ declare module 'mongoose' {
24
24
  | 'runValidators'
25
25
  | 'sanitizeProjection'
26
26
  | 'sanitizeFilter'
27
+ | 'schemaLevelProjections'
27
28
  | 'setDefaultsOnInsert'
28
29
  | 'strict'
29
30
  | 'strictQuery'
@@ -179,6 +180,11 @@ declare module 'mongoose' {
179
180
  * aren't explicitly allowed using `mongoose.trusted()`.
180
181
  */
181
182
  sanitizeFilter?: boolean;
183
+ /**
184
+ * Enable or disable schema level projections for this query. Enabled by default.
185
+ * Set to `false` to include fields with `select: false` in the query result by default.
186
+ */
187
+ schemaLevelProjections?: boolean;
182
188
  setDefaultsOnInsert?: boolean;
183
189
  skip?: number;
184
190
  sort?: any;
@@ -211,7 +217,7 @@ declare module 'mongoose' {
211
217
  type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';
212
218
 
213
219
  type GetLeanResultType<RawDocType, ResultType, QueryOp> = QueryOp extends QueryOpThatReturnsDocument
214
- ? (ResultType extends any[] ? Require_id<BufferToBinary<FlattenMaps<RawDocType>>>[] : Require_id<BufferToBinary<FlattenMaps<RawDocType>>>)
220
+ ? (ResultType extends any[] ? Default__v<Require_id<BufferToBinary<FlattenMaps<RawDocType>>>>[] : Default__v<Require_id<BufferToBinary<FlattenMaps<RawDocType>>>>)
215
221
  : ResultType;
216
222
 
217
223
  type MergePopulatePaths<RawDocType, ResultType, QueryOp, Paths, TQueryHelpers, TInstanceMethods = Record<string, never>> = QueryOp extends QueryOpThatReturnsDocument
@@ -734,6 +740,12 @@ declare module 'mongoose' {
734
740
  */
735
741
  sanitizeProjection(value: boolean): this;
736
742
 
743
+ /**
744
+ * Enable or disable schema level projections for this query. Enabled by default.
745
+ * Set to `false` to include fields with `select: false` in the query result by default.
746
+ */
747
+ schemaLevelProjections(value: boolean): this;
748
+
737
749
  /** Specifies which document fields to include or exclude (also known as the query "projection") */
738
750
  select<RawDocTypeOverride extends { [P in keyof RawDocType]?: any } = {}>(
739
751
  arg: string | string[] | Record<string, number | boolean | string | object>