mongoose 9.3.2 → 9.4.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 (40) hide show
  1. package/lib/aggregate.js +1 -1
  2. package/lib/connection.js +20 -8
  3. package/lib/document.js +108 -48
  4. package/lib/drivers/node-mongodb-native/collection.js +37 -10
  5. package/lib/drivers/node-mongodb-native/connection.js +13 -0
  6. package/lib/error/index.js +1 -1
  7. package/lib/helpers/isBsonType.js +1 -1
  8. package/lib/helpers/setDefaultsOnInsert.js +1 -1
  9. package/lib/model.js +6 -3
  10. package/lib/mongoose.js +4 -4
  11. package/lib/query.js +1 -1
  12. package/lib/schema/array.js +1 -3
  13. package/lib/schema/bigint.js +1 -3
  14. package/lib/schema/boolean.js +1 -3
  15. package/lib/schema/buffer.js +1 -3
  16. package/lib/schema/date.js +1 -3
  17. package/lib/schema/decimal128.js +1 -3
  18. package/lib/schema/documentArray.js +2 -4
  19. package/lib/schema/double.js +1 -3
  20. package/lib/schema/int32.js +1 -3
  21. package/lib/schema/map.js +1 -4
  22. package/lib/schema/number.js +2 -4
  23. package/lib/schema/objectId.js +1 -3
  24. package/lib/schema/string.js +1 -3
  25. package/lib/schema/subdocument.js +1 -3
  26. package/lib/schema/union.js +50 -0
  27. package/lib/schema/uuid.js +1 -3
  28. package/lib/schema.js +5 -5
  29. package/lib/schemaType.js +46 -13
  30. package/lib/types/array/methods/index.js +4 -4
  31. package/lib/utils.js +12 -2
  32. package/package.json +6 -6
  33. package/types/document.d.ts +40 -3
  34. package/types/index.d.ts +17 -2
  35. package/types/models.d.ts +2 -2
  36. package/types/populate.d.ts +44 -0
  37. package/types/query.d.ts +26 -9
  38. package/types/utility.d.ts +3 -3
  39. package/lib/helpers/createJSONSchemaTypeDefinition.js +0 -24
  40. /package/{tstyche.config.json → tstyche.json} +0 -0
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