@depup/mongoose 9.3.2-depup.0 → 9.4.1-depup.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.
Files changed (41) hide show
  1. package/README.md +9 -3
  2. package/changes.json +8 -3
  3. package/lib/aggregate.js +1 -1
  4. package/lib/connection.js +20 -8
  5. package/lib/document.js +108 -48
  6. package/lib/drivers/node-mongodb-native/collection.js +37 -10
  7. package/lib/drivers/node-mongodb-native/connection.js +13 -0
  8. package/lib/error/index.js +1 -1
  9. package/lib/helpers/isBsonType.js +1 -1
  10. package/lib/model.js +6 -3
  11. package/lib/mongoose.js +4 -4
  12. package/lib/query.js +1 -1
  13. package/lib/schema/array.js +1 -3
  14. package/lib/schema/bigint.js +1 -3
  15. package/lib/schema/boolean.js +1 -3
  16. package/lib/schema/buffer.js +1 -3
  17. package/lib/schema/date.js +1 -3
  18. package/lib/schema/decimal128.js +1 -3
  19. package/lib/schema/documentArray.js +2 -4
  20. package/lib/schema/double.js +1 -3
  21. package/lib/schema/int32.js +1 -3
  22. package/lib/schema/map.js +1 -4
  23. package/lib/schema/number.js +2 -4
  24. package/lib/schema/objectId.js +1 -3
  25. package/lib/schema/string.js +1 -3
  26. package/lib/schema/subdocument.js +1 -3
  27. package/lib/schema/union.js +50 -0
  28. package/lib/schema/uuid.js +1 -3
  29. package/lib/schema.js +5 -5
  30. package/lib/schemaType.js +46 -13
  31. package/lib/types/array/methods/index.js +4 -4
  32. package/lib/utils.js +12 -2
  33. package/package.json +16 -11
  34. package/types/document.d.ts +40 -3
  35. package/types/index.d.ts +17 -2
  36. package/types/models.d.ts +2 -2
  37. package/types/populate.d.ts +44 -0
  38. package/types/query.d.ts +26 -9
  39. package/types/utility.d.ts +3 -3
  40. package/lib/helpers/createJSONSchemaTypeDefinition.js +0 -24
  41. /package/{tstyche.config.json → tstyche.json} +0 -0
