kysely 0.16.11 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/dialect/mysql/mysql-adapter.js +3 -4
  3. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -0
  4. package/dist/cjs/dialect/mysql/mysql-introspector.js +2 -1
  5. package/dist/cjs/dialect/postgres/postgres-adapter.js +3 -2
  6. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +2 -1
  7. package/dist/cjs/driver/connection-provider.d.ts +1 -1
  8. package/dist/cjs/driver/default-connection-provider.d.ts +1 -1
  9. package/dist/cjs/driver/default-connection-provider.js +2 -2
  10. package/dist/cjs/driver/dummy-driver.d.ts +8 -0
  11. package/dist/cjs/driver/dummy-driver.js +8 -0
  12. package/dist/cjs/driver/single-connection-provider.d.ts +1 -1
  13. package/dist/cjs/driver/single-connection-provider.js +2 -2
  14. package/dist/cjs/index-nodeless.d.ts +2 -0
  15. package/dist/cjs/index-nodeless.js +2 -0
  16. package/dist/cjs/kysely.d.ts +3 -6
  17. package/dist/cjs/kysely.js +30 -41
  18. package/dist/cjs/migration/migrator.js +4 -4
  19. package/dist/cjs/operation-node/operation-node-transformer.js +1 -1
  20. package/dist/cjs/operation-node/raw-node.d.ts +4 -2
  21. package/dist/cjs/operation-node/raw-node.js +8 -2
  22. package/dist/cjs/parser/complex-expression-parser.d.ts +5 -5
  23. package/dist/cjs/parser/complex-expression-parser.js +5 -4
  24. package/dist/cjs/parser/filter-parser.d.ts +9 -9
  25. package/dist/cjs/parser/filter-parser.js +31 -30
  26. package/dist/cjs/parser/group-by-parser.d.ts +1 -2
  27. package/dist/cjs/parser/group-by-parser.js +2 -2
  28. package/dist/cjs/parser/insert-values-parser.d.ts +1 -2
  29. package/dist/cjs/parser/insert-values-parser.js +6 -6
  30. package/dist/cjs/parser/join-parser.d.ts +1 -2
  31. package/dist/cjs/parser/join-parser.js +8 -7
  32. package/dist/cjs/parser/order-by-parser.d.ts +1 -2
  33. package/dist/cjs/parser/order-by-parser.js +4 -4
  34. package/dist/cjs/parser/parse-utils.d.ts +10 -0
  35. package/dist/cjs/parser/parse-utils.js +38 -0
  36. package/dist/cjs/parser/reference-parser.d.ts +2 -3
  37. package/dist/cjs/parser/reference-parser.js +5 -5
  38. package/dist/cjs/parser/select-parser.d.ts +1 -2
  39. package/dist/cjs/parser/select-parser.js +5 -5
  40. package/dist/cjs/parser/table-parser.d.ts +2 -3
  41. package/dist/cjs/parser/table-parser.js +5 -5
  42. package/dist/cjs/parser/update-set-parser.d.ts +1 -2
  43. package/dist/cjs/parser/update-set-parser.js +2 -2
  44. package/dist/cjs/parser/value-parser.d.ts +3 -4
  45. package/dist/cjs/parser/value-parser.js +7 -7
  46. package/dist/cjs/parser/with-parser.d.ts +1 -2
  47. package/dist/cjs/parser/with-parser.js +3 -2
  48. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
  49. package/dist/cjs/query-builder/delete-query-builder.d.ts +34 -23
  50. package/dist/cjs/query-builder/delete-query-builder.js +15 -15
  51. package/dist/cjs/query-builder/expression-builder.d.ts +4 -13
  52. package/dist/cjs/query-builder/expression-builder.js +12 -21
  53. package/dist/cjs/query-builder/function-builder.d.ts +2 -6
  54. package/dist/cjs/query-builder/function-builder.js +5 -8
  55. package/dist/cjs/query-builder/insert-query-builder.d.ts +12 -8
  56. package/dist/cjs/query-builder/insert-query-builder.js +8 -7
  57. package/dist/cjs/query-builder/join-builder.d.ts +0 -2
  58. package/dist/cjs/query-builder/join-builder.js +8 -8
  59. package/dist/cjs/query-builder/on-conflict-builder.d.ts +46 -28
  60. package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
  61. package/dist/cjs/query-builder/returning-interface.d.ts +3 -1
  62. package/dist/cjs/query-builder/select-query-builder.d.ts +52 -33
  63. package/dist/cjs/query-builder/select-query-builder.js +28 -26
  64. package/dist/cjs/query-builder/update-query-builder.d.ts +41 -28
  65. package/dist/cjs/query-builder/update-query-builder.js +22 -20
  66. package/dist/cjs/query-builder/where-interface.d.ts +31 -20
  67. package/dist/cjs/query-compiler/default-query-compiler.js +1 -1
  68. package/dist/cjs/query-creator.d.ts +10 -129
  69. package/dist/cjs/query-creator.js +5 -136
  70. package/dist/cjs/query-executor/default-query-executor.d.ts +21 -7
  71. package/dist/cjs/query-executor/default-query-executor.js +17 -19
  72. package/dist/cjs/query-executor/noop-query-executor.d.ts +20 -6
  73. package/dist/cjs/query-executor/noop-query-executor.js +11 -4
  74. package/dist/cjs/query-executor/query-executor-base.d.ts +62 -0
  75. package/dist/cjs/query-executor/query-executor-base.js +46 -0
  76. package/dist/cjs/query-executor/query-executor-provider.d.ts +8 -0
  77. package/dist/cjs/query-executor/query-executor-provider.js +2 -0
  78. package/dist/cjs/query-executor/query-executor.d.ts +20 -13
  79. package/dist/cjs/query-executor/query-executor.js +0 -57
  80. package/dist/cjs/raw-builder/raw-builder.d.ts +42 -11
  81. package/dist/cjs/raw-builder/raw-builder.js +55 -56
  82. package/dist/cjs/raw-builder/sql.d.ts +331 -0
  83. package/dist/cjs/raw-builder/sql.js +161 -0
  84. package/dist/cjs/schema/alter-table-builder.d.ts +42 -8
  85. package/dist/cjs/schema/alter-table-builder.js +20 -0
  86. package/dist/cjs/schema/column-definition-builder.d.ts +22 -8
  87. package/dist/cjs/schema/create-index-builder.d.ts +3 -1
  88. package/dist/cjs/schema/create-index-builder.js +3 -1
  89. package/dist/cjs/schema/create-table-builder.d.ts +7 -3
  90. package/dist/cjs/schema/create-table-builder.js +7 -3
  91. package/dist/cjs/schema/create-view-builder.d.ts +3 -3
  92. package/dist/cjs/schema/create-view-builder.js +3 -3
  93. package/dist/cjs/util/query-id.js +10 -4
  94. package/dist/cjs/util/random-string.js +65 -2
  95. package/dist/cjs/util/type-utils.d.ts +1 -1
  96. package/dist/esm/dialect/mysql/mysql-adapter.js +3 -4
  97. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +19 -0
  98. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -1
  99. package/dist/esm/dialect/postgres/postgres-adapter.js +3 -2
  100. package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -1
  101. package/dist/esm/driver/connection-provider.d.ts +1 -1
  102. package/dist/esm/driver/default-connection-provider.d.ts +1 -1
  103. package/dist/esm/driver/default-connection-provider.js +2 -2
  104. package/dist/esm/driver/dummy-driver.d.ts +8 -0
  105. package/dist/esm/driver/dummy-driver.js +8 -0
  106. package/dist/esm/driver/single-connection-provider.d.ts +1 -1
  107. package/dist/esm/driver/single-connection-provider.js +2 -2
  108. package/dist/esm/index-nodeless.d.ts +2 -0
  109. package/dist/esm/index-nodeless.js +2 -0
  110. package/dist/esm/kysely.d.ts +3 -6
  111. package/dist/esm/kysely.js +30 -41
  112. package/dist/esm/migration/migrator.js +4 -4
  113. package/dist/esm/operation-node/operation-node-transformer.js +1 -1
  114. package/dist/esm/operation-node/raw-node.d.ts +4 -2
  115. package/dist/esm/operation-node/raw-node.js +8 -2
  116. package/dist/esm/parser/complex-expression-parser.d.ts +5 -5
  117. package/dist/esm/parser/complex-expression-parser.js +5 -4
  118. package/dist/esm/parser/filter-parser.d.ts +9 -9
  119. package/dist/esm/parser/filter-parser.js +31 -30
  120. package/dist/esm/parser/group-by-parser.d.ts +1 -2
  121. package/dist/esm/parser/group-by-parser.js +2 -2
  122. package/dist/esm/parser/insert-values-parser.d.ts +1 -2
  123. package/dist/esm/parser/insert-values-parser.js +6 -6
  124. package/dist/esm/parser/join-parser.d.ts +1 -2
  125. package/dist/esm/parser/join-parser.js +8 -7
  126. package/dist/esm/parser/order-by-parser.d.ts +1 -2
  127. package/dist/esm/parser/order-by-parser.js +4 -4
  128. package/dist/esm/parser/parse-utils.d.ts +10 -0
  129. package/dist/esm/parser/parse-utils.js +32 -0
  130. package/dist/esm/parser/reference-parser.d.ts +2 -3
  131. package/dist/esm/parser/reference-parser.js +5 -5
  132. package/dist/esm/parser/select-parser.d.ts +1 -2
  133. package/dist/esm/parser/select-parser.js +5 -5
  134. package/dist/esm/parser/table-parser.d.ts +2 -3
  135. package/dist/esm/parser/table-parser.js +5 -5
  136. package/dist/esm/parser/update-set-parser.d.ts +1 -2
  137. package/dist/esm/parser/update-set-parser.js +2 -2
  138. package/dist/esm/parser/value-parser.d.ts +3 -4
  139. package/dist/esm/parser/value-parser.js +7 -7
  140. package/dist/esm/parser/with-parser.d.ts +1 -2
  141. package/dist/esm/parser/with-parser.js +3 -2
  142. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
  143. package/dist/esm/query-builder/delete-query-builder.d.ts +34 -23
  144. package/dist/esm/query-builder/delete-query-builder.js +15 -15
  145. package/dist/esm/query-builder/expression-builder.d.ts +4 -13
  146. package/dist/esm/query-builder/expression-builder.js +12 -21
  147. package/dist/esm/query-builder/function-builder.d.ts +2 -6
  148. package/dist/esm/query-builder/function-builder.js +5 -8
  149. package/dist/esm/query-builder/insert-query-builder.d.ts +12 -8
  150. package/dist/esm/query-builder/insert-query-builder.js +8 -7
  151. package/dist/esm/query-builder/join-builder.d.ts +0 -2
  152. package/dist/esm/query-builder/join-builder.js +8 -8
  153. package/dist/esm/query-builder/on-conflict-builder.d.ts +46 -28
  154. package/dist/esm/query-builder/on-conflict-builder.js +17 -17
  155. package/dist/esm/query-builder/returning-interface.d.ts +3 -1
  156. package/dist/esm/query-builder/select-query-builder.d.ts +52 -33
  157. package/dist/esm/query-builder/select-query-builder.js +28 -26
  158. package/dist/esm/query-builder/update-query-builder.d.ts +41 -28
  159. package/dist/esm/query-builder/update-query-builder.js +22 -20
  160. package/dist/esm/query-builder/where-interface.d.ts +31 -20
  161. package/dist/esm/query-compiler/default-query-compiler.js +1 -1
  162. package/dist/esm/query-creator.d.ts +10 -129
  163. package/dist/esm/query-creator.js +5 -136
  164. package/dist/esm/query-executor/default-query-executor.d.ts +21 -7
  165. package/dist/esm/query-executor/default-query-executor.js +17 -19
  166. package/dist/esm/query-executor/noop-query-executor.d.ts +20 -6
  167. package/dist/esm/query-executor/noop-query-executor.js +10 -3
  168. package/dist/esm/query-executor/query-executor-base.d.ts +62 -0
  169. package/dist/esm/query-executor/query-executor-base.js +43 -0
  170. package/dist/esm/query-executor/query-executor-provider.d.ts +8 -0
  171. package/dist/esm/query-executor/query-executor-provider.js +2 -0
  172. package/dist/esm/query-executor/query-executor.d.ts +20 -13
  173. package/dist/esm/query-executor/query-executor.js +1 -55
  174. package/dist/esm/raw-builder/raw-builder.d.ts +42 -11
  175. package/dist/esm/raw-builder/raw-builder.js +56 -57
  176. package/dist/esm/raw-builder/sql.d.ts +331 -0
  177. package/dist/esm/raw-builder/sql.js +159 -0
  178. package/dist/esm/schema/alter-table-builder.d.ts +42 -8
  179. package/dist/esm/schema/alter-table-builder.js +20 -0
  180. package/dist/esm/schema/column-definition-builder.d.ts +22 -8
  181. package/dist/esm/schema/create-index-builder.d.ts +3 -1
  182. package/dist/esm/schema/create-index-builder.js +3 -1
  183. package/dist/esm/schema/create-table-builder.d.ts +7 -3
  184. package/dist/esm/schema/create-table-builder.js +7 -3
  185. package/dist/esm/schema/create-view-builder.d.ts +3 -3
  186. package/dist/esm/schema/create-view-builder.js +3 -3
  187. package/dist/esm/util/query-id.js +10 -4
  188. package/dist/esm/util/random-string.js +65 -2
  189. package/dist/esm/util/type-utils.d.ts +1 -1
  190. package/package.json +14 -14
  191. package/dist/cjs/parser/parse-context.d.ts +0 -55
  192. package/dist/cjs/parser/parse-context.js +0 -50
  193. package/dist/cjs/util/private-symbols.d.ts +0 -5
  194. package/dist/cjs/util/private-symbols.js +0 -8
  195. package/dist/esm/parser/parse-context.d.ts +0 -55
  196. package/dist/esm/parser/parse-context.js +0 -47
  197. package/dist/esm/util/private-symbols.d.ts +0 -5
  198. package/dist/esm/util/private-symbols.js +0 -6
