kysely 0.16.8 → 0.17.0

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 (179) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +23 -11
  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/index-nodeless.d.ts +2 -0
  8. package/dist/cjs/index-nodeless.js +2 -0
  9. package/dist/cjs/kysely.d.ts +3 -6
  10. package/dist/cjs/kysely.js +9 -14
  11. package/dist/cjs/migration/migrator.d.ts +22 -0
  12. package/dist/cjs/migration/migrator.js +27 -5
  13. package/dist/cjs/operation-node/delete-query-node.d.ts +7 -0
  14. package/dist/cjs/operation-node/delete-query-node.js +15 -0
  15. package/dist/cjs/operation-node/operation-node-transformer.js +1 -1
  16. package/dist/cjs/operation-node/raw-node.d.ts +4 -2
  17. package/dist/cjs/operation-node/raw-node.js +8 -2
  18. package/dist/cjs/parser/complex-expression-parser.d.ts +5 -5
  19. package/dist/cjs/parser/complex-expression-parser.js +5 -4
  20. package/dist/cjs/parser/filter-parser.d.ts +9 -9
  21. package/dist/cjs/parser/filter-parser.js +31 -30
  22. package/dist/cjs/parser/group-by-parser.d.ts +1 -2
  23. package/dist/cjs/parser/group-by-parser.js +2 -2
  24. package/dist/cjs/parser/insert-values-parser.d.ts +1 -2
  25. package/dist/cjs/parser/insert-values-parser.js +6 -6
  26. package/dist/cjs/parser/join-parser.d.ts +1 -2
  27. package/dist/cjs/parser/join-parser.js +8 -7
  28. package/dist/cjs/parser/order-by-parser.d.ts +1 -2
  29. package/dist/cjs/parser/order-by-parser.js +4 -4
  30. package/dist/cjs/parser/parse-utils.d.ts +10 -0
  31. package/dist/cjs/parser/parse-utils.js +38 -0
  32. package/dist/cjs/parser/reference-parser.d.ts +2 -3
  33. package/dist/cjs/parser/reference-parser.js +5 -5
  34. package/dist/cjs/parser/select-parser.d.ts +1 -2
  35. package/dist/cjs/parser/select-parser.js +5 -5
  36. package/dist/cjs/parser/table-parser.d.ts +2 -3
  37. package/dist/cjs/parser/table-parser.js +5 -5
  38. package/dist/cjs/parser/update-set-parser.d.ts +1 -2
  39. package/dist/cjs/parser/update-set-parser.js +2 -2
  40. package/dist/cjs/parser/value-parser.d.ts +3 -4
  41. package/dist/cjs/parser/value-parser.js +7 -7
  42. package/dist/cjs/parser/with-parser.d.ts +1 -2
  43. package/dist/cjs/parser/with-parser.js +3 -2
  44. package/dist/cjs/query-builder/delete-query-builder.d.ts +77 -20
  45. package/dist/cjs/query-builder/delete-query-builder.js +81 -14
  46. package/dist/cjs/query-builder/expression-builder.d.ts +4 -13
  47. package/dist/cjs/query-builder/expression-builder.js +12 -21
  48. package/dist/cjs/query-builder/function-builder.d.ts +2 -6
  49. package/dist/cjs/query-builder/function-builder.js +5 -8
  50. package/dist/cjs/query-builder/insert-query-builder.d.ts +11 -9
  51. package/dist/cjs/query-builder/insert-query-builder.js +8 -7
  52. package/dist/cjs/query-builder/join-builder.d.ts +0 -2
  53. package/dist/cjs/query-builder/join-builder.js +8 -8
  54. package/dist/cjs/query-builder/on-conflict-builder.d.ts +24 -20
  55. package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
  56. package/dist/cjs/query-builder/returning-interface.d.ts +2 -2
  57. package/dist/cjs/query-builder/select-query-builder.d.ts +43 -32
  58. package/dist/cjs/query-builder/select-query-builder.js +28 -26
  59. package/dist/cjs/query-builder/update-query-builder.d.ts +29 -25
  60. package/dist/cjs/query-builder/update-query-builder.js +22 -20
  61. package/dist/cjs/query-builder/where-interface.d.ts +20 -16
  62. package/dist/cjs/query-compiler/default-query-compiler.js +16 -7
  63. package/dist/cjs/query-creator.d.ts +24 -138
  64. package/dist/cjs/query-creator.js +19 -145
  65. package/dist/cjs/query-executor/default-query-executor.d.ts +8 -1
  66. package/dist/cjs/query-executor/default-query-executor.js +13 -13
  67. package/dist/cjs/query-executor/noop-query-executor.d.ts +8 -0
  68. package/dist/cjs/query-executor/noop-query-executor.js +8 -1
  69. package/dist/cjs/query-executor/query-executor-provider.d.ts +8 -0
  70. package/dist/cjs/query-executor/query-executor-provider.js +2 -0
  71. package/dist/cjs/query-executor/query-executor.d.ts +7 -0
  72. package/dist/cjs/raw-builder/raw-builder.d.ts +42 -11
  73. package/dist/cjs/raw-builder/raw-builder.js +55 -56
  74. package/dist/cjs/raw-builder/sql.d.ts +323 -0
  75. package/dist/cjs/raw-builder/sql.js +153 -0
  76. package/dist/cjs/schema/alter-table-builder.d.ts +20 -8
  77. package/dist/cjs/schema/alter-table-builder.js +3 -3
  78. package/dist/cjs/schema/column-definition-builder.d.ts +20 -8
  79. package/dist/cjs/schema/create-index-builder.d.ts +3 -1
  80. package/dist/cjs/schema/create-index-builder.js +3 -1
  81. package/dist/cjs/schema/create-table-builder.d.ts +6 -2
  82. package/dist/cjs/schema/create-table-builder.js +8 -4
  83. package/dist/cjs/schema/create-view-builder.d.ts +3 -3
  84. package/dist/cjs/schema/create-view-builder.js +3 -3
  85. package/dist/cjs/util/query-id.js +10 -4
  86. package/dist/cjs/util/random-string.js +65 -2
  87. package/dist/esm/dialect/mysql/mysql-adapter.js +3 -4
  88. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -1
  89. package/dist/esm/dialect/postgres/postgres-adapter.js +3 -2
  90. package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -1
  91. package/dist/esm/index-nodeless.d.ts +2 -0
  92. package/dist/esm/index-nodeless.js +2 -0
  93. package/dist/esm/kysely.d.ts +3 -6
  94. package/dist/esm/kysely.js +9 -14
  95. package/dist/esm/migration/migrator.d.ts +22 -0
  96. package/dist/esm/migration/migrator.js +27 -5
  97. package/dist/esm/operation-node/delete-query-node.d.ts +7 -0
  98. package/dist/esm/operation-node/delete-query-node.js +15 -0
  99. package/dist/esm/operation-node/operation-node-transformer.js +1 -1
  100. package/dist/esm/operation-node/raw-node.d.ts +4 -2
  101. package/dist/esm/operation-node/raw-node.js +8 -2
  102. package/dist/esm/parser/complex-expression-parser.d.ts +5 -5
  103. package/dist/esm/parser/complex-expression-parser.js +5 -4
  104. package/dist/esm/parser/filter-parser.d.ts +9 -9
  105. package/dist/esm/parser/filter-parser.js +31 -30
  106. package/dist/esm/parser/group-by-parser.d.ts +1 -2
  107. package/dist/esm/parser/group-by-parser.js +2 -2
  108. package/dist/esm/parser/insert-values-parser.d.ts +1 -2
  109. package/dist/esm/parser/insert-values-parser.js +6 -6
  110. package/dist/esm/parser/join-parser.d.ts +1 -2
  111. package/dist/esm/parser/join-parser.js +8 -7
  112. package/dist/esm/parser/order-by-parser.d.ts +1 -2
  113. package/dist/esm/parser/order-by-parser.js +4 -4
  114. package/dist/esm/parser/parse-utils.d.ts +10 -0
  115. package/dist/esm/parser/parse-utils.js +32 -0
  116. package/dist/esm/parser/reference-parser.d.ts +2 -3
  117. package/dist/esm/parser/reference-parser.js +5 -5
  118. package/dist/esm/parser/select-parser.d.ts +1 -2
  119. package/dist/esm/parser/select-parser.js +5 -5
  120. package/dist/esm/parser/table-parser.d.ts +2 -3
  121. package/dist/esm/parser/table-parser.js +5 -5
  122. package/dist/esm/parser/update-set-parser.d.ts +1 -2
  123. package/dist/esm/parser/update-set-parser.js +2 -2
  124. package/dist/esm/parser/value-parser.d.ts +3 -4
  125. package/dist/esm/parser/value-parser.js +7 -7
  126. package/dist/esm/parser/with-parser.d.ts +1 -2
  127. package/dist/esm/parser/with-parser.js +3 -2
  128. package/dist/esm/query-builder/delete-query-builder.d.ts +77 -20
  129. package/dist/esm/query-builder/delete-query-builder.js +81 -14
  130. package/dist/esm/query-builder/expression-builder.d.ts +4 -13
  131. package/dist/esm/query-builder/expression-builder.js +12 -21
  132. package/dist/esm/query-builder/function-builder.d.ts +2 -6
  133. package/dist/esm/query-builder/function-builder.js +5 -8
  134. package/dist/esm/query-builder/insert-query-builder.d.ts +11 -9
  135. package/dist/esm/query-builder/insert-query-builder.js +8 -7
  136. package/dist/esm/query-builder/join-builder.d.ts +0 -2
  137. package/dist/esm/query-builder/join-builder.js +8 -8
  138. package/dist/esm/query-builder/on-conflict-builder.d.ts +24 -20
  139. package/dist/esm/query-builder/on-conflict-builder.js +17 -17
  140. package/dist/esm/query-builder/returning-interface.d.ts +2 -2
  141. package/dist/esm/query-builder/select-query-builder.d.ts +43 -32
  142. package/dist/esm/query-builder/select-query-builder.js +28 -26
  143. package/dist/esm/query-builder/update-query-builder.d.ts +29 -25
  144. package/dist/esm/query-builder/update-query-builder.js +22 -20
  145. package/dist/esm/query-builder/where-interface.d.ts +20 -16
  146. package/dist/esm/query-compiler/default-query-compiler.js +16 -7
  147. package/dist/esm/query-creator.d.ts +24 -138
  148. package/dist/esm/query-creator.js +19 -145
  149. package/dist/esm/query-executor/default-query-executor.d.ts +8 -1
  150. package/dist/esm/query-executor/default-query-executor.js +13 -13
  151. package/dist/esm/query-executor/noop-query-executor.d.ts +8 -0
  152. package/dist/esm/query-executor/noop-query-executor.js +7 -0
  153. package/dist/esm/query-executor/query-executor-provider.d.ts +8 -0
  154. package/dist/esm/query-executor/query-executor-provider.js +2 -0
  155. package/dist/esm/query-executor/query-executor.d.ts +7 -0
  156. package/dist/esm/raw-builder/raw-builder.d.ts +42 -11
  157. package/dist/esm/raw-builder/raw-builder.js +56 -57
  158. package/dist/esm/raw-builder/sql.d.ts +323 -0
  159. package/dist/esm/raw-builder/sql.js +151 -0
  160. package/dist/esm/schema/alter-table-builder.d.ts +20 -8
  161. package/dist/esm/schema/alter-table-builder.js +3 -3
  162. package/dist/esm/schema/column-definition-builder.d.ts +20 -8
  163. package/dist/esm/schema/create-index-builder.d.ts +3 -1
  164. package/dist/esm/schema/create-index-builder.js +3 -1
  165. package/dist/esm/schema/create-table-builder.d.ts +6 -2
  166. package/dist/esm/schema/create-table-builder.js +8 -4
  167. package/dist/esm/schema/create-view-builder.d.ts +3 -3
  168. package/dist/esm/schema/create-view-builder.js +3 -3
  169. package/dist/esm/util/query-id.js +10 -4
  170. package/dist/esm/util/random-string.js +65 -2
  171. package/package.json +13 -13
  172. package/dist/cjs/parser/parse-context.d.ts +0 -55
  173. package/dist/cjs/parser/parse-context.js +0 -50
  174. package/dist/cjs/util/private-symbols.d.ts +0 -5
  175. package/dist/cjs/util/private-symbols.js +0 -8
  176. package/dist/esm/parser/parse-context.d.ts +0 -55
  177. package/dist/esm/parser/parse-context.js +0 -47
  178. package/dist/esm/util/private-symbols.d.ts +0 -5
  179. 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;
