-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathquery-creator.ts
505 lines (492 loc) · 14.9 KB
/
query-creator.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import { QueryBuilder } from './query-builder/query-builder.js'
import { DeleteQueryNode } from './operation-node/delete-query-node.js'
import { InsertQueryNode } from './operation-node/insert-query-node.js'
import { SelectQueryNode } from './operation-node/select-query-node.js'
import { UpdateQueryNode } from './operation-node/update-query-node.js'
import {
parseTable,
parseTableExpression,
parseTableExpressionOrList,
QueryBuilderWithTable,
TableExpression,
TableExpressionOrList,
TableReference,
} from './parser/table-parser.js'
import {
InsertResultTypeTag,
DeleteResultTypeTag,
UpdateResultTypeTag,
} from './util/type-utils.js'
import { QueryExecutor } from './query-executor/query-executor.js'
import { RawBuilder } from './raw-builder/raw-builder.js'
import {
CommonTableExpression,
parseCommonTableExpression,
QueryCreatorWithCommonTableExpression,
} from './parser/with-parser.js'
import { WithNode } from './operation-node/with-node.js'
import { createQueryId } from './util/query-id.js'
import { WithSchemaPlugin } from './plugin/with-schema/with-schema-plugin.js'
import { freeze } from './util/object-utils.js'
import { ParseContext } from './parser/parse-context.js'
export class QueryCreator<DB> {
readonly #props: QueryCreatorProps
constructor(props: QueryCreatorProps) {
this.#props = freeze(props)
}
/**
* Creates a `select` query builder for the given table or tables.
*
* The tables passed to this method are built as the query's `from` clause.
*
* @example
* Create a select query for one table:
*
* ```ts
* db.selectFrom('person').selectAll()
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select * from "person"
* ```
*
* @example
* Create a select query for one table with an alias:
*
* ```ts
* const persons = await db.selectFrom('person as p')
* .select(['p.id', 'p.first_name'])
* .execute()
*
* console.log(persons[0].id)
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select "p"."id", "p"."first_name" from "person" as "p"
* ```
*
* @example
* Create a select query from a subquery:
*
* ```ts
* const persons = await db.selectFrom(
* db.selectFrom('person').select('person.id as identifier').as('p')
* )
* .select('p.identifier')
* .execute()
*
* console.log(persons[0].identifier)
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select "p"."identifier",
* from (
* select "person"."id" as "identifier" from "person"
* ) as p
* ```
*
* @example
* Create a select query from raw sql:
*
* ```ts
* const items = await db.selectFrom(
* db.raw<{ one: number }>('(select 1 as one)').as('q')
* )
* .select('q.one')
* .execute()
*
* console.log(items[0].one)
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select "q"."one",
* from (
* select 1 as one
* ) as q
* ```
*
* When you use `raw` you need to also provide the result type of the
* raw segment / query so that Kysely can figure out what columns are
* available for the query.
*
* @example
* The `selectFrom` method also accepts an array for multiple tables. All
* the above examples can also be used in an array.
*
* ```ts
* const items = await db.selectFrom([
* 'person as p',
* db.selectFrom('pet').select('pet.species').as('a'),
* db.raw<{ one: number }>('(select 1 as one)').as('q')
* ])
* .select(['p.id', 'a.species', 'q.one'])
* .execute()
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select "p".id, "a"."species", "q"."one"
* from
* "person" as "p",
* (select "pet"."species" from "pet") as a,
* (select 1 as one) as "q"
* ```
*/
selectFrom<F extends TableExpression<DB, keyof DB>>(
from: F[]
): QueryBuilderWithTable<DB, never, {}, F>
selectFrom<F extends TableExpression<DB, keyof DB>>(
from: F
): QueryBuilderWithTable<DB, never, {}, F>
selectFrom(from: TableExpressionOrList<any, any>): any {
return new QueryBuilder({
queryId: createQueryId(),
executor: this.#props.executor,
parseContext: this.#props.parseContext,
queryNode: SelectQueryNode.create(
parseTableExpressionOrList(this.#props.parseContext, from),
this.#props.withNode
),
})
}
/**
* Creates an insert query.
*
* The return value of this query is `number | undefined` because of the differences
* between database engines. Most engines (like Mysql) return the auto incrementing
* primary key (if it exists), but some (like postgres) return nothing by default.
* If you are running a database engine like `Mysql` that always returns the primary
* key, you can safely use `!` or the {@link QueryBuilder.castTo | castTo} method
* to cast away the `undefined` from the type.
*
* See the {@link QueryBuilder.values | values} method for more info and examples. Also see
* the {@link QueryBuilder.returning | returning} method for a way to return columns
* on supported databases like postgres.
*
* @example
* ```ts
* const maybePrimaryKey: number | undefined = await db
* .insertInto('person')
* .values({
* id: db.generated,
* first_name: 'Jennifer',
* last_name: 'Aniston'
* })
* .executeTakeFirst()
* ```
*
* @example
* Some databases like postgres support the `returning` method:
*
* ```ts
* const { id } = await db
* .insertInto('person')
* .values({
* id: db.generated,
* first_name: 'Jennifer',
* last_name: 'Aniston'
* })
* .returning('id')
* .executeTakeFirst()
* ```
*/
insertInto<T extends keyof DB & string>(
table: T
): QueryBuilder<DB, T, InsertResultTypeTag> {
return new QueryBuilder({
queryId: createQueryId(),
executor: this.#props.executor,
parseContext: this.#props.parseContext,
queryNode: InsertQueryNode.create(
parseTable(table),
this.#props.withNode
),
})
}
/**
* Creates a delete query.
*
* See the {@link QueryBuilder.where} method for examples on how to specify
* a where clause for the delete operation .
*
* @example
* ```ts
* const numAffectedRows = await db
* .deleteFrom('person')
* .where('person.id', '=', 1)
* .executeTakeFirst()
* ```
*/
deleteFrom<TR extends TableReference<DB>>(
table: TR
): QueryBuilderWithTable<DB, never, DeleteResultTypeTag, TR> {
return new QueryBuilder({
queryId: createQueryId(),
executor: this.#props.executor,
parseContext: this.#props.parseContext,
queryNode: DeleteQueryNode.create(
parseTableExpression(this.#props.parseContext, table),
this.#props.withNode
),
})
}
/**
* Creates an update query.
*
* See the {@link QueryBuilder.where} method for examples on how to specify
* a where clause for the update operation.
*
* See the {@link QueryBuilder.set} method for examples on how to
* specify the updates.
*
* @example
* ```ts
* const numAffectedRows = await db
* .updateTable('person')
* .set({ first_name: 'Jennifer' })
* .where('person.id', '=', 1)
* .executeTakeFirst()
* ```
*/
updateTable<TR extends TableReference<DB>>(
table: TR
): QueryBuilderWithTable<DB, never, UpdateResultTypeTag, TR> {
return new QueryBuilder({
queryId: createQueryId(),
executor: this.#props.executor,
parseContext: this.#props.parseContext,
queryNode: UpdateQueryNode.create(
parseTableExpression(this.#props.parseContext, table),
this.#props.withNode
),
})
}
/**
* Creates a `with` query (common table expressions).
*
* @example
* ```ts
* await db
* .with('jennifers', (db) => db
* .selectFrom('person')
* .where('first_name', '=', 'Jennifer')
* .select(['id', 'age'])
* )
* .with('adult_jennifers', (db) => db
* .selectFrom('jennifers')
* .where('age', '>', 18)
* .select(['id', 'age'])
* )
* .selectFrom('adult_jennifers')
* .where('age', '<', 60)
* .selectAll()
* .execute()
* ```
*/
with<N extends string, E extends CommonTableExpression<DB>>(
name: N,
expression: E
): QueryCreatorWithCommonTableExpression<DB, N, E> {
const cte = parseCommonTableExpression(
this.#props.parseContext,
name,
expression
)
return new QueryCreator({
...this.#props,
withNode: this.#props.withNode
? WithNode.cloneWithExpression(this.#props.withNode, cte)
: WithNode.create(cte),
})
}
/**
* Sets the schema to be used for all table references that don't explicitly
* specify a schema.
*
* This only affects the query created through the builder returned from
* this method and doesn't modify the `db` instance.
*
* @example
* ```
* await db.withSchema('mammals')
* .selectFrom('pet')
* .selectAll()
* .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
* .execute()
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select * from "mammals"."pet"
* inner join "public"."person"
* on "public"."person"."id" = "mammals"."pet"."owner_id"
* ```
*
* @example
* `withSchema` is smart enough to not add schema for aliases,
* common table expressions or other places where the schema
* doesn't belong to:
*
* ```
* await db.withSchema('mammals')
* .selectFrom('pet as p')
* .select('p.name')
* .execute()
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select "p"."name" from "mammals"."pet" as "p"
* ```
*/
withSchema(schema: string): QueryCreator<DB> {
return new QueryCreator({
...this.#props,
executor: this.#props.executor.withPluginAtFront(
new WithSchemaPlugin(schema)
),
})
}
/**
* Provides a way to pass arbitrary SQL into your query and executing completely
* raw queries.
*
* You can use the strings `?` and `??` in the `sql` to bind parameters such as
* user input to the SQL. You should never EVER concatenate untrusted user
* input to the SQL string to avoid injection vulnerabilities. Instead use `?`
* in place of a value and pass the actual value in the `parameters` list. See
* the examples below.
*
* You should only use `raw` when there is no other way to get the job done. This is
* because Kysely is not able to use type inference when you use raw SQL. For example
* Kysely won't be able to automatically provide you with the correct query result
* type. However, there are ways to manually provide types when you use `raw` in most
* cases. See the examples below.
*
* Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
* `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
*
* @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
* placeholders. `?` for values and `??` for identifiers such as column names
* or `column.table` references.
*
* @param params - The parameters that will be bound to the `?` and `??` placeholders in
* the sql string.
*
* @example
* Example of using `raw` in a select statement:
*
* ```ts
* const [person] = await db.selectFrom('person')
* .select(db.raw<string>('concat(first_name, ' ', last_name)').as('name'))
* .where('id', '=', 1)
* .execute()
*
* console.log(person.name)
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select concat(first_name, ' ', last_name) as "name"
* from "person" where "id" = 1
* ```
*
* The above example selects computed column `name` by concatenating the first name
* and last name together.
*
* There are couple of things worth noticing:
*
* 1. You need to provide the output type of your SQL segment for the `raw` method
* so that Kysely knows what type to give for the `name` column. In this case it's
* a `string` since that's the output type of the `concat` function in SQL.
*
* 2. You need to give an alias for the selection using the `as` method so that
* Kysely is able to add a column to the output type. The alias needs to be
* known at compile time! If you pass a string variable whose value is not known
* at compile time, there is no way for Kysely or typescript to add a column to
* the output type. In this case you need to use the `castTo` method on the query
* to specify a return type for the query.
*
* We could've also used a `??` placeholder to provide `first_name` and `last_name` like
* this:
*
* ```ts
* db.raw<string>('concat(??, ' ', ??)', ['first_name', 'last_name'])
* ```
*
* or this:
*
* ```ts
* db.raw<string>('concat(??, ' ', ??)', ['person.first_name', 'person.last_name'])
* ```
*
* But it's often cleaner to just write the column names in the SQL. Again remember to
* never concatenate column names or any other untrusted user input to the SQL string or you
* are going to create an injection vulnerability. All user input should go to the parameters
* array, never to the SQL string directly. But if the column names or values are trusted
* and known at compile time, there is no reason to use parameters.
*
* @example
* Example of using `raw` in `where`:
*
* ```ts
* function getPersonsOlderThan(ageLimit: number) {
* return await db.selectFrom('person')
* .selectAll()
* .where(
* db.raw('now() - birth_date'),
* '>',
* db.raw('interval ? year', [ageLimit.toString()])
* )
* .execute()
* }
* ```
*
* The generated SQL (postgresql):
*
* ```sql
* select * from "person" where now() - birth_date > interval $1 year
* ```
*
* The function in the above example returns people that are older than the given number of
* years. The number of years in this example is an untrusted user input, and therefore we use
* a `?` placeholder for it.
*
* @example
* Example of creating a completely raw query from scratch:
*
* ```ts
* const persons = await db.raw<Person>('select p.* from person p').execute()
* ```
*
* For a raw query, you need to specify the type of the returned __row__. In
* this case we know the resulting items will be of type `Person` se specify that.
* The result of `execute()` method is always an array. In this case the type of
* the `persons` variable is `Person[]`.
*/
raw<T = unknown>(sql: string, parameters?: any[]): RawBuilder<T> {
return new RawBuilder({
queryId: createQueryId(),
executor: this.#props.executor,
sql,
parameters,
})
}
}
export interface QueryCreatorProps {
readonly executor: QueryExecutor
readonly parseContext: ParseContext
readonly withNode?: WithNode
}