-
Notifications
You must be signed in to change notification settings - Fork 280
/
function-module.ts
383 lines (370 loc) · 11.3 KB
/
function-module.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js'
import { AggregateFunctionNode } from '../operation-node/aggregate-function-node.js'
import {
CoalesceReferenceExpressionList,
parseCoalesce,
} from '../parser/coalesce-parser.js'
import {
ExtractTypeFromReferenceExpression,
SimpleReferenceExpression,
parseSimpleReferenceExpression,
ReferenceExpression,
StringReference,
} from '../parser/reference-parser.js'
import { RawBuilder } from '../raw-builder/raw-builder.js'
import { createQueryId } from '../util/query-id.js'
import { Equals, IsNever } from '../util/type-utils.js'
import { AggregateFunctionBuilder } from './aggregate-function-builder.js'
/**
* Helpers for type safe SQL function calls.
*
* You can always use the {@link sql} tag to call functions and build arbitrary
* expressions. This module simply has shortcuts for most common function calls.
*
* ### Examples
*
* ```ts
* const { count } = db.fn
*
* await db.selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* .select([
* 'person.id',
* count('pet.id').as('pet_count')
* ])
* .groupBy('person.id')
* .having(count('pet.id'), '>', 10)
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "person"."id", count("pet"."id") as "pet_count"
* from "person"
* inner join "pet" on "pet"."owner_id" = "person"."id"
* group by "person"."id"
* having count("pet"."id") > $1
* ```
*/
export class FunctionModule<DB, TB extends keyof DB> {
constructor() {
this.avg = this.avg.bind(this)
this.coalesce = this.coalesce.bind(this)
this.count = this.count.bind(this)
this.max = this.max.bind(this)
this.min = this.min.bind(this)
this.sum = this.sum.bind(this)
}
/**
* Calls the `avg` function for the column given as the argument.
*
* If this is used in a `select` statement the type of the selected expression
* will be `number | string` by default. This is because Kysely can't know the
* type the db driver outputs. Sometimes the output can be larger than the largest
* javascript number and a string is returned instead. Most drivers allow you
* to configure the output type of large numbers and Kysely can't know if you've
* done so.
*
* You can specify the output type of the expression by providing the type as
* the first type argument:
*
* ```ts
* const { avg } = db.fn
*
* db.selectFrom('toy')
* .select(avg<number>('price').as('avg_price'))
* .execute()
* ```
*
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
* was used. It is highly recommended to include null in the output type union
* and handle null values in post-execute code, or wrap the function with a `coalesce`
* function.
*
* ```ts
* const { avg } = db.fn
*
* db.selectFrom('toy')
* .select(avg<number | null>('price').as('avg_price'))
* .execute()
* ```
*/
avg<
O extends number | string | null = number | string,
C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<
DB,
TB
>
>(column: C): AggregateFunctionBuilder<DB, TB, O> {
return new AggregateFunctionBuilder({
aggregateFunctionNode: AggregateFunctionNode.create(
'avg',
parseSimpleReferenceExpression(column)
),
})
}
/**
* Calls the `coalesce` function for given arguments.
*
* This function returns the first non-null value from left to right, commonly
* used to provide a default scalar for nullable columns or functions.
*
* ```ts
* const { coalesce, max } = db.fn
*
* db.selectFrom('person')
* .select(coalesce(max('age'), sql<number>`0`).as('max_age'))
* .where('first_name', '=', 'Jennifer')
* .execute()
* ```
*
* The generated SQL (postgres):
*
* ```sql
* select coalesce(max("age"), 0) as "max_age" from "person" where "first_name" = $1
* ```
*
* If this is used in a `select` statement the type of the selected expression
* is inferred in the same manner that the function computes. A union of arguments'
* types - if a non-nullable argument exists, it stops there (ignoring any further
* arguments' types) and exludes null from the final union type.
*
* Examples:
*
* `(string | null, number | null)` is inferred as `string | number | null`.
*
* `(string | null, number, Date | null)` is inferred as `string | number`.
*
* `(number, string | null)` is inferred as `number`.
*/
coalesce<
V extends ReferenceExpression<DB, TB>,
OV extends ReferenceExpression<DB, TB>[]
>(
value: V,
...otherValues: OV
): RawBuilder<CoalesceReferenceExpressionList<DB, TB, [V, ...OV]>> {
return new RawBuilder({
queryId: createQueryId(),
rawNode: parseCoalesce([value, ...otherValues]),
})
}
/**
* Calls the `count` function for the column given as the argument.
*
* If this is used in a `select` statement the type of the selected expression
* will be `number | string | bigint` by default. This is because Kysely
* can't know the type the db driver outputs. Sometimes the output can be larger
* than the largest javascript number and a string is returned instead. Most
* drivers allow you to configure the output type of large numbers and Kysely
* can't know if you've done so.
*
* You can specify the output type of the expression by providing
* the type as the first type argument:
*
* ```ts
* const { count } = db.fn
*
* db.selectFrom('toy')
* .select(count<number>('id').as('num_toys'))
* .execute()
* ```
*
* You can limit column range:
*
* ```ts
* db.selectFrom('toy')
* .select(qb => qb.fn.count<number>('id').as('num_toys'))
* .execute()
* ```
*/
count<
O extends number | string | bigint,
C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<
DB,
TB
>
>(column: C): AggregateFunctionBuilder<DB, TB, O> {
return new AggregateFunctionBuilder({
aggregateFunctionNode: AggregateFunctionNode.create(
'count',
parseSimpleReferenceExpression(column)
),
})
}
/**
* Calls the `max` function for the column given as the argument.
*
* If this is used in a `select` statement the type of the selected expression
* will be the referenced column's type.
*
* ### Examples
*
* ```ts
* const { max } = db.fn
*
* db.selectFrom('toy')
* .select(max('price').as('max_price'))
* .execute()
* ```
*
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
* was used. It is highly recommended to include null in the output type union
* and handle null values in post-execute code, or wrap the function with a `coalesce`
* function.
*
* ```ts
* const { max } = db.fn
*
* db.selectFrom('toy')
* .select(max<number | null, 'price'>('price').as('max_price'))
* .execute()
* ```
*/
max<
O extends number | string | bigint | null = never,
C extends StringReference<DB, TB> = StringReference<DB, TB>
>(
column: OutputBoundStringReference<DB, TB, C, O>
): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>
max<O extends number | string | bigint | null = number | string | bigint>(
column: DynamicReferenceBuilder
): AggregateFunctionBuilder<DB, TB, O>
max<
C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<
DB,
TB
>
>(column: C): any {
return new AggregateFunctionBuilder({
aggregateFunctionNode: AggregateFunctionNode.create(
'max',
parseSimpleReferenceExpression(column)
),
})
}
/**
* Calls the `min` function for the column given as the argument.
*
* If this is used in a `select` statement the type of the selected expression
* will be the referenced column's type.
*
* ### Examples
*
* ```ts
* const { min } = db.fn
*
* db.selectFrom('toy')
* .select(min('price').as('min_price'))
* .execute()
* ```
*
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
* was used. It is highly recommended to include null in the output type union
* and handle null values in post-execute code, or wrap the function with a `coalesce`
* function.
*
* ```ts
* const { min } = db.fn
*
* db.selectFrom('toy')
* .select(min<number | null, 'price'>('price').as('min_price'))
* .execute()
* ```
*/
min<
O extends number | string | bigint | null = never,
C extends StringReference<DB, TB> = StringReference<DB, TB>
>(
column: OutputBoundStringReference<DB, TB, C, O>
): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>
min<O extends number | string | bigint | null = number | string | bigint>(
column: DynamicReferenceBuilder
): AggregateFunctionBuilder<DB, TB, O>
min<
C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<
DB,
TB
>
>(column: C): any {
return new AggregateFunctionBuilder({
aggregateFunctionNode: AggregateFunctionNode.create(
'min',
parseSimpleReferenceExpression(column)
),
})
}
/**
* Calls the `sum` function for the column given as the argument.
*
* If this is used in a `select` statement the type of the selected expression
* will be `number | string | bigint` by default. This is because Kysely
* can't know the type the db driver outputs. Sometimes the output can be larger
* than the largest javascript number and a string is returned instead. Most
* drivers allow you to configure the output type of large numbers and Kysely
* can't know if you've done so.
*
* You can specify the output type of the expression by providing
* the type as the first type argument:
*
* ```ts
* const { sum } = db.fn
*
* db.selectFrom('toy')
* .select(sum<number>('price').as('total_price'))
* .execute()
* ```
*
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
* was used. It is highly recommended to include null in the output type union
* and handle null values in post-execute code, or wrap the function with a `coalesce`
* function.
*
* ```ts
* const { sum } = db.fn
*
* db.selectFrom('toy')
* .select(sum<number | null>('price').as('total_price'))
* .execute()
* ```
*/
sum<
O extends number | string | bigint | null = number | string | bigint,
C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<
DB,
TB
>
>(column: C): AggregateFunctionBuilder<DB, TB, O> {
return new AggregateFunctionBuilder({
aggregateFunctionNode: AggregateFunctionNode.create(
'sum',
parseSimpleReferenceExpression(column)
),
})
}
}
type OutputBoundStringReference<
DB,
TB extends keyof DB,
C extends StringReference<DB, TB>,
O
> = IsNever<O> extends true
? C // output not provided, unbound
: Equals<
ExtractTypeFromReferenceExpression<DB, TB, C> | null,
O | null
> extends true
? C
: never
type StringReferenceBoundAggregateFunctionBuilder<
DB,
TB extends keyof DB,
C extends StringReference<DB, TB>,
O
> = AggregateFunctionBuilder<
DB,
TB,
| ExtractTypeFromReferenceExpression<DB, TB, C>
| (null extends O ? null : never) // output is nullable, but column type might not be nullable.
>