@@ -4,9 +4,11 @@ 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
@@ -14,6 +16,11 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
14
16
  */
15
17
  compileQuery(node: RootOperationNode): CompiledQuery;
16
18
  protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
19
+ /**
20
+ * Returns a copy of this executor with a list of plugins added
21
+ * as the last plugins.
22
+ */
23
+ withPlugins(plugins: ReadonlyArray<KyselyPlugin>): DefaultQueryExecutor;
17
24
  /**
18
25
  * Returns a copy of this executor with a plugin added as the
19
26
  * last plugin.
@@ -4,12 +4,17 @@ 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
  }
@@ -18,25 +23,20 @@ class DefaultQueryExecutor extends query_executor_js_1.QueryExecutor {
18
23
  return connection.executeQuery(compiledQuery);
19
24
  });
20
25
  }
26
+ withPlugins(plugins) {
27
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, ...plugins]);
28
+ }
21
29
  withPlugin(plugin) {
22
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
23
- ...this.plugins,
24
- plugin,
25
- ]);
30
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, plugin]);
26
31
  }
27
32
  withPluginAtFront(plugin) {
28
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
29
- plugin,
30
- ...this.plugins,
31
- ]);
33
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [plugin, ...this.plugins]);
32
34
  }
33
35
  withConnectionProvider(connectionProvider) {
34
- return new DefaultQueryExecutor(this.#compiler, connectionProvider, [
35
- ...this.plugins,
36
- ]);
36
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, connectionProvider, [...this.plugins]);
37
37
  }
38
38
  withoutPlugins() {
39
- return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, []);
39
+ return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, []);
40
40
  }
41
41
  }
42
42
  exports.DefaultQueryExecutor = DefaultQueryExecutor;
@@ -2,12 +2,14 @@ import { QueryResult } from '../driver/database-connection.js';
2
2
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
3
  import { QueryExecutor } from './query-executor.js';
4
4
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
5
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
5
6
  /**
6
7
  * A {@link QueryExecutor} subclass that can be used when you don't
7
8
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
8
9
  * other needed things to actually execute queries.
9
10
  */
