mongoose 9.12.3 → 9.13.1

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 (57) hide show
  1. package/lib/aggregate.js +4 -0
  2. package/lib/cast.js +4 -2
  3. package/lib/connection.js +10 -7
  4. package/lib/defaults/schemaDefaultsCache.js +62 -0
  5. package/lib/document.js +68 -52
  6. package/lib/filters/operators.js +8 -0
  7. package/lib/helpers/clone.js +9 -8
  8. package/lib/helpers/document/applyDefaults.js +1 -1
  9. package/lib/helpers/document/compile.js +2 -1
  10. package/lib/helpers/document/handleSpreadDoc.js +2 -1
  11. package/lib/helpers/getInheritedProperty.js +28 -0
  12. package/lib/helpers/getOwnProperty.js +19 -0
  13. package/lib/helpers/isMongooseDocument.js +24 -0
  14. package/lib/helpers/isMongooseObject.js +5 -3
  15. package/lib/helpers/model/discriminator.js +9 -3
  16. package/lib/helpers/parallelLimit.js +31 -11
  17. package/lib/helpers/populate/assignVals.js +10 -8
  18. package/lib/helpers/populate/getModelsMapForPopulate.js +9 -7
  19. package/lib/helpers/populate/getSchemaTypes.js +2 -1
  20. package/lib/helpers/populate/removeDeselectedForeignField.js +2 -1
  21. package/lib/helpers/query/castUpdate.js +5 -3
  22. package/lib/helpers/query/sanitizeFilter.js +2 -1
  23. package/lib/helpers/schematype/getConditionalHandler.js +23 -0
  24. package/lib/helpers/update/castArrayFilters.js +6 -5
  25. package/lib/helpers/update/removeUnusedArrayFilters.js +4 -0
  26. package/lib/helpers/updateValidators.js +5 -4
  27. package/lib/index.js +7 -0
  28. package/lib/internal.js +4 -0
  29. package/lib/model.js +28 -14
  30. package/lib/mongoose.js +17 -3
  31. package/lib/options/schemaArrayOptions.js +13 -2
  32. package/lib/query.js +74 -7
  33. package/lib/schema/array.js +17 -13
  34. package/lib/schema/bigint.js +2 -1
  35. package/lib/schema/boolean.js +2 -1
  36. package/lib/schema/buffer.js +10 -13
  37. package/lib/schema/date.js +2 -1
  38. package/lib/schema/documentArray.js +16 -9
  39. package/lib/schema/int32.js +2 -1
  40. package/lib/schema/number.js +2 -1
  41. package/lib/schema/string.js +2 -1
  42. package/lib/schema/subdocument.js +2 -1
  43. package/lib/schema/uuid.js +2 -1
  44. package/lib/schema.js +88 -41
  45. package/lib/schemaType.js +9 -2
  46. package/lib/types/array/methods/index.js +81 -3
  47. package/lib/types/map.js +8 -7
  48. package/lib/types/subdocument.js +5 -1
  49. package/lib/utils.js +8 -6
  50. package/package.json +2 -2
  51. package/types/models.d.ts +12 -4
  52. package/types/pipelinestage.d.ts +4 -2
  53. package/types/query.d.ts +1 -1
  54. package/types/schemaoptions.d.ts +7 -0
  55. package/types/utility.d.ts +117 -5
  56. package/tstyche.json +0 -7
  57. package/tstyche.matrix.json +0 -9
