-
Notifications
You must be signed in to change notification settings - Fork 279
/
select-parser.ts
232 lines (219 loc) · 6.03 KB
/
select-parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { isOperationNodeSource } from '../operation-node/operation-node-source.js'
import { AliasedRawBuilder } from '../raw-builder/raw-builder.js'
import { isFunction, isReadonlyArray, isString } from '../util/object-utils.js'
import {
AliasedQueryBuilder,
QueryBuilder,
} from '../query-builder/query-builder.js'
import { SelectionNode } from '../operation-node/selection-node.js'
import {
AliasedQueryBuilderFactory,
AliasedRawBuilderFactory,
AnyAliasedColumn,
AnyAliasedColumnWithTable,
AnyAliasedQueryBuilder,
AnyAliasedRawBuilder,
AnyColumn,
AnyColumnWithTable,
InsertResultTypeTag,
RowType,
ValueType,
} from '../util/type-utils.js'
import { parseAliasedStringReference } from './reference-parser.js'
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js'
import { ParseContext } from './parse-context.js'
/**
* A selection expression.
*/
export type SelectExpression<DB, TB extends keyof DB> =
| AnyAliasedColumnWithTable<DB, TB>
| AnyAliasedColumn<DB, TB>
| AnyColumnWithTable<DB, TB>
| AnyColumn<DB, TB>
| AnyAliasedRawBuilder
| AliasedRawBuilderFactory<DB, TB>
| AnyAliasedQueryBuilder
| AliasedQueryBuilderFactory<DB, TB>
| DynamicReferenceBuilder<any>
export type SelectExpressionOrList<DB, TB extends keyof DB> =
| SelectExpression<DB, TB>
| ReadonlyArray<SelectExpression<DB, TB>>
/**
* Given a selection expression returns a query builder type that
* has the selection.
*/
export type QueryBuilderWithSelection<
DB,
TB extends keyof DB,
O,
S
> = QueryBuilder<
DB,
TB,
O extends InsertResultTypeTag ? InsertResultTypeTag : O & Selection<DB, TB, S>
>
/**
* `selectAll` output query builder type.
*/
export type SelectAllQueryBuilder<
DB,
TB extends keyof DB,
O,
S extends keyof DB
> = QueryBuilder<DB, TB, O & RowType<DB, S>>
export type Selection<DB, TB extends keyof DB, S> = {
[A in ExtractAliasFromSelectExpression<S>]: ExtractTypeFromSelectExpression<
DB,
TB,
S,
A
>
}
type ExtractAliasFromSelectExpression<S> = S extends string
? ExtractAliasFromStringSelectExpression<S>
: S extends AliasedRawBuilder<any, infer RA>
? RA
: S extends (qb: any) => AliasedRawBuilder<any, infer RA>
? RA
: S extends AliasedQueryBuilder<any, any, any, infer QA>
? QA
: S extends (qb: any) => AliasedQueryBuilder<any, any, any, infer QA>
? QA
: S extends DynamicReferenceBuilder<infer RA>
? ExtractAliasFromStringSelectExpression<RA>
: never
type ExtractAliasFromStringSelectExpression<S extends string> =
S extends `${string}.${string}.${string} as ${infer A}`
? A
: S extends `${string}.${string} as ${infer A}`
? A
: S extends `${string} as ${infer A}`
? A
: S extends `${string}.${string}.${infer C}`
? C
: S extends `${string}.${infer C}`
? C
: S
type ExtractTypeFromSelectExpression<
DB,
TB extends keyof DB,
S,
A extends keyof any
> = S extends string
? ExtractTypeFromStringSelectExpression<DB, TB, S, A>
: S extends AliasedRawBuilder<infer O, infer RA>
? RA extends A
? O
: never
: S extends (qb: any) => AliasedRawBuilder<infer O, infer RA>
? RA extends A
? O
: never
: S extends AliasedQueryBuilder<any, any, infer O, infer QA>
? QA extends A
? ValueType<O>
: never
: S extends (qb: any) => AliasedQueryBuilder<any, any, infer O, infer QA>
? QA extends A
? ValueType<O>
: never
: S extends DynamicReferenceBuilder<infer RA>
? A extends ExtractAliasFromStringSelectExpression<RA>
? ExtractTypeFromStringSelectExpression<DB, TB, RA, A> | undefined
: never
: never
type ExtractTypeFromStringSelectExpression<
DB,
TB extends keyof DB,
S extends string,
A extends keyof any,
R = RowType<DB, TB>
> = S extends `${infer SC}.${infer T}.${infer C} as ${infer RA}`
? RA extends A
? `${SC}.${T}` extends TB
? C extends keyof DB[`${SC}.${T}`]
? DB[`${SC}.${T}`][C]
: never
: never
: never
: S extends `${infer T}.${infer C} as ${infer RA}`
? RA extends A
? T extends TB
? C extends keyof DB[T]
? DB[T][C]
: never
: never
: never
: S extends `${infer C} as ${infer RA}`
? RA extends A
? C extends keyof R
? R[C]
: never
: never
: S extends `${infer SC}.${infer T}.${infer C}`
? C extends A
? `${SC}.${T}` extends TB
? C extends keyof DB[`${SC}.${T}`]
? DB[`${SC}.${T}`][C]
: never
: never
: never
: S extends `${infer T}.${infer C}`
? C extends A
? T extends TB
? C extends keyof DB[T]
? DB[T][C]
: never
: never
: never
: S extends A
? S extends keyof R
? R[S]
: never
: never
export function parseSelectExpressionOrList(
ctx: ParseContext,
selection: SelectExpressionOrList<any, any>
): SelectionNode[] {
if (isReadonlyArray(selection)) {
return selection.map((it) => parseSelectExpression(ctx, it))
} else {
return [parseSelectExpression(ctx, selection)]
}
}
function parseSelectExpression(
ctx: ParseContext,
selection: SelectExpression<any, any>
): SelectionNode {
if (isString(selection)) {
return SelectionNode.create(parseAliasedStringReference(selection))
} else if (isOperationNodeSource(selection)) {
return SelectionNode.create(selection.toOperationNode())
} else if (isFunction(selection)) {
return SelectionNode.create(
selection(ctx.createExpressionBuilder()).toOperationNode()
)
} else {
throw new Error(
`invalid value passed to select method: ${JSON.stringify(selection)}`
)
}
}
export function parseSelectAll(table?: string | string[]): SelectionNode[] {
if (!table) {
return [SelectionNode.createSelectAll()]
} else if (Array.isArray(table)) {
return table.map(parseSelectAllArg)
} else {
return [parseSelectAllArg(table)]
}
}
function parseSelectAllArg(table: string): SelectionNode {
if (isString(table)) {
return SelectionNode.createSelectAllFromTable(table)
} else {
throw new Error(
`invalid value passed to selectAll method: ${JSON.stringify(table)}`
)
}
}