10
11
  export declare class NoopQueryExecutor extends QueryExecutor {
12
+ get adapter(): DialectAdapter;
11
13
  /**
12
14
  * Compiles the transformed query into SQL. You usually want to pass
13
15
  * the output of {@link transformQuery} into this method but you can
@@ -24,6 +26,11 @@ export declare class NoopQueryExecutor extends QueryExecutor {
24
26
  * last plugin.
25
27
  */
26
28
  withPlugin(plugin: KyselyPlugin): NoopQueryExecutor;
29
+ /**
30
+ * Returns a copy of this executor with a list of plugins added
31
+ * as the last plugins.
32
+ */
33
+ withPlugins(plugins: ReadonlyArray<KyselyPlugin>): NoopQueryExecutor;
27
34
  /**
28
35
  * Returns a copy of this executor with a plugin added as the
29
36
  * first plugin.
@@ -34,3 +41,4 @@ export declare class NoopQueryExecutor extends QueryExecutor {
34
41
  */
35
42
  withoutPlugins(): QueryExecutor;
36
43
  }
44
+ 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,6 +8,9 @@ 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
  }
@@ -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 });
@@ -4,6 +4,7 @@ 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}
@@ -13,6 +14,7 @@ import { QueryId } from '../util/query-id.js';
13
14
  export declare abstract class QueryExecutor {
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)
@@ -44,6 +46,11 @@ export declare abstract class QueryExecutor {
44
46
  * last plugin.
45
47
  */
46
48
  abstract withPlugin(plugin: KyselyPlugin): QueryExecutor;
49
+ /**
50
+ * Returns a copy of this executor with a list of plugins added
51
+ * as the last plugins.
52
+ */
53
+ abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
47
54
  /**
48
55
  * Returns a copy of this executor with a plugin added as the
49
56
  * first plugin.
@@ -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
  }
@@ -2,75 +2,85 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AliasedRawBuilder = exports.RawBuilder = void 0;
4
4
  const alias_node_js_1 = require("../operation-node/alias-node.js");
5
- const operation_node_source_js_1 = require("../operation-node/operation-node-source.js");
6
- const raw_node_js_1 = require("../operation-node/raw-node.js");
7
- const value_node_js_1 = require("../operation-node/value-node.js");
8
- const reference_parser_js_1 = require("../parser/reference-parser.js");
9
5
  const prevent_await_js_1 = require("../util/prevent-await.js");
10
- const query_id_js_1 = require("../util/query-id.js");
11
6
  const object_utils_js_1 = require("../util/object-utils.js");
12
7
  const noop_query_executor_js_1 = require("../query-executor/noop-query-executor.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
+ */
13
14
  class RawBuilder {
14
15
  #props;
15
16
  constructor(props) {
16
- this.#props = (0, object_utils_js_1.freeze)({
17
- queryId: props.queryId ?? (0, query_id_js_1.createQueryId)(),
18
- executor: props.executor ?? new noop_query_executor_js_1.NoopQueryExecutor(),
19
- sql: props.sql,
20
- parameters: props.parameters,
21
- });
17
+ this.#props = (0, object_utils_js_1.freeze)(props);
22
18
  }