@@ -4,10 +4,8 @@ import { DeleteQueryBuilder } from './query-builder/delete-query-builder.js';
4
4
  import { UpdateQueryBuilder } from './query-builder/update-query-builder.js';
5
5
  import { TableExpression, TableExpressionDatabase, TableExpressionTables, TableReference } from './parser/table-parser.js';
6
6
  import { QueryExecutor } from './query-executor/query-executor.js';
7
- import { RawBuilder } from './raw-builder/raw-builder.js';
8
7
  import { CommonTableExpression, QueryCreatorWithCommonTableExpression, RecursiveCommonTableExpression } from './parser/with-parser.js';
9
8
  import { WithNode } from './operation-node/with-node.js';
10
- import { ParseContext } from './parser/parse-context.js';
11
9
  import { InsertResult } from './query-builder/insert-result.js';
12
10
  import { DeleteResult } from './query-builder/delete-result.js';
13
11
  import { UpdateResult } from './query-builder/update-result.js';
@@ -73,9 +71,10 @@ export declare class QueryCreator<DB> {
73
71
  * Create a select query from raw sql:
74
72
  *
75
73
  * ```ts
76
- * const items = await db.selectFrom(
77
- * db.raw<{ one: number }>('(select 1 as one)').as('q')
78
- * )
74
+ * import { sql } from 'kysely'
75
+ *
76
+ * const items = await db
77
+ * .selectFrom(sql<{ one: number }>`(select 1 as one)`.as('q'))
79
78
  * .select('q.one')
80
79
  * .execute()
81
80
  *
@@ -91,18 +90,20 @@ export declare class QueryCreator<DB> {
91
90
  * ) as q
92
91
  * ```
93
92
  *
94
- * When you use `raw` you need to also provide the result type of the
95
- * raw segment / query so that Kysely can figure out what columns are
96
- * available for the query.
93
+ * When you use the `sql` tag you need to also provide the result type of the
94
+ * raw snippet / query so that Kysely can figure out what columns are
95
+ * available for the rest of the query.
97
96
  *
98
97
  * The `selectFrom` method also accepts an array for multiple tables. All
99
98
  * the above examples can also be used in an array.
100
99
  *
101
100
  * ```ts
101
+ * import { sql } from 'kysely'
102
+ *
102
103
  * const items = await db.selectFrom([
103
104
  * 'person as p',
104
105
  * db.selectFrom('pet').select('pet.species').as('a'),
105
- * db.raw<{ one: number }>('(select 1 as one)').as('q')
106
+ * sql<{ one: number }>`(select 1 as one)`.as('q')
106
107
  * ])
107
108
  * .select(['p.id', 'a.species', 'q.one'])
108
109
  * .execute()
@@ -299,128 +300,8 @@ export declare class QueryCreator<DB> {
299
300
  * ```
300
301
  */
301
302
  withSchema(schema: string): QueryCreator<DB>;
302
- /**
303
- * Provides a way to pass arbitrary SQL into your query and executing completely
304
- * raw queries.
305
- *
306
- * You can use the strings `?` and `??` in the `sql` to bind parameters such as
307
- * user input to the SQL. You should never EVER concatenate untrusted user
308
- * input to the SQL string to avoid injection vulnerabilities. Instead use `?`
309
- * in place of a value and pass the actual value in the `parameters` list. See
310
- * the examples below.
311
- *
312
- * You should only use `raw` when there is no other way to get the job done. This is
313
- * because Kysely is not able to use type inference when you use raw SQL. For example
314
- * Kysely won't be able to automatically provide you with the correct query result
315
- * type. However, there are ways to manually provide types when you use `raw` in most
316
- * cases. See the examples below.
317
- *
318
- * Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
319
- * `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
320
- *
321
- * ### Examples
322
- *
323
- * Example of using `raw` in a select statement:
324
- *
325
- * ```ts
326
- * const [person] = await db.selectFrom('person')
327
- * .select(db.raw<string>('concat(first_name, ' ', last_name)').as('name'))
328
- * .where('id', '=', 1)
329
- * .execute()
330
- *
331
- * console.log(person.name)
332
- * ```
333
- *
334
- * The generated SQL (PostgreSQL):
335
- *
336
- * ```sql
337
- * select concat(first_name, ' ', last_name) as "name"
338
- * from "person" where "id" = 1
339
- * ```
340
- *
341
- * The above example selects computed column `name` by concatenating the first name
342
- * and last name together.
343
- *
344
- * There are couple of things worth noticing:
345
- *
346
- * 1. You need to provide the output type of your SQL segment for the `raw` method
347
- * so that Kysely knows what type to give for the `name` column. In this case it's
348
- * a `string` since that's the output type of the `concat` function in SQL.
349
- *
350
- * 2. You need to give an alias for the selection using the `as` method so that
351
- * Kysely is able to add a column to the output type. The alias needs to be
352
- * known at compile time! If you pass a string variable whose value is not known
353
- * at compile time, there is no way for Kysely or typescript to add a column to
354
- * the output type. In this case you need to use the `castTo` method on the query
355
- * to specify a return type for the query.
356
- *
357
- * We could've also used a `??` placeholder to provide `first_name` and `last_name` like
358
- * this:
359
- *
360
- * ```ts
361
- * db.raw<string>('concat(??, ' ', ??)', ['first_name', 'last_name'])
362
- * ```
363
- *
364
- * or this:
365
- *
366
- * ```ts
367
- * db.raw<string>('concat(??, ' ', ??)', ['person.first_name', 'person.last_name'])
368
- * ```
369
- *
370
- * But it's often cleaner to just write the column names in the SQL. Again remember to
371
- * never concatenate column names or any other untrusted user input to the SQL string or you
372
- * are going to create an injection vulnerability. All user input should go to the parameters
373
- * array, never to the SQL string directly. But if the column names or values are trusted
374
- * and known at compile time, there is no reason to use parameters.
375
- *
376
- * Example of using `raw` in `where`:
377
- *
378
- * ```ts
379
- * function getPersonsOlderThan(ageLimit: number) {
380
- * return await db.selectFrom('person')
381
- * .selectAll()
382
- * .where(
383
- * db.raw('now() - birth_date'),
384
- * '>',
385
- * db.raw('interval ? year', [ageLimit.toString()])
386
- * )
387
- * .execute()
388
- * }
389
- * ```
390
- *
391
- * The generated SQL (PostgreSQL):
392
- *
393
- * ```sql
394
- * select * from "person" where now() - birth_date > interval $1 year
395
- * ```
396
- *
397
- * The function in the above example returns people that are older than the given number of
398
- * years. The number of years in this example is an untrusted user input, and therefore we use
399
- * a `?` placeholder for it.
400
- *
401
- * Example of creating a completely raw query from scratch:
402
- *
403
- * ```ts
404
- * const result = await db.raw<Person>('select p.* from person p').execute()
405
- * const persons = result.rows
406
- * ```
407
- *
408
- * For a raw query, you need to specify the type of the returned __row__. In
409
- * this case we know the resulting items will be of type `Person` se specify that.
410
- * The result of `execute()` method is always an array. In this case the type of
411
- * the `persons` variable is `Person[]`.
412
- *
413
- * @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
414
- * placeholders. `?` for values and `??` for identifiers such as column names
415
- * or `column.table` references.
416
- *
417
- * @param params - The parameters that will be bound to the `?` and `??` placeholders in
418
- * the sql string.
419
- */
420
- raw<T = unknown>(sql: string, parameters?: unknown[]): RawBuilder<T>;
421
303
  }