@@ -1,18 +1,130 @@
1
1
  declare module 'mongoose' {
2
+ import mongodb = require('mongodb');
3
+
2
4
  type IsNonDefiningProjection<Value, Key, Projection> = Value extends { $slice: any } | { $meta: any }
3
5
  ? true
4
6
  : Key extends '_id'
5
7
  ? Value extends 0 | false
6
8
  ? false
7
- // Including `_id` only defines inclusion when it is the sole projected field.
8
- : Exclude<keyof Projection, '_id'> extends never ? false : true
9
+ // Including `_id` only defines inclusion when no other key defines the mode.
10
+ : Exclude<Projection[Exclude<keyof Projection, '_id'>], { $slice: any } | { $meta: any }> extends never ? false : true
9
11
  : false;
10
12
  type ProjectionPath<Key> = Key extends `${infer Parent}.$` ? Parent : Key;
13
+ type ProjectionRoot<Key> = Key extends `${infer Root}.${string}` ? Root : Key;
14
+ type ProjectionSubpath<Key, Root extends string> = Key extends `${Root}.${infer Rest}` ? Rest : never;
15
+ // Values MongoDB treats as scalars: an inclusion path into a scalar removes it from the result, an exclusion
16
+ // path leaves it as is. `Error` is left out because plain objects with `name` and `message` also match it.
17
+ type ProjectionScalar = Exclude<TreatAsPrimitives, Error> | mongodb.BSON.BSONValue;
18
+ // Values with subpaths we can't narrow, because rebuilding them as plain objects would lose their methods.
19
+ type ProjectionOpaque = Map<any, any> | Types.Array<any> | Document;
20
+ type PickProjectedPaths<T, Keys> = IfAny<
21
+ T,
22
+ { [K in ProjectionRoot<Keys> & string]: any },
23
+ // Intersecting with `keyof T` rather than filtering `keyof T` so projected paths on index signatures are kept.
24
+ NarrowProjectedPaths<Pick<T, ProjectionRoot<Keys> & keyof T>, Keys>
25
+ >;
26
+ type NarrowProjectedPaths<T, Keys> = {
27
+ [K in keyof T as [Exclude<NarrowProjectedPath<T, K, Keys>, undefined>] extends [never] ? never : K]: NarrowProjectedPath<T, K, Keys>
28
+ };
29
+ type NarrowProjectedPath<T, K extends keyof T, Keys> = K extends Keys
30
+ ? T[K]
31
+ : PickNestedProjectedPaths<T[K], ProjectionSubpath<Keys, K & string>>;
32
+ // `undefined` means this value won't be in the result. MongoDB filters these out of arrays instead.
33
+ type PickNestedProjectedPaths<T, Keys> = IfAny<
34
+ T,
35
+ T,
36
+ T extends ProjectionScalar
37
+ ? undefined
38
+ : T extends Types.DocumentArray<infer Raw, infer Hydrated>
39
+ ? IfAny<Raw, T, ProjectedDocumentArray<Raw, Hydrated, PickProjectedPaths<Raw, Keys>>>
40
+ : T extends ProjectionOpaque
41
+ ? T
42
+ : T extends Array<infer Item>
43
+ ? NonNullable<PickNestedProjectedPaths<Item, Keys>>[]
44
+ : T extends AnyObject
45
+ ? EmptyIfNoKeys<PickProjectedPaths<T, Keys>>
46
+ : T
47
+ >;
48
+ // Only for nested paths: top-level results are intersected with `_id`, which `Record<string, never>` would make `never`.
49
+ type EmptyIfNoKeys<T> = [keyof T] extends [never] ? Record<string, never> : T;
50
+ // Swaps the raw paths on the hydrated subdocument for the narrowed ones, which keeps document methods and any custom
51
+ // methods. `_id` stays because `Types.Subdocument` requires it.
52
+ type ProjectedDocumentArray<Raw, Hydrated, Narrowed, Item = Omit<Hydrated, Exclude<keyof Raw, '_id'>> & Narrowed> =
53
+ Item extends Types.Subdocument<any, any, any> ? Types.DocumentArray<Narrowed, Item> : Types.DocumentArray<Narrowed>;
54
+ type OmitProjectedPaths<T, Keys> = IfAny<
55
+ T,
56
+ Omit<T, Extract<Keys, keyof T>>,
57
+ // A non-literal key could be any path, so we can't tell which paths the projection removes.
58
+ string extends Keys
59
+ ? T
60
+ : {
61
+ [K in keyof T as K extends Keys ? never : K]: K extends ProjectionRoot<Keys>
62
+ ? OmitNestedProjectedPaths<T[K], ProjectionSubpath<Keys, K & string>>
63
+ : T[K]
64
+ }
65
+ >;
66
+ type OmitNestedProjectedPaths<T, Keys> = IfAny<
67
+ T,
68
+ T,
69
+ T extends Types.DocumentArray<infer Raw, infer Hydrated>
70
+ ? IfAny<Raw, T, ProjectedDocumentArray<Raw, Hydrated, OmitProjectedPaths<Raw, Keys>>>
71
+ : T extends ProjectionScalar | ProjectionOpaque
72
+ ? T
73
+ : T extends Array<infer Item>
74
+ ? OmitNestedProjectedPaths<Item, Keys>[]
75
+ : T extends AnyObject
76
+ ? Extract<ProjectionRoot<Keys>, keyof T> extends never ? T : OmitProjectedPaths<T, Keys>
77
+ : T
78
+ >;
11
79
  type DefiningProjectionKeys<Projection> = {
12
80
  [Key in keyof Projection]-?: Key extends string
13
81
  ? IsNonDefiningProjection<Projection[Key], Key, Projection> extends true ? never : ProjectionPath<Key>
14
82
  : never
15
83
  }[keyof Projection];
84
+ // `$slice` and `$meta` don't set the mode, but in an inclusion projection their paths are part of the result.
85
+ type SliceAndMetaProjectionKeys<Projection> = {
86
+ [Key in keyof Projection]-?: Key extends string
87
+ ? Projection[Key] extends { $slice: any } | { $meta: any } ? ProjectionPath<Key> : never
88
+ : never
89
+ }[keyof Projection];
90
+ // `$meta` adds a path without setting the mode, creating any missing parent objects. Paths already in `T` keep their
91
+ // type: MongoDB overwrites them with the metadata, but hydrated documents cast it back to the schema type. Added paths
92
+ // are optional because hydrated documents only expose them through `get()` and `toObject()`, and MongoDB leaves the
93
+ // metadata out when it isn't available, like `indexKey` on a collection scan. `Prefix` is the path to `T`.
94
+ type AddMetaProjectionPaths<T, Projection, Prefix extends string = '', Keys = {
95
+ [Key in keyof Projection]-?: IfAny<
96
+ Projection[Key],
97
+ never,
98
+ Projection[Key] extends { $meta: any } ? Key extends `${Prefix}${infer Rest}` ? Rest : never : never
99
+ >
100
+ }[keyof Projection]> = IfAny<
101
+ T,
102
+ T,
103
+ [Keys] extends [never]
104
+ ? T
105
+ : {
106
+ [K in keyof T]: K extends Exclude<ProjectionRoot<Keys>, Keys>
107
+ ? AddNestedMetaProjectionPaths<T[K], Projection, `${Prefix}${K & string}.`>
108
+ : T[K]
109
+ } & {
110
+ [K in Exclude<ProjectionRoot<Keys>, keyof T> & string]?: K extends Keys
111
+ ? Projection[`${Prefix}${K}` & keyof Projection] extends { $meta: 'textScore' } ? number : any
112
+ : AddMetaProjectionPaths<{}, Projection, `${Prefix}${K}.`>
113
+ }
114
+ >;
115
+ type AddNestedMetaProjectionPaths<T, Projection, Prefix extends string> = IfAny<
116
+ T,
117
+ T,
118
+ T extends Types.DocumentArray<infer Raw, infer Hydrated>
119
+ ? IfAny<Raw, T, ProjectedDocumentArray<Raw, Hydrated, AddMetaProjectionPaths<Raw, Projection, Prefix>>>
120
+ : T extends ProjectionScalar | ProjectionOpaque
121
+ ? T
122
+ : T extends Array<infer Item>
123
+ ? AddNestedMetaProjectionPaths<Item, Projection, Prefix>[]
124
+ : T extends AnyObject
125
+ ? AddMetaProjectionPaths<T, Projection, Prefix>
126
+ : T
127
+ >;
16
128
  type DefiningProjectionValues<Projection> = {
17
129
  [Key in keyof Projection]-?: IsNonDefiningProjection<Projection[Key], Key, Projection> extends true ? never : Projection[Key]
18
130
  }[keyof Projection];
@@ -21,10 +133,10 @@ declare module 'mongoose' {
21
133
  ? T
22
134
  : Projection extends AnyObject
23
135
  ? [DefiningProjectionKeys<Projection>] extends [never]
24
- ? T
136
+ ? AddMetaProjectionPaths<T, Projection>
25
137
  : Exclude<DefiningProjectionValues<Projection>, 0 | false | undefined> extends never
26
- ? Omit<T, Extract<DefiningProjectionKeys<Projection>, keyof T>>
27
- : Pick<T, Extract<DefiningProjectionKeys<Projection>, keyof T>> &
138
+ ? OmitProjectedPaths<AddMetaProjectionPaths<T, Projection>, DefiningProjectionKeys<Projection>>
139
+ : PickProjectedPaths<AddMetaProjectionPaths<T, Projection>, DefiningProjectionKeys<Projection> | SliceAndMetaProjectionKeys<Projection>> &
28
140
  (Projection extends { _id?: infer Id }
29
141
  ? Id extends 0 | false
30
142
  ? unknown
package/tstyche.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "$schema": "https://tstyche.org/schemas/config.json",
3
- "testFileMatch": [
4
- "test/types/*.test.*",
5
- "test/types/**/*.test.*"
6
- ]
7
- }
@@ -1,9 +0,0 @@
1
- {
2
- "$schema": "https://tstyche.org/schemas/config.json",
3
- "checkDeclarationFiles": false,
4
- "checkSuppressedErrors": false,
5
- "testFileMatch": [
6
- "test/types/*.test.*",
7
- "test/types/**/*.test.*"
8
- ]
9
- }