19
+ /**
20
+ * Returns an aliased version of the SQL expression.
21
+ *
22
+ * In addition to slapping `as "the_alias"` to the end of the SQL,
23
+ * this method also provides strict typing:
24
+ *
25
+ * ```ts
26
+ * const result = await db
27
+ * .selectFrom('person')
28
+ * .select(
29
+ * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
30
+ * )
31
+ * .executeTakeFirstOrThrow()
32
+ *
33
+ * // `full_name: string` field exists in the result type.
34
+ * console.log(result.full_name)
35
+ * ```
36
+ *
37
+ * The generated SQL (PostgreSQL):
38
+ *
39
+ * ```ts
40
+ * select concat(first_name, ' ', last_name) as "full_name"
41
+ * from "person"
42
+ * ```
43
+ */
23
44
  as(alias) {
24
45
  return new AliasedRawBuilder(this, alias);
25
46
  }
26
47
  /**
27
48
  * Change the output type of the raw expression.
28
49
  *
29
- * This doesn't produce any SQL. This methods simply returns a copy
30
- * of this `RawBuilder` with a new output type.
50
+ * This method call doesn't change the SQL in any way. This methods simply
51
+ * returns a copy of this `RawBuilder` with a new output type.
31
52
  */
32
53
  castTo() {
33
- return new RawBuilder({
34
- ...this.#props,
35
- });
54
+ return new RawBuilder({ ...this.#props });
36
55
  }
56
+ /**
57
+ * Adds a plugin for this SQL snippet.
58
+ */
37
59
  withPlugin(plugin) {
38
60
  return new RawBuilder({
39
61
  ...this.#props,
40
- executor: this.#props.executor.withPlugin(plugin),
62
+ plugins: this.#props.plugins !== undefined
63
+ ? (0, object_utils_js_1.freeze)([...this.#props.plugins, plugin])
64
+ : (0, object_utils_js_1.freeze)([plugin]),
41
65
  });
42
66
  }
43
67
  toOperationNode() {
44
- const parameterPlaceholderRegex = /(\?\??)/g;
45
- const sql = this.#props.sql;
46
- const parameters = this.#props.parameters ?? [];
47
- const sqlFragments = [];
48
- const argNodes = [];
49
- let paramIdx = 0;
50
- let sqlIdx = 0;
51
- let match = null;
52
- while ((match = parameterPlaceholderRegex.exec(sql))) {
53
- const str = match[1];
54
- if (paramIdx >= parameters.length) {
55
- throw new Error(`value not provided for all placeholders in string ${sql}`);
56
- }
57
- if (match.index > 0 && sql[match.index - 1] === '\\') {
58
- continue;
59
- }
60
- sqlFragments.push(sql.slice(sqlIdx, match.index).replace(/\\\?/g, '?'));
61
- argNodes.push(parseRawArg(str, parameters[paramIdx]));
62
- sqlIdx = match.index + str.length;
63
- ++paramIdx;
64
- }
65
- sqlFragments.push(sql.slice(sqlIdx));
66
- const rawNode = raw_node_js_1.RawNode.create(sqlFragments, argNodes);
67
- return this.#props.executor.transformQuery(rawNode, this.#props.queryId);
68
+ const executor = this.#props.plugins !== undefined
69
+ ? noop_query_executor_js_1.NOOP_QUERY_EXECUTOR.withPlugins(this.#props.plugins)
70
+ : noop_query_executor_js_1.NOOP_QUERY_EXECUTOR;
71
+ return this.#toOperationNode(executor);
68
72
  }
69
- compile() {
70
- return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
73
+ async execute(executorProvider) {
74
+ const executor = this.#props.plugins !== undefined
75
+ ? executorProvider.getExecutor().withPlugins(this.#props.plugins)
76
+ : executorProvider.getExecutor();
77
+ return executor.executeQuery(this.#compile(executor), this.#props.queryId);
71
78
  }
72
- async execute() {
73
- return this.#props.executor.executeQuery(this.compile(), this.#props.queryId);
79
+ #toOperationNode(executor) {
80
+ return executor.transformQuery(this.#props.rawNode, this.#props.queryId);
81
+ }
82
+ #compile(executor) {
83
+ return executor.compileQuery(this.#toOperationNode(executor), this.#props.queryId);
74
84
  }
75
85
  }
76
86
  exports.RawBuilder = RawBuilder;
@@ -101,14 +111,3 @@ class AliasedRawBuilder {
101
111
  }
102
112
  }
103
113
  exports.AliasedRawBuilder = AliasedRawBuilder;
104
- function parseRawArg(match, arg) {
105
- if ((0, operation_node_source_js_1.isOperationNodeSource)(arg)) {
106
- return arg.toOperationNode();
107
- }
108
- else if (match === '??') {
109
- return (0, reference_parser_js_1.parseStringReference)(arg);
110
- }
111
- else {
112
- return value_node_js_1.ValueNode.create(arg);
113
- }
114
- }