422
304
  export interface QueryCreatorProps {
423
305
  readonly executor: QueryExecutor;
424
- readonly parseContext: ParseContext;
425
306
  readonly withNode?: WithNode;
426
307
  }
@@ -8,7 +8,6 @@ import { InsertQueryNode } from './operation-node/insert-query-node.js';
8
8
  import { SelectQueryNode } from './operation-node/select-query-node.js';
9
9
  import { UpdateQueryNode } from './operation-node/update-query-node.js';
10
10
  import { parseTable, parseTableExpression, parseTableExpressionOrList, } from './parser/table-parser.js';
11
- import { RawBuilder } from './raw-builder/raw-builder.js';
12
11
  import { parseCommonTableExpression, } from './parser/with-parser.js';
13
12
  import { WithNode } from './operation-node/with-node.js';
14
13
  import { createQueryId } from './util/query-id.js';
@@ -23,8 +22,7 @@ export class QueryCreator {
23
22
  return new SelectQueryBuilder({
24
23
  queryId: createQueryId(),
25
24
  executor: this.#props.executor,
26
- parseContext: this.#props.parseContext,
27
- queryNode: SelectQueryNode.create(parseTableExpressionOrList(this.#props.parseContext, from), this.#props.withNode),
25
+ queryNode: SelectQueryNode.create(parseTableExpressionOrList(from), this.#props.withNode),
28
26
  });
29
27
  }
30
28
  /**
@@ -69,7 +67,6 @@ export class QueryCreator {
69
67
  return new InsertQueryBuilder({
70
68
  queryId: createQueryId(),
71
69
  executor: this.#props.executor,
72
- parseContext: this.#props.parseContext,
73
70
  queryNode: InsertQueryNode.create(parseTable(table), this.#props.withNode),
74
71
  });
75
72
  }
@@ -96,8 +93,7 @@ export class QueryCreator {
96
93
  return new DeleteQueryBuilder({
97
94
  queryId: createQueryId(),
98
95
  executor: this.#props.executor,
99
- parseContext: this.#props.parseContext,
100
- queryNode: DeleteQueryNode.create(parseTableExpression(this.#props.parseContext, table), this.#props.withNode),
96
+ queryNode: DeleteQueryNode.create(parseTableExpression(table), this.#props.withNode),
101
97
  });
102
98
  }
103
99
  /**
@@ -127,8 +123,7 @@ export class QueryCreator {
127
123
  return new UpdateQueryBuilder({
128
124
  queryId: createQueryId(),
129
125
  executor: this.#props.executor,
130
- parseContext: this.#props.parseContext,
131
- queryNode: UpdateQueryNode.create(parseTableExpression(this.#props.parseContext, table), this.#props.withNode),
126
+ queryNode: UpdateQueryNode.create(parseTableExpression(table), this.#props.withNode),
132
127
  });
133
128
  }
134
129
  /**
@@ -173,7 +168,7 @@ export class QueryCreator {
173
168
  * ```
174
169
  */
175
170
  with(name, expression) {
176
- const cte = parseCommonTableExpression(this.#props.parseContext, name, expression);
171
+ const cte = parseCommonTableExpression(name, expression);
177
172
  return new QueryCreator({
178
173
  ...this.#props,
179
174
  withNode: this.#props.withNode
@@ -187,7 +182,7 @@ export class QueryCreator {
187
182
  * See the {@link with} method for examples and more documentation.
188
183
  */
189
184
  withRecursive(name, expression) {
190
- const cte = parseCommonTableExpression(this.#props.parseContext, name, expression);
185
+ const cte = parseCommonTableExpression(name, expression);
191
186
  return new QueryCreator({
192
187
  ...this.#props,
193
188
  withNode: this.#props.withNode
@@ -248,130 +243,4 @@ export class QueryCreator {
248
243
  executor: this.#props.executor.withPluginAtFront(new WithSchemaPlugin(schema)),
249
244
  });
250
245
  }
251
- /**
252
- * Provides a way to pass arbitrary SQL into your query and executing completely
253
- * raw queries.
254
- *
255
- * You can use the strings `?` and `??` in the `sql` to bind parameters such as
256
- * user input to the SQL. You should never EVER concatenate untrusted user
257
- * input to the SQL string to avoid injection vulnerabilities. Instead use `?`
258
- * in place of a value and pass the actual value in the `parameters` list. See
259
- * the examples below.
260
- *
261
- * You should only use `raw` when there is no other way to get the job done. This is
262
- * because Kysely is not able to use type inference when you use raw SQL. For example
263
- * Kysely won't be able to automatically provide you with the correct query result
264
- * type. However, there are ways to manually provide types when you use `raw` in most
265
- * cases. See the examples below.
266
- *
267
- * Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
268
- * `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
269
- *
270
- * ### Examples
271
- *
272
- * Example of using `raw` in a select statement:
273
- *
274
- * ```ts
275
- * const [person] = await db.selectFrom('person')
276
- * .select(db.raw<string>('concat(first_name, ' ', last_name)').as('name'))
277
- * .where('id', '=', 1)
278
- * .execute()
279
- *
280
- * console.log(person.name)
281
- * ```
282
- *
283
- * The generated SQL (PostgreSQL):
284
- *
285
- * ```sql
286
- * select concat(first_name, ' ', last_name) as "name"
287
- * from "person" where "id" = 1
288
- * ```
289
- *
290
- * The above example selects computed column `name` by concatenating the first name
291
- * and last name together.
292
- *
293
- * There are couple of things worth noticing:
294
- *
295
- * 1. You need to provide the output type of your SQL segment for the `raw` method
296
- * so that Kysely knows what type to give for the `name` column. In this case it's
297
- * a `string` since that's the output type of the `concat` function in SQL.
298
- *
299
- * 2. You need to give an alias for the selection using the `as` method so that
300
- * Kysely is able to add a column to the output type. The alias needs to be
301
- * known at compile time! If you pass a string variable whose value is not known
302
- * at compile time, there is no way for Kysely or typescript to add a column to
303
- * the output type. In this case you need to use the `castTo` method on the query
304
- * to specify a return type for the query.
305
- *
306
- * We could've also used a `??` placeholder to provide `first_name` and `last_name` like
307
- * this:
308
- *
309
- * ```ts
310
- * db.raw<string>('concat(??, ' ', ??)', ['first_name', 'last_name'])
311
- * ```
312
- *
313
- * or this:
314
- *
315
- * ```ts
316
- * db.raw<string>('concat(??, ' ', ??)', ['person.first_name', 'person.last_name'])
317
- * ```
318
- *
319
- * But it's often cleaner to just write the column names in the SQL. Again remember to
320
- * never concatenate column names or any other untrusted user input to the SQL string or you
321
- * are going to create an injection vulnerability. All user input should go to the parameters
322
- * array, never to the SQL string directly. But if the column names or values are trusted
323
- * and known at compile time, there is no reason to use parameters.
324
- *
325
- * Example of using `raw` in `where`:
326
- *
327
- * ```ts
328
- * function getPersonsOlderThan(ageLimit: number) {
329
- * return await db.selectFrom('person')
330
- * .selectAll()
331
- * .where(
332
- * db.raw('now() - birth_date'),
333
- * '>',
334
- * db.raw('interval ? year', [ageLimit.toString()])
335
- * )
336
- * .execute()
337
- * }
338
- * ```
339
- *
340
- * The generated SQL (PostgreSQL):
341
- *
342
- * ```sql
343
- * select * from "person" where now() - birth_date > interval $1 year
344
- * ```
345
- *
346
- * The function in the above example returns people that are older than the given number of
347
- * years. The number of years in this example is an untrusted user input, and therefore we use
348
- * a `?` placeholder for it.
349
- *
350
- * Example of creating a completely raw query from scratch:
351
- *
352
- * ```ts
353
- * const result = await db.raw<Person>('select p.* from person p').execute()
354
- * const persons = result.rows
355
- * ```
356
- *
357
- * For a raw query, you need to specify the type of the returned __row__. In
358
- * this case we know the resulting items will be of type `Person` se specify that.
359
- * The result of `execute()` method is always an array. In this case the type of
360
- * the `persons` variable is `Person[]`.
361
- *
362
- * @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
363
- * placeholders. `?` for values and `??` for identifiers such as column names
364
- * or `column.table` references.
365
- *
366
- * @param params - The parameters that will be bound to the `?` and `??` placeholders in
367
- * the sql string.
368
- */
369
- raw(sql, parameters) {
370
- return new RawBuilder({
371
- queryId: createQueryId(),
372
- executor: this.#props.executor,
373
- sql,
374
- parameters,
375
- });
376
- }
377
246
  }
@@ -1,19 +1,33 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { QueryResult } from '../driver/database-connection.js';
2
+ import { DatabaseConnection } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode, QueryCompiler } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
- import { QueryExecutor } from './query-executor.js';
7
- export declare class DefaultQueryExecutor extends QueryExecutor {
6
+ import { QueryExecutorBase } from './query-executor-base.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
+ export declare class DefaultQueryExecutor extends QueryExecutorBase {
8
9
  #private;
9
- constructor(compiler: QueryCompiler, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
10
+ constructor(compiler: QueryCompiler, adapter: DialectAdapter, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
11
+ /**
12
+ * Returns the adapter for the current dialect.
13
+ */
14
+ get adapter(): DialectAdapter;
10
15
  /**
11
16
  * Compiles the transformed query into SQL. You usually want to pass
12
17
  * the output of {@link transformQuery} into this method but you can
13
18
  * compile any query using this method.
14
19
  */
15
20
  compileQuery(node: RootOperationNode): CompiledQuery;
16
- protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
21
+ /**
22
+ * Provides a connection for the callback and takes care of disposing
23
+ * the connection after the callback has been run.
24
+ */
25
+ provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
26
+ /**
27
+ * Returns a copy of this executor with a list of plugins added
28
+ * as the last plugins.
29
+ */
30
+ withPlugins(plugins: ReadonlyArray<KyselyPlugin>): DefaultQueryExecutor;
17
31
  /**
18
32
  * Returns a copy of this executor with a plugin added as the
19
33
  * last plugin.
@@ -27,9 +41,9 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
27
41
  /**
28
42
  * Returns a copy of this executor with a new connection provider.
29
43
  */
30
- withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
44
+ withConnectionProvider(connectionProvider: ConnectionProvider): DefaultQueryExecutor;
31
45
  /**
32
46
  * Returns a copy of this executor without any plugins.
33
47
  */
34
- withoutPlugins(): QueryExecutor;
48
+ withoutPlugins(): DefaultQueryExecutor;
35
49
  }
@@ -1,39 +1,37 @@
1
1
  /// <reference types="./default-query-executor.d.ts" />
2
- import { QueryExecutor } from './query-executor.js';
3
- export class DefaultQueryExecutor extends QueryExecutor {
2
+ import { QueryExecutorBase } from './query-executor-base.js';
3
+ export class DefaultQueryExecutor extends QueryExecutorBase {
4
4
  #compiler;
5
+ #adapter;
5
6
  #connectionProvider;
6
- constructor(compiler, connectionProvider, plugins = []) {
7
+ constructor(compiler, adapter, connectionProvider, plugins = []) {
7
8
  super(plugins);
8
9
  this.#compiler = compiler;
10
+ this.#adapter = adapter;
9
11
  this.#connectionProvider = connectionProvider;
10
12
  }
13
+ get adapter() {
14
+ return this.#adapter;
15
+ }
11
16
  compileQuery(node) {
12
17
  return this.#compiler.compileQuery(node);
13
18
  }
14
- async executeQueryImpl(compiledQuery) {
15
- return this.#connectionProvider.withConnection((connection) => {
16
- return connection.executeQuery(compiledQuery);
17
- });
19
+ provideConnection(consumer) {
20
+ return this.#connectionProvider.provideConnection(consumer);
21
+ }
22
+ withPlugins(plugins) {
23
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, ...plugins]);
18
24
  }
19
25
  withPlugin(plugin) {
20
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
21
- ...this.plugins,
22
- plugin,
23
- ]);
26
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, plugin]);
24
27
  }
25
28
  withPluginAtFront(plugin) {
26
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
27
- plugin,
28
- ...this.plugins,
29
- ]);
29
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [plugin, ...this.plugins]);
30
30
  }
31
31
  withConnectionProvider(connectionProvider) {
32
- return new DefaultQueryExecutor(this.#compiler, connectionProvider, [
33
- ...this.plugins,
34
- ]);
32
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, connectionProvider, [...this.plugins]);
35
33
  }
36
34
  withoutPlugins() {
37
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, []);
35
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, []);
38
36
  }
