1
1
import type { CollectionImpl } from "../../collection.js"
2
2
import type { Aggregate , BasicExpression , OrderByDirection } from "../ir.js"
3
3
import type { QueryBuilder } from "./index.js"
4
+ import type { ResolveType } from "../../types.js"
4
5
5
6
export interface Context {
6
7
// The collections available in the base schema
@@ -27,13 +28,16 @@ export type Source = {
27
28
}
28
29
29
30
// Helper type to infer collection type from CollectionImpl
31
+ // This uses ResolveType directly to ensure consistency with collection creation logic
30
32
export type InferCollectionType < T > =
31
- T extends CollectionImpl < infer U > ? U : never
33
+ T extends CollectionImpl < infer U , any , any , infer TSchema , any >
34
+ ? ResolveType < U , TSchema , U >
35
+ : never
32
36
33
37
// Helper type to create schema from source
34
38
export type SchemaFromSource < T extends Source > = Prettify < {
35
- [ K in keyof T ] : T [ K ] extends CollectionImpl < infer U >
36
- ? U
39
+ [ K in keyof T ] : T [ K ] extends CollectionImpl < any , any , any , any , any >
40
+ ? InferCollectionType < T [ K ] >
37
41
: T [ K ] extends QueryBuilder < infer TContext >
38
42
? GetResult < TContext >
39
43
: never
@@ -58,16 +62,18 @@ export type SelectObject<
58
62
// Helper type to get the result type from a select object
59
63
export type ResultTypeFromSelect < TSelectObject > = {
60
64
[ K in keyof TSelectObject ] : TSelectObject [ K ] extends RefProxy < infer T >
61
- ? // For RefProxy, preserve the type as-is (including optionality from joins)
62
- T
65
+ ? T
63
66
: TSelectObject [ K ] extends BasicExpression < infer T >
64
67
? T
65
68
: TSelectObject [ K ] extends Aggregate < infer T >
66
69
? T
67
70
: TSelectObject [ K ] extends RefProxyFor < infer T >
68
- ? // For RefProxyFor, preserve the type as-is (including optionality from joins)
69
- T
70
- : never
71
+ ? T
72
+ : TSelectObject [ K ] extends undefined
73
+ ? undefined
74
+ : TSelectObject [ K ] extends { __type : infer U }
75
+ ? U
76
+ : never
71
77
}
72
78
73
79
// Callback type for orderBy clauses
@@ -142,22 +148,26 @@ export type RefProxyFor<T> = OmitRefProxy<
142
148
? // T is optional (T | undefined) but not exactly undefined
143
149
NonUndefined < T > extends Record < string , any >
144
150
? {
145
- // Properties are accessible and their types become optional
146
- [ K in keyof NonUndefined < T > ] : NonUndefined < T > [ K ] extends Record <
151
+ [ K in keyof NonUndefined < T > ] -?: NonUndefined < T > [ K ] extends Record <
147
152
string ,
148
153
any
149
154
>
150
- ? RefProxyFor < NonUndefined < T > [ K ] | undefined > &
155
+ ? RefProxyFor < NonUndefined < T > [ K ] > &
151
156
RefProxy < NonUndefined < T > [ K ] | undefined >
152
157
: RefProxy < NonUndefined < T > [ K ] | undefined >
153
158
} & RefProxy < T >
154
159
: RefProxy < T >
155
160
: // T is not optional
156
161
T extends Record < string , any >
157
162
? {
158
- [ K in keyof T ] : T [ K ] extends Record < string , any >
159
- ? RefProxyFor < T [ K ] > & RefProxy < T [ K ] >
160
- : RefProxy < T [ K ] >
163
+ // Make all properties required, but for optional ones, include undefined in the RefProxy type
164
+ [ K in keyof T ] -?: undefined extends T [ K ]
165
+ ? T [ K ] extends Record < string , any >
166
+ ? RefProxyFor < T [ K ] > & RefProxy < T [ K ] >
167
+ : RefProxy < T [ K ] >
168
+ : T [ K ] extends Record < string , any >
169
+ ? RefProxyFor < T [ K ] > & RefProxy < T [ K ] >
170
+ : RefProxy < T [ K ] >
161
171
} & RefProxy < T >
162
172
: RefProxy < T >
163
173
>
0 commit comments