@@ -32,7 +32,7 @@ declare module 'mongoose' {
32
32
  _id: T;
33
33
 
34
34
  /** Assert that a given path or paths is populated. Throws an error if not populated. */
35
- $assertPopulated<Paths = {}>(path: string | string[], values?: Partial<Paths>): Omit<this, keyof Paths> & Paths;
35
+ $assertPopulated<Paths = {}>(path: string | string[], values?: Partial<Paths>): PopulateDocumentResult<this, Paths, PopulatedPathsDocumentType<DocType, Paths>, DocType>;
36
36
 
37
37
  /** Clear the document's modified paths. */
38
38
  $clearModifiedPaths(): this;
@@ -171,6 +171,13 @@ declare module 'mongoose' {
171
171
  * Returns the changes that happened to the document
172
172
  * in the format that will be sent to MongoDB.
173
173
  */
174
+ $getChanges(): UpdateQuery<this>;
175
+
176
+ /**
177
+ * Returns the changes that happened to the document
178
+ * in the format that will be sent to MongoDB.
179
+ * @deprecated Use `$getChanges()` instead.
180
+ */
174
181
  getChanges(): UpdateQuery<this>;
175
182
 
176
183
  /** Signal that we desire an increment of this documents version. */
@@ -238,8 +245,8 @@ declare module 'mongoose' {
238
245
  $parent(): Document | undefined;
239
246
 
240
247
  /** Populates document references. */
241
- populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<MergeType<this, Paths>>;
242
- populate<Paths = {}>(path: string, select?: string | AnyObject, model?: Model<any>, match?: AnyObject, options?: PopulateOptions): Promise<MergeType<this, Paths>>;
248
+ populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<PopulateDocumentResult<this, Paths, PopulatedPathsDocumentType<DocType, Paths>, DocType>>;
249
+ populate<Paths = {}>(path: string, select?: string | AnyObject, model?: Model<any>, match?: AnyObject, options?: PopulateOptions): Promise<PopulateDocumentResult<this, Paths, PopulatedPathsDocumentType<DocType, Paths>, DocType>>;
243
250
 
244
251
  /** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */
245
252
  populated(path: string): any;
@@ -262,11 +269,41 @@ declare module 'mongoose' {
262
269
  toBSON(): Require_id<DocType>;
263
270
 
264
271
  /** The return value of this method is used in calls to JSON.stringify(doc). */
272
+ toJSON<PopulatedRawDocType, DepopulatedRawDocType>(
273
+ this: PopulatedDocumentMarker<PopulatedRawDocType, DepopulatedRawDocType>,
274
+ options: { depopulate: true }
275
+ ): Default__v<Require_id<DepopulatedRawDocType>, TSchemaOptions>;
276
+ toJSON<PopulatedRawDocType, DepopulatedRawDocType, O extends ToObjectOptions & { depopulate: true }>(
277
+ this: PopulatedDocumentMarker<PopulatedRawDocType, DepopulatedRawDocType>,
278
+ options: O
279
+ ): ToObjectReturnType<DepopulatedRawDocType, TVirtuals, O, TSchemaOptions>;
280
+ toJSON<PopulatedRawDocType, O extends ToObjectOptions>(
281
+ this: PopulatedDocumentMarker<PopulatedRawDocType, any>,
282
+ options: O
283
+ ): ToObjectReturnType<PopulatedRawDocType, TVirtuals, O, TSchemaOptions>;
284
+ toJSON<PopulatedRawDocType>(
285
+ this: PopulatedDocumentMarker<PopulatedRawDocType, any>
286
+ ): Default__v<Require_id<PopulatedRawDocType>, TSchemaOptions>;
265
287
  toJSON<O extends ToObjectOptions>(options: O): ToObjectReturnType<DocType, TVirtuals, O, TSchemaOptions>;
266
288
  toJSON(options?: ToObjectOptions): Default__v<Require_id<DocType>, TSchemaOptions>;
267
289
  toJSON<T>(options?: ToObjectOptions): Default__v<Require_id<T>, ResolveSchemaOptions<TSchemaOptions>>;
268
290
 
269
291
  /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
292
+ toObject<PopulatedRawDocType, DepopulatedRawDocType>(
293
+ this: PopulatedDocumentMarker<PopulatedRawDocType, DepopulatedRawDocType>,
294
+ options: { depopulate: true }
295
+ ): Default__v<Require_id<DepopulatedRawDocType>, TSchemaOptions>;
296
+ toObject<PopulatedRawDocType, DepopulatedRawDocType, O extends ToObjectOptions & { depopulate: true }>(
297
+ this: PopulatedDocumentMarker<PopulatedRawDocType, DepopulatedRawDocType>,
298
+ options: O
299
+ ): ToObjectReturnType<DepopulatedRawDocType, TVirtuals, O, TSchemaOptions>;
300
+ toObject<PopulatedRawDocType, O extends ToObjectOptions>(
301
+ this: PopulatedDocumentMarker<PopulatedRawDocType, any>,
302
+ options: O
303
+ ): ToObjectReturnType<PopulatedRawDocType, TVirtuals, O, TSchemaOptions>;
304
+ toObject<PopulatedRawDocType>(
305
+ this: PopulatedDocumentMarker<PopulatedRawDocType, any>
306
+ ): Default__v<Require_id<PopulatedRawDocType>, TSchemaOptions>;
270
307
  toObject<O extends ToObjectOptions>(options: O): ToObjectReturnType<DocType, TVirtuals, O, TSchemaOptions>;
271
308
  toObject(options?: ToObjectOptions): Default__v<Require_id<DocType>, TSchemaOptions>;
272
309
  toObject<T>(options?: ToObjectOptions): Default__v<Require_id<T>, ResolveSchemaOptions<TSchemaOptions>>;
package/types/index.d.ts CHANGED
@@ -177,13 +177,25 @@ declare module 'mongoose' {
177
177
  HydratedDocPathsType,
178
178
  any,
179
179
  TOverrides extends Record<string, never> ?
180
- Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions> & AddDefaultId<HydratedDocPathsType, {}, TSchemaOptions> :
180
+ Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> &
181
+ Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions> &
182
+ IfEquals<
183
+ TVirtuals,
184
+ {},
185
+ AddDefaultId<HydratedDocPathsType, {}, TSchemaOptions>,
186
+ TVirtuals
187
+ > :
181
188
  IfAny<
182
189
  TOverrides,
183
190
  Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions>,
184
191
  Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & MergeType<
185
192
  Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions>,
186
- TOverrides
193
+ IfEquals<
194
+ TOverrides,
195
+ {},
196
+ TOverrides,
197
+ TOverrides & AddDefaultId<HydratedDocPathsType, TVirtuals, TSchemaOptions>
198
+ >
187
199
  >
188
200
  >
189
201
  >;
@@ -1094,6 +1106,9 @@ declare module 'mongoose' {
1094
1106
  // Handle DocumentArray - recurse into items
1095
1107
  : T extends Types.DocumentArray<infer ItemType>
1096
1108
  ? Types.DocumentArray<ApplyFlattenTransforms<ItemType, O>>
1109
+ // Handle plain arrays - recurse into items
1110
+ : T extends Array<infer ItemType>
1111
+ ? ApplyFlattenTransforms<ItemType, O>[]
1097
1112
  // Handle Subdocument - recurse into subdoc type
1098
1113
  : T extends Types.Subdocument<unknown, unknown, infer SubdocType>
1099
1114
  ? HydratedSingleSubdocument<ApplyFlattenTransforms<SubdocType, O>>
package/types/models.d.ts CHANGED
@@ -628,10 +628,10 @@ declare module 'mongoose' {
628
628
  populate<Paths>(
629
629
  docs: Array<any>,
630
630
  options: PopulateOptions | Array<PopulateOptions> | string
631
- ): Promise<Array<MergeType<THydratedDocumentType, Paths>>>;
631
+ ): Promise<Array<PopulateDocumentResult<THydratedDocumentType, Paths, PopulatedPathsDocumentType<TRawDocType, Paths>, TRawDocType>>>;
632
632
  populate<Paths>(
633
633
  doc: any, options: PopulateOptions | Array<PopulateOptions> | string
634
- ): Promise<MergeType<THydratedDocumentType, Paths>>;
634
+ ): Promise<PopulateDocumentResult<THydratedDocumentType, Paths, PopulatedPathsDocumentType<TRawDocType, Paths>, TRawDocType>>;
635
635
 
636
636
  /**
637
637
  * Update an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/).
@@ -8,6 +8,50 @@ declare module 'mongoose' {
8
8
  RawId extends RefType = (PopulatedType extends { _id?: RefType; } ? NonNullable<PopulatedType['_id']> : Types.ObjectId) | undefined
9
9
  > = PopulatedType | RawId;
10
10
 
11
+ const mongoosePopulatedDocumentMarker: unique symbol;
12
+
13
+ type ExtractDocumentObjectType<T> = T extends infer ObjectType & Document ? FlatRecord<ObjectType> : T;
14
+
15
+ type PopulatePathToRawDocType<T> =
16
+ T extends Types.DocumentArray<any, infer ItemType>
17
+ ? PopulatePathToRawDocType<ItemType>[]
18
+ : T extends Array<infer ItemType>
19
+ ? PopulatePathToRawDocType<ItemType>[]
20
+ : T extends Document
21
+ ? SubdocsToPOJOs<ExtractDocumentObjectType<T>>
22
+ : T extends Record<string, any>
23
+ ? { [K in keyof T]: PopulatePathToRawDocType<T[K]> }
24
+ : T;
25
+
26
+ type PopulatedPathsDocumentType<RawDocType, Paths> = UnpackedIntersection<RawDocType, PopulatePathToRawDocType<Paths>>;
27
+
28
+ type PopulatedDocumentMarker<
29
+ PopulatedRawDocType,
30
+ DepopulatedRawDocType,
31
+ > = {
32
+ [mongoosePopulatedDocumentMarker]?: {
33
+ populated: PopulatedRawDocType,
34
+ depopulated: DepopulatedRawDocType
35
+ }
36
+ };
37
+
38
+ type ResolvePopulatedRawDocType<
39
+ ThisType,
40
+ FallbackRawDocType,
41
+ O = never
42
+ > = ThisType extends PopulatedDocumentMarker<infer PopulatedRawDocType, infer DepopulatedRawDocType>
43
+ ? O extends { depopulate: true }
44
+ ? DepopulatedRawDocType
45
+ : PopulatedRawDocType
46
+ : FallbackRawDocType;
47
+
48
+ type PopulateDocumentResult<
49
+ Doc,
50
+ Paths,
51
+ PopulatedRawDocType,
52
+ DepopulatedRawDocType = PopulatedRawDocType
53
+ > = MergeType<Doc, Paths> & PopulatedDocumentMarker<PopulatedRawDocType, DepopulatedRawDocType>;
54
+
11
55
  interface PopulateOptions {
12
56
  /** space delimited path(s) to populate */
13
57
  path: string;
package/types/query.d.ts CHANGED
@@ -18,17 +18,34 @@ declare module 'mongoose' {
18
18
  ? DateQueryTypeCasting
19
19
  : T;
20
20
 
21
- export type ApplyBasicQueryCasting<T> = QueryTypeCasting<T> | QueryTypeCasting<T[]> | (T extends (infer U)[] ? QueryTypeCasting<U> : T) | null;
21
+ export type ApplyBasicQueryCasting<T> = QueryTypeCasting<T> | QueryTypeCasting<T>[] | (T extends (infer U)[] ? QueryTypeCasting<U> : T) | null;
22
+
23
+ type RemoveIndexSignature<T> = {
24
+ [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
25
+ };
26
+
27
+ type StrictFilterOperators<TValue> = RemoveIndexSignature<mongodb.FilterOperators<TValue>>;
28
+
29
+ type StrictCondition<T> = mongodb.AlternativeType<T> | StrictFilterOperators<mongodb.AlternativeType<T>>;
30
+
31
+ type _StrictFilter<TSchema> = {
32
+ [P in keyof TSchema]?: StrictCondition<ApplyBasicQueryCasting<TSchema[P]>>;
33
+ } & StrictRootFilterOperators<TSchema>;
34
+
35
+ type StrictRootFilterOperators<TSchema> = Omit<mongodb.RootFilterOperators<TSchema>, '$and' | '$or' | '$nor'> & {
36
+ $and?: _StrictFilter<TSchema>[];
37
+ $or?: _StrictFilter<TSchema>[];
38
+ $nor?: _StrictFilter<TSchema>[];
39
+ };
22
40
 
23
41
  type _QueryFilter<T> = (
24
- { [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } &
25
- mongodb.RootFilterOperators<{ [P in keyof mongodb.WithId<T>]?: ApplyBasicQueryCasting<mongodb.WithId<T>[P]>; }>
42
+ { [P in keyof T]?: StrictCondition<ApplyBasicQueryCasting<T[P]>>; } &
43
+ StrictRootFilterOperators<{ [P in keyof mongodb.WithId<T>]?: ApplyBasicQueryCasting<mongodb.WithId<T>[P]>; }>
26
44
  );
27
45
  type _QueryFilterLooseId<T> = (
28
- { [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } &
29
- mongodb.RootFilterOperators<
30
- { [P in keyof T]?: ApplyBasicQueryCasting<T[P]>; } &
31
- { _id?: any; }
46
+ { [P in keyof T]?: StrictCondition<ApplyBasicQueryCasting<T[P]>>; } &
47
+ StrictRootFilterOperators<
48
+ { [P in keyof T]?: ApplyBasicQueryCasting<T[P]>; }
32
49
  >
33
50
  );
34
51
  type QueryFilter<T> = IsItRecordAndNotAny<T> extends true ? _QueryFilter<WithLevel1NestedPaths<T>> : _QueryFilterLooseId<Record<string, any>>;
@@ -201,10 +218,10 @@ declare module 'mongoose' {
201
218
  ? ResultType
202
219
  : ResultType extends (infer U)[]
203
220
  ? U extends Document
204
- ? HydratedDocument<MergeType<RawDocType, Paths>, TDocOverrides, TQueryHelpers>[]
221
+ ? PopulateDocumentResult<U, Paths, MergeType<RawDocType, Paths>, RawDocType>[]
205
222
  : (MergeType<U, Paths>)[]
206
223
  : ResultType extends Document
207
- ? HydratedDocument<MergeType<RawDocType, Paths>, TDocOverrides, TQueryHelpers>
224
+ ? PopulateDocumentResult<ResultType, Paths, MergeType<RawDocType, Paths>, RawDocType>
208
225
  : MergeType<ResultType, Paths>
209
226
  : MergeType<ResultType, Paths>;
210
227
 
@@ -98,12 +98,12 @@ declare module 'mongoose' {
98
98
  type UnpackedIntersection<T, U> = T extends null
99
99
  ? null
100
100
  : T extends (infer A)[]
101
- ? (Omit<A, keyof U> & U)[]
101
+ ? (A extends any ? (Omit<A, keyof U> & U) : never)[]
102
102
  : keyof U extends never
103
103
  ? T
104
- : Omit<T, keyof U> & U;
104
+ : T extends any ? (Omit<T, keyof U> & U) : never;
105
105
 
106
- type MergeType<A, B> = Omit<A, keyof B> & B;
106
+ type MergeType<A, B> = A extends unknown ? Omit<A, keyof B> & B : never;
107
107
 
108
108
  /**
109
109
  * @summary Converts Unions to one record "object".
@@ -1,24 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Handles creating `{ type: 'object' }` vs `{ bsonType: 'object' }` vs `{ bsonType: ['object', 'null'] }`
5
- *
6
- * @param {string} type
7
- * @param {string} bsonType
8
- * @param {boolean} useBsonType
9
- * @param {boolean} isRequired
10
- */
11
-
12
- module.exports = function createJSONSchemaTypeArray(type, bsonType, useBsonType, isRequired) {
13
- if (useBsonType) {
14
- if (isRequired) {
15
- return { bsonType };
16
- }
17
- return { bsonType: [bsonType, 'null'] };
18
- } else {
19
- if (isRequired) {
20
- return { type };
21
- }
22
- return { type: [type, 'null'] };
23
- }
24
- };
File without changes