39
37
  }
@@ -1,29 +1,42 @@
1
- import { QueryResult } from '../driver/database-connection.js';
2
1
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
- import { QueryExecutor } from './query-executor.js';
4
2
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
3
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
4
+ import { QueryExecutorBase } from './query-executor-base.js';
5
5
  /**
6
6
  * A {@link QueryExecutor} subclass that can be used when you don't
7
7
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
8
8
  * other needed things to actually execute queries.
9
9
  */
10
- export declare class NoopQueryExecutor extends QueryExecutor {
10
+ export declare class NoopQueryExecutor extends QueryExecutorBase {
11
+ /**
12
+ * Returns the adapter for the current dialect.
13
+ */
14
+ get adapter(): DialectAdapter;
11
15
  /**
12
16
  * Compiles the transformed query into SQL. You usually want to pass
13
17
  * the output of {@link transformQuery} into this method but you can
14
18
  * compile any query using this method.
15
19
  */
16
20
  compileQuery(): CompiledQuery;
17
- protected executeQueryImpl<R>(): Promise<QueryResult<R>>;
21
+ /**
22
+ * Provides a connection for the callback and takes care of disposing
23
+ * the connection after the callback has been run.
24
+ */
25
+ provideConnection<T>(): Promise<T>;
18
26
  /**
19
27
  * Returns a copy of this executor with a new connection provider.
20
28
  */
21
- withConnectionProvider(): QueryExecutor;
29
+ withConnectionProvider(): NoopQueryExecutor;
22
30
  /**
23
31
  * Returns a copy of this executor with a plugin added as the
24
32
  * last plugin.
25
33
  */
26
34
  withPlugin(plugin: KyselyPlugin): NoopQueryExecutor;
35
+ /**
36
+ * Returns a copy of this executor with a list of plugins added
37
+ * as the last plugins.
38
+ */
39
+ withPlugins(plugins: ReadonlyArray<KyselyPlugin>): NoopQueryExecutor;
27
40
  /**
28
41
  * Returns a copy of this executor with a plugin added as the
29
42
  * first plugin.
@@ -32,5 +45,6 @@ export declare class NoopQueryExecutor extends QueryExecutor {
32
45
  /**
33
46
  * Returns a copy of this executor without any plugins.
34
47
  */
35
- withoutPlugins(): QueryExecutor;
48
+ withoutPlugins(): NoopQueryExecutor;
36
49
  }
50
+ export declare const NOOP_QUERY_EXECUTOR: NoopQueryExecutor;
@@ -1,15 +1,18 @@
1
1
  /// <reference types="./noop-query-executor.d.ts" />
2
- import { QueryExecutor } from './query-executor.js';
2
+ import { QueryExecutorBase } from './query-executor-base.js';
3
3
  /**
4
4
  * A {@link QueryExecutor} subclass that can be used when you don't
5
5
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
6
6
  * other needed things to actually execute queries.
7
7
  */
8
- export class NoopQueryExecutor extends QueryExecutor {
8
+ export class NoopQueryExecutor extends QueryExecutorBase {
9
+ get adapter() {
10
+ throw new Error('this query cannot be compiled to SQL');
11
+ }
9
12
  compileQuery() {
10
13
  throw new Error('this query cannot be compiled to SQL');
11
14
  }
12
- async executeQueryImpl() {
15
+ provideConnection() {
13
16
  throw new Error('this query cannot be executed');
14
17
  }
15
18
  withConnectionProvider() {
@@ -18,6 +21,9 @@ export class NoopQueryExecutor extends QueryExecutor {
18
21
  withPlugin(plugin) {
19
22
  return new NoopQueryExecutor([...this.plugins, plugin]);
20
23
  }
24
+ withPlugins(plugins) {
25
+ return new NoopQueryExecutor([...this.plugins, ...plugins]);
26
+ }
21
27
  withPluginAtFront(plugin) {
22
28
  return new NoopQueryExecutor([plugin, ...this.plugins]);
23
29
  }
@@ -25,3 +31,4 @@ export class NoopQueryExecutor extends QueryExecutor {
25
31
  return new NoopQueryExecutor([]);
26
32
  }
27
33
  }
34
+ export const NOOP_QUERY_EXECUTOR = new NoopQueryExecutor();
@@ -0,0 +1,62 @@
1
+ import { ConnectionProvider } from '../driver/connection-provider.js';
2
+ import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
3
+ import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
+ import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
+ import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
+ import { QueryId } from '../util/query-id.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
+ import { QueryExecutor } from './query-executor.js';
9
+ export declare abstract class QueryExecutorBase implements QueryExecutor {
10
+ #private;
11
+ constructor(plugins?: ReadonlyArray<KyselyPlugin>);
12
+ abstract get adapter(): DialectAdapter;
13
+ /**
14
+ * Returns all installed plugins.
15
+ */
16
+ get plugins(): ReadonlyArray<KyselyPlugin>;
17
+ /**
18
+ * Given the query the user has built (expressed as an operation node tree)
19
+ * this method runs it through all plugins' `transformQuery` methods and
20
+ * returns the result.
21
+ */
22
+ transformQuery<T extends RootOperationNode>(node: T, queryId: QueryId): T;
23
+ /**
24
+ * Compiles the transformed query into SQL. You usually want to pass
25
+ * the output of {@link transformQuery} into this method but you can
26
+ * compile any query using this method.
27
+ */
28
+ abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
29
+ /**
30
+ * Provides a connection for the callback and takes care of disposing
31
+ * the connection after the callback has been run.
32
+ */
33
+ abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
34
+ /**
35
+ * Executes a compiled query and runs the result through all plugins'
36
+ * `transformResult` method.
37
+ */
38
+ executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
39
+ /**
40
+ * Returns a copy of this executor with a new connection provider.
41
+ */
42
+ abstract withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutorBase;
43
+ /**
44
+ * Returns a copy of this executor with a plugin added as the
45
+ * last plugin.
46
+ */
47
+ abstract withPlugin(plugin: KyselyPlugin): QueryExecutorBase;
48
+ /**
49
+ * Returns a copy of this executor with a list of plugins added
50
+ * as the last plugins.
51
+ */
52
+ abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutorBase;
53
+ /**
54
+ * Returns a copy of this executor with a plugin added as the
55
+ * first plugin.
56
+ */
57
+ abstract withPluginAtFront(plugin: KyselyPlugin): QueryExecutorBase;
58
+ /**
59
+ * Returns a copy of this executor without any plugins.
60
+ */
61
+ abstract withoutPlugins(): QueryExecutorBase;
62
+ }