@@ -13,6 +13,7 @@ import {
13
13
import {
14
14
CheckDuplicateEmbededReference ,
15
15
GetFieldNodeResultName ,
16
+ IsAny ,
16
17
IsRelationNullable ,
17
18
ResolveRelationship ,
18
19
SelectQueryError ,
@@ -33,7 +34,13 @@ export type GetResult<
33
34
RelationName ,
34
35
Relationships ,
35
36
Query extends string
36
- > = Relationships extends null // For .rpc calls the passed relationships will be null in that case, the result will always be the function return type
37
+ > = IsAny < Schema > extends true
38
+ ? ParseQuery < Query > extends infer ParsedQuery extends Ast . Node [ ]
39
+ ? RelationName extends string
40
+ ? ProcessNodesWithoutSchema < ParsedQuery >
41
+ : any
42
+ : any
43
+ : Relationships extends null // For .rpc calls the passed relationships will be null in that case, the result will always be the function return type
37
44
? ParseQuery < Query > extends infer ParsedQuery extends Ast . Node [ ]
38
45
? RPCCallNodes < ParsedQuery , RelationName extends string ? RelationName : 'rpc_call' , Row >
39
46
: Row
@@ -47,6 +54,71 @@ export type GetResult<
47
54
: ParsedQuery
48
55
: never
49
56
57
+ type ProcessSimpleFieldWithoutSchema < Field extends Ast . FieldNode > =
58
+ Field [ 'aggregateFunction' ] extends AggregateFunctions
59
+ ? {
60
+ // An aggregate function will always override the column name id.sum() will become sum
61
+ // except if it has been aliased
62
+ [ K in GetFieldNodeResultName < Field > ] : Field [ 'castType' ] extends PostgreSQLTypes
63
+ ? TypeScriptTypes < Field [ 'castType' ] >
64
+ : number
65
+ }
66
+ : {
67
+ // Aliases override the property name in the result
68
+ [ K in GetFieldNodeResultName < Field > ] : Field [ 'castType' ] extends PostgreSQLTypes // We apply the detected casted as the result type
69
+ ? TypeScriptTypes < Field [ 'castType' ] >
70
+ : any
71
+ }
72
+
73
+ type ProcessFieldNodeWithoutSchema < Node extends Ast . FieldNode > = IsNonEmptyArray <
74
+ Node [ 'children' ]
75
+ > extends true
76
+ ? {
77
+ [ K in Node [ 'name' ] ] : Node [ 'children' ] extends Ast . StarNode [ ]
78
+ ? any [ ]
79
+ : Node [ 'children' ] extends Ast . FieldNode [ ]
80
+ ? {
81
+ [ P in Node [ 'children' ] [ number ] as GetFieldNodeResultName < P > ] : P [ 'castType' ] extends PostgreSQLTypes
82
+ ? TypeScriptTypes < P [ 'castType' ] >
83
+ : any
84
+ } [ ]
85
+ : any [ ]
86
+ }
87
+ : ProcessSimpleFieldWithoutSchema < Node >
88
+
89
+ /**
90
+ * Processes a single Node without schema and returns the resulting TypeScript type.
91
+ */
92
+ type ProcessNodeWithoutSchema < Node extends Ast . Node > = Node extends Ast . StarNode
93
+ ? any
94
+ : Node extends Ast . SpreadNode
95
+ ? Node [ 'target' ] [ 'children' ] extends Ast . StarNode [ ]
96
+ ? any
97
+ : Node [ 'target' ] [ 'children' ] extends Ast . FieldNode [ ]
98
+ ? {
99
+ [ P in Node [ 'target' ] [ 'children' ] [ number ] as GetFieldNodeResultName < P > ] : P [ 'castType' ] extends PostgreSQLTypes
100
+ ? TypeScriptTypes < P [ 'castType' ] >
101
+ : any
102
+ }
103
+ : any
104
+ : Node extends Ast . FieldNode
105
+ ? ProcessFieldNodeWithoutSchema < Node >
106
+ : any
107
+
108
+ /**
109
+ * Processes nodes when Schema is any, providing basic type inference
110
+ */
111
+ type ProcessNodesWithoutSchema <
112
+ Nodes extends Ast . Node [ ] ,
113
+ Acc extends Record < string , unknown > = { }
114
+ > = Nodes extends [ infer FirstNode extends Ast . Node , ...infer RestNodes extends Ast . Node [ ] ]
115
+ ? ProcessNodeWithoutSchema < FirstNode > extends infer FieldResult
116
+ ? FieldResult extends Record < string , unknown >
117
+ ? ProcessNodesWithoutSchema < RestNodes , Acc & FieldResult >
118
+ : FieldResult
119
+ : any
120
+ : Prettify < Acc >
121
+
50
122
/**
51
123
* Processes a single Node from a select chained after a rpc call
52
124
*
0 commit comments