kysely 0.16.9 → 0.17.1

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 (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -2
  3. package/dist/cjs/dialect/mysql/mysql-adapter.js +3 -4
  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 +1 -1
  10. package/dist/cjs/driver/single-connection-provider.d.ts +1 -1
  11. package/dist/cjs/driver/single-connection-provider.js +1 -1
  12. package/dist/cjs/index-nodeless.d.ts +2 -0
  13. package/dist/cjs/index-nodeless.js +2 -0
  14. package/dist/cjs/kysely.d.ts +3 -6
  15. package/dist/cjs/kysely.js +30 -41
  16. package/dist/cjs/migration/migrator.js +4 -4
  17. package/dist/cjs/operation-node/delete-query-node.d.ts +7 -0
  18. package/dist/cjs/operation-node/delete-query-node.js +15 -0
  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/query-builder/delete-query-builder.d.ts +77 -20
  49. package/dist/cjs/query-builder/delete-query-builder.js +81 -14
  50. package/dist/cjs/query-builder/expression-builder.d.ts +4 -13
  51. package/dist/cjs/query-builder/expression-builder.js +12 -21
  52. package/dist/cjs/query-builder/function-builder.d.ts +2 -6
  53. package/dist/cjs/query-builder/function-builder.js +5 -8
  54. package/dist/cjs/query-builder/insert-query-builder.d.ts +11 -9
  55. package/dist/cjs/query-builder/insert-query-builder.js +8 -7
  56. package/dist/cjs/query-builder/join-builder.d.ts +0 -2
  57. package/dist/cjs/query-builder/join-builder.js +8 -8
  58. package/dist/cjs/query-builder/on-conflict-builder.d.ts +24 -20
  59. package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
  60. package/dist/cjs/query-builder/returning-interface.d.ts +2 -2
  61. package/dist/cjs/query-builder/select-query-builder.d.ts +43 -32
  62. package/dist/cjs/query-builder/select-query-builder.js +28 -26
  63. package/dist/cjs/query-builder/update-query-builder.d.ts +29 -25
  64. package/dist/cjs/query-builder/update-query-builder.js +22 -20
  65. package/dist/cjs/query-builder/where-interface.d.ts +20 -16
  66. package/dist/cjs/query-compiler/default-query-compiler.js +16 -7
  67. package/dist/cjs/query-creator.d.ts +24 -138
  68. package/dist/cjs/query-creator.js +19 -145
  69. package/dist/cjs/query-executor/default-query-executor.d.ts +14 -3
  70. package/dist/cjs/query-executor/default-query-executor.js +15 -17
  71. package/dist/cjs/query-executor/noop-query-executor.d.ts +13 -2
  72. package/dist/cjs/query-executor/noop-query-executor.js +9 -2
  73. package/dist/cjs/query-executor/query-executor-provider.d.ts +8 -0
  74. package/dist/cjs/query-executor/query-executor-provider.js +2 -0
  75. package/dist/cjs/query-executor/query-executor.d.ts +14 -6
  76. package/dist/cjs/query-executor/query-executor.js +4 -2
  77. package/dist/cjs/raw-builder/raw-builder.d.ts +42 -11
  78. package/dist/cjs/raw-builder/raw-builder.js +55 -56
  79. package/dist/cjs/raw-builder/sql.d.ts +323 -0
  80. package/dist/cjs/raw-builder/sql.js +153 -0
  81. package/dist/cjs/schema/alter-table-builder.d.ts +20 -8
  82. package/dist/cjs/schema/alter-table-builder.js +3 -3
  83. package/dist/cjs/schema/column-definition-builder.d.ts +20 -8
  84. package/dist/cjs/schema/create-index-builder.d.ts +3 -1
  85. package/dist/cjs/schema/create-index-builder.js +3 -1
  86. package/dist/cjs/schema/create-table-builder.d.ts +6 -2
  87. package/dist/cjs/schema/create-table-builder.js +8 -4
  88. package/dist/cjs/schema/create-view-builder.d.ts +3 -3
  89. package/dist/cjs/schema/create-view-builder.js +3 -3
  90. package/dist/cjs/util/query-id.js +10 -4
  91. package/dist/cjs/util/random-string.js +65 -2
  92. package/dist/esm/dialect/mysql/mysql-adapter.js +3 -4
  93. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -1
  94. package/dist/esm/dialect/postgres/postgres-adapter.js +3 -2
  95. package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -1
  96. package/dist/esm/driver/connection-provider.d.ts +1 -1
  97. package/dist/esm/driver/default-connection-provider.d.ts +1 -1
  98. package/dist/esm/driver/default-connection-provider.js +1 -1
  99. package/dist/esm/driver/single-connection-provider.d.ts +1 -1
  100. package/dist/esm/driver/single-connection-provider.js +1 -1
  101. package/dist/esm/index-nodeless.d.ts +2 -0
  102. package/dist/esm/index-nodeless.js +2 -0
  103. package/dist/esm/kysely.d.ts +3 -6
  104. package/dist/esm/kysely.js +30 -41
  105. package/dist/esm/migration/migrator.js +4 -4
  106. package/dist/esm/operation-node/delete-query-node.d.ts +7 -0
  107. package/dist/esm/operation-node/delete-query-node.js +15 -0
  108. package/dist/esm/operation-node/operation-node-transformer.js +1 -1
  109. package/dist/esm/operation-node/raw-node.d.ts +4 -2
  110. package/dist/esm/operation-node/raw-node.js +8 -2
  111. package/dist/esm/parser/complex-expression-parser.d.ts +5 -5
  112. package/dist/esm/parser/complex-expression-parser.js +5 -4
  113. package/dist/esm/parser/filter-parser.d.ts +9 -9
  114. package/dist/esm/parser/filter-parser.js +31 -30
  115. package/dist/esm/parser/group-by-parser.d.ts +1 -2
  116. package/dist/esm/parser/group-by-parser.js +2 -2
  117. package/dist/esm/parser/insert-values-parser.d.ts +1 -2
  118. package/dist/esm/parser/insert-values-parser.js +6 -6
  119. package/dist/esm/parser/join-parser.d.ts +1 -2
  120. package/dist/esm/parser/join-parser.js +8 -7
  121. package/dist/esm/parser/order-by-parser.d.ts +1 -2
  122. package/dist/esm/parser/order-by-parser.js +4 -4
  123. package/dist/esm/parser/parse-utils.d.ts +10 -0
  124. package/dist/esm/parser/parse-utils.js +32 -0
  125. package/dist/esm/parser/reference-parser.d.ts +2 -3
  126. package/dist/esm/parser/reference-parser.js +5 -5
  127. package/dist/esm/parser/select-parser.d.ts +1 -2
  128. package/dist/esm/parser/select-parser.js +5 -5
  129. package/dist/esm/parser/table-parser.d.ts +2 -3
  130. package/dist/esm/parser/table-parser.js +5 -5
  131. package/dist/esm/parser/update-set-parser.d.ts +1 -2
  132. package/dist/esm/parser/update-set-parser.js +2 -2
  133. package/dist/esm/parser/value-parser.d.ts +3 -4
  134. package/dist/esm/parser/value-parser.js +7 -7
  135. package/dist/esm/parser/with-parser.d.ts +1 -2
  136. package/dist/esm/parser/with-parser.js +3 -2
  137. package/dist/esm/query-builder/delete-query-builder.d.ts +77 -20
  138. package/dist/esm/query-builder/delete-query-builder.js +81 -14
  139. package/dist/esm/query-builder/expression-builder.d.ts +4 -13
  140. package/dist/esm/query-builder/expression-builder.js +12 -21
  141. package/dist/esm/query-builder/function-builder.d.ts +2 -6
  142. package/dist/esm/query-builder/function-builder.js +5 -8
  143. package/dist/esm/query-builder/insert-query-builder.d.ts +11 -9
  144. package/dist/esm/query-builder/insert-query-builder.js +8 -7
  145. package/dist/esm/query-builder/join-builder.d.ts +0 -2
  146. package/dist/esm/query-builder/join-builder.js +8 -8
  147. package/dist/esm/query-builder/on-conflict-builder.d.ts +24 -20
  148. package/dist/esm/query-builder/on-conflict-builder.js +17 -17
  149. package/dist/esm/query-builder/returning-interface.d.ts +2 -2
  150. package/dist/esm/query-builder/select-query-builder.d.ts +43 -32
  151. package/dist/esm/query-builder/select-query-builder.js +28 -26
  152. package/dist/esm/query-builder/update-query-builder.d.ts +29 -25
  153. package/dist/esm/query-builder/update-query-builder.js +22 -20
  154. package/dist/esm/query-builder/where-interface.d.ts +20 -16
  155. package/dist/esm/query-compiler/default-query-compiler.js +16 -7
  156. package/dist/esm/query-creator.d.ts +24 -138
  157. package/dist/esm/query-creator.js +19 -145
  158. package/dist/esm/query-executor/default-query-executor.d.ts +14 -3
  159. package/dist/esm/query-executor/default-query-executor.js +15 -17
  160. package/dist/esm/query-executor/noop-query-executor.d.ts +13 -2
  161. package/dist/esm/query-executor/noop-query-executor.js +8 -1
  162. package/dist/esm/query-executor/query-executor-provider.d.ts +8 -0
  163. package/dist/esm/query-executor/query-executor-provider.js +2 -0
  164. package/dist/esm/query-executor/query-executor.d.ts +14 -6
  165. package/dist/esm/query-executor/query-executor.js +4 -2
  166. package/dist/esm/raw-builder/raw-builder.d.ts +42 -11
  167. package/dist/esm/raw-builder/raw-builder.js +56 -57
  168. package/dist/esm/raw-builder/sql.d.ts +323 -0
  169. package/dist/esm/raw-builder/sql.js +151 -0
  170. package/dist/esm/schema/alter-table-builder.d.ts +20 -8
  171. package/dist/esm/schema/alter-table-builder.js +3 -3
  172. package/dist/esm/schema/column-definition-builder.d.ts +20 -8
  173. package/dist/esm/schema/create-index-builder.d.ts +3 -1
  174. package/dist/esm/schema/create-index-builder.js +3 -1
  175. package/dist/esm/schema/create-table-builder.d.ts +6 -2
  176. package/dist/esm/schema/create-table-builder.js +8 -4
  177. package/dist/esm/schema/create-view-builder.d.ts +3 -3
  178. package/dist/esm/schema/create-view-builder.js +3 -3
  179. package/dist/esm/util/query-id.js +10 -4
  180. package/dist/esm/util/random-string.js +65 -2
  181. package/package.json +13 -13
  182. package/dist/cjs/parser/parse-context.d.ts +0 -55
  183. package/dist/cjs/parser/parse-context.js +0 -50
  184. package/dist/cjs/util/private-symbols.d.ts +0 -5
  185. package/dist/cjs/util/private-symbols.js +0 -8
  186. package/dist/esm/parser/parse-context.d.ts +0 -55
  187. package/dist/esm/parser/parse-context.js +0 -47
  188. package/dist/esm/util/private-symbols.d.ts +0 -5
  189. package/dist/esm/util/private-symbols.js +0 -6
@@ -10,7 +10,6 @@ const insert_query_node_js_1 = require("./operation-node/insert-query-node.js");
10
10
  const select_query_node_js_1 = require("./operation-node/select-query-node.js");
11
11
  const update_query_node_js_1 = require("./operation-node/update-query-node.js");
12
12
  const table_parser_js_1 = require("./parser/table-parser.js");
13
- const raw_builder_js_1 = require("./raw-builder/raw-builder.js");
14
13
  const with_parser_js_1 = require("./parser/with-parser.js");
15
14
  const with_node_js_1 = require("./operation-node/with-node.js");
16
15
  const query_id_js_1 = require("./util/query-id.js");
@@ -25,8 +24,7 @@ class QueryCreator {
25
24
  return new select_query_builder_js_1.SelectQueryBuilder({
26
25
  queryId: (0, query_id_js_1.createQueryId)(),
27
26
  executor: this.#props.executor,
28
- parseContext: this.#props.parseContext,
29
- queryNode: select_query_node_js_1.SelectQueryNode.create((0, table_parser_js_1.parseTableExpressionOrList)(this.#props.parseContext, from), this.#props.withNode),
27
+ queryNode: select_query_node_js_1.SelectQueryNode.create((0, table_parser_js_1.parseTableExpressionOrList)(from), this.#props.withNode),
30
28
  });
31
29
  }
32
30
  /**
@@ -71,7 +69,6 @@ class QueryCreator {
71
69
  return new insert_query_builder_js_1.InsertQueryBuilder({
72
70
  queryId: (0, query_id_js_1.createQueryId)(),
73
71
  executor: this.#props.executor,
74
- parseContext: this.#props.parseContext,
75
72
  queryNode: insert_query_node_js_1.InsertQueryNode.create((0, table_parser_js_1.parseTable)(table), this.#props.withNode),
76
73
  });
77
74
  }
@@ -98,8 +95,7 @@ class QueryCreator {
98
95
  return new delete_query_builder_js_1.DeleteQueryBuilder({
99
96
  queryId: (0, query_id_js_1.createQueryId)(),
100
97
  executor: this.#props.executor,
101
- parseContext: this.#props.parseContext,
102
- queryNode: delete_query_node_js_1.DeleteQueryNode.create((0, table_parser_js_1.parseTableExpression)(this.#props.parseContext, table), this.#props.withNode),
98
+ queryNode: delete_query_node_js_1.DeleteQueryNode.create((0, table_parser_js_1.parseTableExpression)(table), this.#props.withNode),
103
99
  });
104
100
  }
105
101
  /**
@@ -129,8 +125,7 @@ class QueryCreator {
129
125
  return new update_query_builder_js_1.UpdateQueryBuilder({
130
126
  queryId: (0, query_id_js_1.createQueryId)(),
131
127
  executor: this.#props.executor,
132
- parseContext: this.#props.parseContext,
133
- queryNode: update_query_node_js_1.UpdateQueryNode.create((0, table_parser_js_1.parseTableExpression)(this.#props.parseContext, table), this.#props.withNode),
128
+ queryNode: update_query_node_js_1.UpdateQueryNode.create((0, table_parser_js_1.parseTableExpression)(table), this.#props.withNode),
134
129
  });
135
130
  }
136
131
  /**
@@ -175,7 +170,7 @@ class QueryCreator {
175
170
  * ```
176
171
  */
177
172
  with(name, expression) {
178
- const cte = (0, with_parser_js_1.parseCommonTableExpression)(this.#props.parseContext, name, expression);
173
+ const cte = (0, with_parser_js_1.parseCommonTableExpression)(name, expression);
179
174
  return new QueryCreator({
180
175
  ...this.#props,
181
176
  withNode: this.#props.withNode
@@ -189,7 +184,7 @@ class QueryCreator {
189
184
  * See the {@link with} method for examples and more documentation.
190
185
  */
191
186
  withRecursive(name, expression) {
192
- const cte = (0, with_parser_js_1.parseCommonTableExpression)(this.#props.parseContext, name, expression);
187
+ const cte = (0, with_parser_js_1.parseCommonTableExpression)(name, expression);
193
188
  return new QueryCreator({
194
189
  ...this.#props,
195
190
  withNode: this.#props.withNode
@@ -204,14 +199,18 @@ class QueryCreator {
204
199
  * This only affects the query created through the builder returned from
205
200
  * this method and doesn't modify the `db` instance.
206
201
  *
202
+ * See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/schemas.md)
203
+ * for a more detailed explanation.
204
+ *
207
205
  * ### Examples
208
206
  *
209
207
  * ```
210
- * await db.withSchema('mammals')
211
- * .selectFrom('pet')
212
- * .selectAll()
213
- * .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
214
- * .execute()
208
+ * await db
209
+ * .withSchema('mammals')
210
+ * .selectFrom('pet')
211
+ * .selectAll()
212
+ * .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
213
+ * .execute()
215
214
  * ```
216
215
  *
217
216
  * The generated SQL (PostgreSQL):
@@ -227,10 +226,11 @@ class QueryCreator {
227
226
  * doesn't belong to:
228
227
  *
229
228
  * ```
230
- * await db.withSchema('mammals')
231
- * .selectFrom('pet as p')
232
- * .select('p.name')
233
- * .execute()
229
+ * await db
230
+ * .withSchema('mammals')
231
+ * .selectFrom('pet as p')
232
+ * .select('p.name')
233
+ * .execute()
234
234
  * ```
235
235
  *
236
236
  * The generated SQL (PostgreSQL):
@@ -245,131 +245,5 @@ class QueryCreator {
245
245
  executor: this.#props.executor.withPluginAtFront(new with_schema_plugin_js_1.WithSchemaPlugin(schema)),
246
246
  });
247
247
  }
248
- /**
249
- * Provides a way to pass arbitrary SQL into your query and executing completely
250
- * raw queries.
251
- *
252
- * You can use the strings `?` and `??` in the `sql` to bind parameters such as
253
- * user input to the SQL. You should never EVER concatenate untrusted user
254
- * input to the SQL string to avoid injection vulnerabilities. Instead use `?`
255
- * in place of a value and pass the actual value in the `parameters` list. See
256
- * the examples below.
257
- *
258
- * You should only use `raw` when there is no other way to get the job done. This is
259
- * because Kysely is not able to use type inference when you use raw SQL. For example
260
- * Kysely won't be able to automatically provide you with the correct query result
261
- * type. However, there are ways to manually provide types when you use `raw` in most
262
- * cases. See the examples below.
263
- *
264
- * Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
265
- * `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
266
- *
267
- * ### Examples
268
- *
269
- * Example of using `raw` in a select statement:
270
- *
271
- * ```ts
272
- * const [person] = await db.selectFrom('person')
273
- * .select(db.raw<string>('concat(first_name, ' ', last_name)').as('name'))
274
- * .where('id', '=', 1)
275
- * .execute()
276
- *
277
- * console.log(person.name)
278
- * ```
279
- *
280
- * The generated SQL (PostgreSQL):
281
- *
282
- * ```sql
283
- * select concat(first_name, ' ', last_name) as "name"
284
- * from "person" where "id" = 1
285
- * ```
286
- *
287
- * The above example selects computed column `name` by concatenating the first name
288
- * and last name together.
289
- *
290
- * There are couple of things worth noticing:
291
- *
292
- * 1. You need to provide the output type of your SQL segment for the `raw` method
293
- * so that Kysely knows what type to give for the `name` column. In this case it's
294
- * a `string` since that's the output type of the `concat` function in SQL.
295
- *
296
- * 2. You need to give an alias for the selection using the `as` method so that
297
- * Kysely is able to add a column to the output type. The alias needs to be
298
- * known at compile time! If you pass a string variable whose value is not known
299
- * at compile time, there is no way for Kysely or typescript to add a column to
300
- * the output type. In this case you need to use the `castTo` method on the query
301
- * to specify a return type for the query.
302
- *
303
- * We could've also used a `??` placeholder to provide `first_name` and `last_name` like
304
- * this:
305
- *
306
- * ```ts
307
- * db.raw<string>('concat(??, ' ', ??)', ['first_name', 'last_name'])
308
- * ```
309
- *
310
- * or this:
311
- *
312
- * ```ts
313
- * db.raw<string>('concat(??, ' ', ??)', ['person.first_name', 'person.last_name'])
314
- * ```
315
- *
316
- * But it's often cleaner to just write the column names in the SQL. Again remember to
317
- * never concatenate column names or any other untrusted user input to the SQL string or you
318
- * are going to create an injection vulnerability. All user input should go to the parameters
319
- * array, never to the SQL string directly. But if the column names or values are trusted
320
- * and known at compile time, there is no reason to use parameters.
321
- *
322
- * Example of using `raw` in `where`:
323
- *
324
- * ```ts
325
- * function getPersonsOlderThan(ageLimit: number) {
326
- * return await db.selectFrom('person')
327
- * .selectAll()
328
- * .where(
329
- * db.raw('now() - birth_date'),
330
- * '>',
331
- * db.raw('interval ? year', [ageLimit.toString()])
332
- * )
333
- * .execute()
334
- * }
335
- * ```
336
- *
337
- * The generated SQL (PostgreSQL):
338
- *
339
- * ```sql
340
- * select * from "person" where now() - birth_date > interval $1 year
341
- * ```
342
- *
343
- * The function in the above example returns people that are older than the given number of
344
- * years. The number of years in this example is an untrusted user input, and therefore we use
345
- * a `?` placeholder for it.
346
- *
347
- * Example of creating a completely raw query from scratch:
348
- *
349
- * ```ts
350
- * const result = await db.raw<Person>('select p.* from person p').execute()
351
- * const persons = result.rows
352
- * ```
353
- *
354
- * For a raw query, you need to specify the type of the returned __row__. In
355
- * this case we know the resulting items will be of type `Person` se specify that.
356
- * The result of `execute()` method is always an array. In this case the type of
357
- * the `persons` variable is `Person[]`.
358
- *
359
- * @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
360
- * placeholders. `?` for values and `??` for identifiers such as column names
361
- * or `column.table` references.
362
- *
363
- * @param params - The parameters that will be bound to the `?` and `??` placeholders in
364
- * the sql string.
365
- */
366
- raw(sql, parameters) {
367
- return new raw_builder_js_1.RawBuilder({
368
- queryId: (0, query_id_js_1.createQueryId)(),
369
- executor: this.#props.executor,
370
- sql,
371
- parameters,
372
- });
373
- }
374
248
  }
375
249
  exports.QueryCreator = QueryCreator;
@@ -1,19 +1,30 @@
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
6
  import { QueryExecutor } from './query-executor.js';
7
+ import { DialectAdapter } from '../index-nodeless.js';
7
8
  export declare class DefaultQueryExecutor extends QueryExecutor {
8
9
  #private;
9
- constructor(compiler: QueryCompiler, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
10
+ constructor(compiler: QueryCompiler, adapter: DialectAdapter, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
11
+ get adapter(): DialectAdapter;
10
12
  /**
11
13
  * Compiles the transformed query into SQL. You usually want to pass
12
14
  * the output of {@link transformQuery} into this method but you can
13
15
  * compile any query using this method.
14
16
  */
15
17
  compileQuery(node: RootOperationNode): CompiledQuery;
16
- protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
18
+ /**
19
+ * Provides a connection for the callback and takes care of disposing
20
+ * the connection after the callback has been run.
21
+ */
22
+ provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
23
+ /**
24
+ * Returns a copy of this executor with a list of plugins added
25
+ * as the last plugins.
26
+ */
27
+ withPlugins(plugins: ReadonlyArray<KyselyPlugin>): DefaultQueryExecutor;
17
28
  /**
18
29
  * Returns a copy of this executor with a plugin added as the
19
30
  * last plugin.
@@ -4,39 +4,37 @@ exports.DefaultQueryExecutor = void 0;
4
4
  const query_executor_js_1 = require("./query-executor.js");
5
5
  class DefaultQueryExecutor extends query_executor_js_1.QueryExecutor {
6
6
  #compiler;
7
+ #adapter;
7
8
  #connectionProvider;
8
- constructor(compiler, connectionProvider, plugins = []) {
9
+ constructor(compiler, adapter, connectionProvider, plugins = []) {
9
10
  super(plugins);
10
11
  this.#compiler = compiler;
12
+ this.#adapter = adapter;
11
13
  this.#connectionProvider = connectionProvider;
12
14
  }
15
+ get adapter() {
16
+ return this.#adapter;
17
+ }
13
18
  compileQuery(node) {
14
19
  return this.#compiler.compileQuery(node);
15
20
  }
16
- async executeQueryImpl(compiledQuery) {
17
- return this.#connectionProvider.withConnection((connection) => {
18
- return connection.executeQuery(compiledQuery);
19
- });
21
+ provideConnection(consumer) {
22
+ return this.#connectionProvider.provideConnection(consumer);
23
+ }
24
+ withPlugins(plugins) {
25
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, ...plugins]);
20
26
  }
21
27
  withPlugin(plugin) {
22
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
23
- ...this.plugins,
24
- plugin,
25
- ]);
28
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, plugin]);
26
29
  }
27
30
  withPluginAtFront(plugin) {
28
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
29
- plugin,
30
- ...this.plugins,
31
- ]);
31
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [plugin, ...this.plugins]);
32
32
  }
33
33
  withConnectionProvider(connectionProvider) {
34
- return new DefaultQueryExecutor(this.#compiler, connectionProvider, [
35
- ...this.plugins,
36
- ]);
34
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, connectionProvider, [...this.plugins]);
37
35
  }
38
36
  withoutPlugins() {
39
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, []);
37
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, []);
40
38
  }
41
39
  }
42
40
  exports.DefaultQueryExecutor = DefaultQueryExecutor;
@@ -1,20 +1,25 @@
1
- import { QueryResult } from '../driver/database-connection.js';
2
1
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
2
  import { QueryExecutor } from './query-executor.js';
4
3
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
4
+ import { DialectAdapter } from '../dialect/dialect-adapter.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
10
  export declare class NoopQueryExecutor extends QueryExecutor {
11
+ get adapter(): DialectAdapter;
11
12
  /**
12
13
  * Compiles the transformed query into SQL. You usually want to pass
13
14
  * the output of {@link transformQuery} into this method but you can
14
15
  * compile any query using this method.
15
16
  */
16
17
  compileQuery(): CompiledQuery;
17
- protected executeQueryImpl<R>(): Promise<QueryResult<R>>;
18
+ /**
19
+ * Provides a connection for the callback and takes care of disposing
20
+ * the connection after the callback has been run.
21
+ */
22
+ provideConnection<T>(): Promise<T>;
18
23
  /**
19
24
  * Returns a copy of this executor with a new connection provider.
20
25
  */
@@ -24,6 +29,11 @@ export declare class NoopQueryExecutor extends QueryExecutor {
24
29
  * last plugin.
25
30
  */
26
31
  withPlugin(plugin: KyselyPlugin): NoopQueryExecutor;
32
+ /**
33
+ * Returns a copy of this executor with a list of plugins added
34
+ * as the last plugins.
35
+ */
36
+ withPlugins(plugins: ReadonlyArray<KyselyPlugin>): NoopQueryExecutor;
27
37
  /**
28
38
  * Returns a copy of this executor with a plugin added as the
29
39
  * first plugin.
@@ -34,3 +44,4 @@ export declare class NoopQueryExecutor extends QueryExecutor {
34
44
  */
35
45
  withoutPlugins(): QueryExecutor;
36
46
  }
47
+ export declare const NOOP_QUERY_EXECUTOR: NoopQueryExecutor;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NoopQueryExecutor = void 0;
3
+ exports.NOOP_QUERY_EXECUTOR = exports.NoopQueryExecutor = void 0;
4
4
  const query_executor_js_1 = require("./query-executor.js");
5
5
  /**
6
6
  * A {@link QueryExecutor} subclass that can be used when you don't
@@ -8,10 +8,13 @@ const query_executor_js_1 = require("./query-executor.js");
8
8
  * other needed things to actually execute queries.
9
9
  */
10
10
  class NoopQueryExecutor extends query_executor_js_1.QueryExecutor {
11
+ get adapter() {
12
+ throw new Error('this query cannot be compiled to SQL');
13
+ }
11
14
  compileQuery() {
12
15
  throw new Error('this query cannot be compiled to SQL');
13
16
  }
14
- async executeQueryImpl() {
17
+ provideConnection() {
15
18
  throw new Error('this query cannot be executed');
16
19
  }
17
20
  withConnectionProvider() {
@@ -20,6 +23,9 @@ class NoopQueryExecutor extends query_executor_js_1.QueryExecutor {
20
23
  withPlugin(plugin) {
21
24
  return new NoopQueryExecutor([...this.plugins, plugin]);
22
25
  }
26
+ withPlugins(plugins) {
27
+ return new NoopQueryExecutor([...this.plugins, ...plugins]);
28
+ }
23
29
  withPluginAtFront(plugin) {
24
30
  return new NoopQueryExecutor([plugin, ...this.plugins]);
25
31
  }
@@ -28,3 +34,4 @@ class NoopQueryExecutor extends query_executor_js_1.QueryExecutor {
28
34
  }
29
35
  }
30
36
  exports.NoopQueryExecutor = NoopQueryExecutor;
37
+ exports.NOOP_QUERY_EXECUTOR = new NoopQueryExecutor();
@@ -0,0 +1,8 @@
1
+ import { QueryExecutor } from './query-executor.js';
2
+ /**
3
+ * @internal
4
+ * @private
5
+ */
6
+ export interface QueryExecutorProvider {
7
+ getExecutor(): QueryExecutor;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,18 +1,20 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { QueryResult } from '../driver/database-connection.js';
2
+ import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
6
  import { QueryId } from '../util/query-id.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
7
8
  /**
8
9
  * This class abstracts away the details of how to compile a query into SQL
9
10
  * and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
10
11
  * and other classes that execute queries can just pass around and instance of
11
12
  * `QueryExecutor`.
12
13
  */
13
- export declare abstract class QueryExecutor {
14
+ export declare abstract class QueryExecutor implements ConnectionProvider {
14
15
  #private;
15
16
  constructor(plugins?: KyselyPlugin[]);
17
+ abstract get adapter(): DialectAdapter;
16
18
  get plugins(): ReadonlyArray<KyselyPlugin>;
17
19
  /**
18
20
  * Given the query the user has built (expressed as an operation node tree)
@@ -26,15 +28,16 @@ export declare abstract class QueryExecutor {
26
28
  * compile any query using this method.
27
29
  */
28
30
  abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
31
+ /**
32
+ * Provides a connection for the callback and takes care of disposing
33
+ * the connection after the callback has been run.
34
+ */
35
+ abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
29
36
  /**
30
37
  * Executes a compiled query and runs the result through all plugins'
31
38
  * `transformResult` method.
32
39
  */
33
40
  executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
34
- /**
35
- * Executes a compiled query.
36
- */
37
- protected abstract executeQueryImpl<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
38
41
  /**
39
42
  * Returns a copy of this executor with a new connection provider.
40
43
  */
@@ -44,6 +47,11 @@ export declare abstract class QueryExecutor {
44
47
  * last plugin.
45
48
  */
46
49
  abstract withPlugin(plugin: KyselyPlugin): QueryExecutor;
50
+ /**
51
+ * Returns a copy of this executor with a list of plugins added
52
+ * as the last plugins.
53
+ */
54
+ abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
47
55
  /**
48
56
  * Returns a copy of this executor with a plugin added as the
49
57
  * first plugin.
@@ -46,8 +46,10 @@ class QueryExecutor {
46
46
  * `transformResult` method.
47
47
  */
48
48
  async executeQuery(compiledQuery, queryId) {
49
- const result = await this.executeQueryImpl(compiledQuery, queryId);
50
- return this.#transformResult(result, queryId);
49
+ return await this.provideConnection(async (connection) => {
50
+ const result = await connection.executeQuery(compiledQuery);
51
+ return this.#transformResult(result, queryId);
52
+ });
51
53
  }
52
54
  async #transformResult(result, queryId) {
53
55
  for (const plugin of this.#plugins) {
@@ -2,25 +2,57 @@ import { QueryResult } from '../driver/database-connection.js';
2
2
  import { AliasNode } from '../operation-node/alias-node.js';
3
3
  import { OperationNodeSource } from '../operation-node/operation-node-source.js';
4
4
  import { RawNode } from '../operation-node/raw-node.js';
5
- import { CompiledQuery } from '../query-compiler/compiled-query.js';
6
- import { QueryExecutor } from '../query-executor/query-executor.js';
7
- import { QueryId } from '../util/query-id.js';
8
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
+ import { QueryExecutorProvider } from '../query-executor/query-executor-provider.js';
7
+ import { QueryId } from '../util/query-id.js';
8
+ /**
9
+ * An instance of this class can be used to create raw SQL snippets or queries.
10
+ *
11
+ * You shouldn't need to create `RawBuilder` instances directly. Instead you should
12
+ * use the {@link sql} template tag.
13
+ */
9
14
  export declare class RawBuilder<O = unknown> implements OperationNodeSource {
10
15
  #private;
11
16
  constructor(props: RawBuilderProps);
17
+ /**
18
+ * Returns an aliased version of the SQL expression.
19
+ *
20
+ * In addition to slapping `as "the_alias"` to the end of the SQL,
21
+ * this method also provides strict typing:
22
+ *
23
+ * ```ts
24
+ * const result = await db
25
+ * .selectFrom('person')
26
+ * .select(
27
+ * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
28
+ * )
29
+ * .executeTakeFirstOrThrow()
30
+ *
31
+ * // `full_name: string` field exists in the result type.
32
+ * console.log(result.full_name)
33
+ * ```
34
+ *
35
+ * The generated SQL (PostgreSQL):
36
+ *
37
+ * ```ts
38
+ * select concat(first_name, ' ', last_name) as "full_name"
39
+ * from "person"
40
+ * ```
41
+ */
12
42
  as<A extends string>(alias: A): AliasedRawBuilder<O, A>;
13
43
  /**
14
44
  * Change the output type of the raw expression.
15
45
  *
16
- * This doesn't produce any SQL. This methods simply returns a copy
17
- * of this `RawBuilder` with a new output type.
46
+ * This method call doesn't change the SQL in any way. This methods simply
47
+ * returns a copy of this `RawBuilder` with a new output type.
18
48
  */
19
49
  castTo<T>(): RawBuilder<T>;
50
+ /**
51
+ * Adds a plugin for this SQL snippet.
52
+ */
20
53
  withPlugin(plugin: KyselyPlugin): RawBuilder<O>;
21
54
  toOperationNode(): RawNode;
22
- compile(): CompiledQuery;
23
- execute(): Promise<QueryResult<O>>;
55
+ execute(executorProvider: QueryExecutorProvider): Promise<QueryResult<O>>;
24
56
  }
25
57
  /**
26
58
  * {@link RawBuilder} with an alias. The result of calling {@link RawBuilder.as}.
@@ -40,8 +72,7 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
40
72
  constructor(rawBuilder: RawBuilder<O>, alias: A);
41
73
  }
42
74
  export interface RawBuilderProps {
43
- readonly queryId?: QueryId;
44
- readonly executor?: QueryExecutor;
45
- readonly sql: string;
46
- readonly parameters?: ReadonlyArray<unknown>;
75
+ readonly queryId: QueryId;
76
+ readonly rawNode: RawNode;
77
+ readonly plugins?: ReadonlyArray<KyselyPlugin>;
47
78
  }