kysely 0.16.11 → 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 (170) 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-introspector.js +2 -1
  4. package/dist/cjs/dialect/postgres/postgres-adapter.js +3 -2
  5. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +2 -1
  6. package/dist/cjs/index-nodeless.d.ts +2 -0
  7. package/dist/cjs/index-nodeless.js +2 -0
  8. package/dist/cjs/kysely.d.ts +3 -6
  9. package/dist/cjs/kysely.js +7 -12
  10. package/dist/cjs/migration/migrator.js +4 -4
  11. package/dist/cjs/operation-node/operation-node-transformer.js +1 -1
  12. package/dist/cjs/operation-node/raw-node.d.ts +4 -2
  13. package/dist/cjs/operation-node/raw-node.js +8 -2
  14. package/dist/cjs/parser/complex-expression-parser.d.ts +5 -5
  15. package/dist/cjs/parser/complex-expression-parser.js +5 -4
  16. package/dist/cjs/parser/filter-parser.d.ts +9 -9
  17. package/dist/cjs/parser/filter-parser.js +31 -30
  18. package/dist/cjs/parser/group-by-parser.d.ts +1 -2
  19. package/dist/cjs/parser/group-by-parser.js +2 -2
  20. package/dist/cjs/parser/insert-values-parser.d.ts +1 -2
  21. package/dist/cjs/parser/insert-values-parser.js +6 -6
  22. package/dist/cjs/parser/join-parser.d.ts +1 -2
  23. package/dist/cjs/parser/join-parser.js +8 -7
  24. package/dist/cjs/parser/order-by-parser.d.ts +1 -2
  25. package/dist/cjs/parser/order-by-parser.js +4 -4
  26. package/dist/cjs/parser/parse-utils.d.ts +10 -0
  27. package/dist/cjs/parser/parse-utils.js +38 -0
  28. package/dist/cjs/parser/reference-parser.d.ts +2 -3
  29. package/dist/cjs/parser/reference-parser.js +5 -5
  30. package/dist/cjs/parser/select-parser.d.ts +1 -2
  31. package/dist/cjs/parser/select-parser.js +5 -5
  32. package/dist/cjs/parser/table-parser.d.ts +2 -3
  33. package/dist/cjs/parser/table-parser.js +5 -5
  34. package/dist/cjs/parser/update-set-parser.d.ts +1 -2
  35. package/dist/cjs/parser/update-set-parser.js +2 -2
  36. package/dist/cjs/parser/value-parser.d.ts +3 -4
  37. package/dist/cjs/parser/value-parser.js +7 -7
  38. package/dist/cjs/parser/with-parser.d.ts +1 -2
  39. package/dist/cjs/parser/with-parser.js +3 -2
  40. package/dist/cjs/query-builder/delete-query-builder.d.ts +23 -19
  41. package/dist/cjs/query-builder/delete-query-builder.js +15 -15
  42. package/dist/cjs/query-builder/expression-builder.d.ts +4 -13
  43. package/dist/cjs/query-builder/expression-builder.js +12 -21
  44. package/dist/cjs/query-builder/function-builder.d.ts +2 -6
  45. package/dist/cjs/query-builder/function-builder.js +5 -8
  46. package/dist/cjs/query-builder/insert-query-builder.d.ts +12 -8
  47. package/dist/cjs/query-builder/insert-query-builder.js +8 -7
  48. package/dist/cjs/query-builder/join-builder.d.ts +0 -2
  49. package/dist/cjs/query-builder/join-builder.js +8 -8
  50. package/dist/cjs/query-builder/on-conflict-builder.d.ts +24 -20
  51. package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
  52. package/dist/cjs/query-builder/returning-interface.d.ts +3 -1
  53. package/dist/cjs/query-builder/select-query-builder.d.ts +41 -29
  54. package/dist/cjs/query-builder/select-query-builder.js +28 -26
  55. package/dist/cjs/query-builder/update-query-builder.d.ts +30 -24
  56. package/dist/cjs/query-builder/update-query-builder.js +22 -20
  57. package/dist/cjs/query-builder/where-interface.d.ts +20 -16
  58. package/dist/cjs/query-compiler/default-query-compiler.js +1 -1
  59. package/dist/cjs/query-creator.d.ts +10 -129
  60. package/dist/cjs/query-creator.js +5 -136
  61. package/dist/cjs/query-executor/default-query-executor.d.ts +8 -1
  62. package/dist/cjs/query-executor/default-query-executor.js +13 -13
  63. package/dist/cjs/query-executor/noop-query-executor.d.ts +8 -0
  64. package/dist/cjs/query-executor/noop-query-executor.js +8 -1
  65. package/dist/cjs/query-executor/query-executor-provider.d.ts +8 -0
  66. package/dist/cjs/query-executor/query-executor-provider.js +2 -0
  67. package/dist/cjs/query-executor/query-executor.d.ts +7 -0
  68. package/dist/cjs/raw-builder/raw-builder.d.ts +42 -11
  69. package/dist/cjs/raw-builder/raw-builder.js +55 -56
  70. package/dist/cjs/raw-builder/sql.d.ts +323 -0
  71. package/dist/cjs/raw-builder/sql.js +153 -0
  72. package/dist/cjs/schema/alter-table-builder.d.ts +20 -8
  73. package/dist/cjs/schema/column-definition-builder.d.ts +20 -8
  74. package/dist/cjs/schema/create-index-builder.d.ts +3 -1
  75. package/dist/cjs/schema/create-index-builder.js +3 -1
  76. package/dist/cjs/schema/create-table-builder.d.ts +6 -2
  77. package/dist/cjs/schema/create-table-builder.js +6 -2
  78. package/dist/cjs/schema/create-view-builder.d.ts +3 -3
  79. package/dist/cjs/schema/create-view-builder.js +3 -3
  80. package/dist/cjs/util/query-id.js +10 -4
  81. package/dist/cjs/util/random-string.js +65 -2
  82. package/dist/esm/dialect/mysql/mysql-adapter.js +3 -4
  83. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -1
  84. package/dist/esm/dialect/postgres/postgres-adapter.js +3 -2
  85. package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -1
  86. package/dist/esm/index-nodeless.d.ts +2 -0
  87. package/dist/esm/index-nodeless.js +2 -0
  88. package/dist/esm/kysely.d.ts +3 -6
  89. package/dist/esm/kysely.js +7 -12
  90. package/dist/esm/migration/migrator.js +4 -4
  91. package/dist/esm/operation-node/operation-node-transformer.js +1 -1
  92. package/dist/esm/operation-node/raw-node.d.ts +4 -2
  93. package/dist/esm/operation-node/raw-node.js +8 -2
  94. package/dist/esm/parser/complex-expression-parser.d.ts +5 -5
  95. package/dist/esm/parser/complex-expression-parser.js +5 -4
  96. package/dist/esm/parser/filter-parser.d.ts +9 -9
  97. package/dist/esm/parser/filter-parser.js +31 -30
  98. package/dist/esm/parser/group-by-parser.d.ts +1 -2
  99. package/dist/esm/parser/group-by-parser.js +2 -2
  100. package/dist/esm/parser/insert-values-parser.d.ts +1 -2
  101. package/dist/esm/parser/insert-values-parser.js +6 -6
  102. package/dist/esm/parser/join-parser.d.ts +1 -2
  103. package/dist/esm/parser/join-parser.js +8 -7
  104. package/dist/esm/parser/order-by-parser.d.ts +1 -2
  105. package/dist/esm/parser/order-by-parser.js +4 -4
  106. package/dist/esm/parser/parse-utils.d.ts +10 -0
  107. package/dist/esm/parser/parse-utils.js +32 -0
  108. package/dist/esm/parser/reference-parser.d.ts +2 -3
  109. package/dist/esm/parser/reference-parser.js +5 -5
  110. package/dist/esm/parser/select-parser.d.ts +1 -2
  111. package/dist/esm/parser/select-parser.js +5 -5
  112. package/dist/esm/parser/table-parser.d.ts +2 -3
  113. package/dist/esm/parser/table-parser.js +5 -5
  114. package/dist/esm/parser/update-set-parser.d.ts +1 -2
  115. package/dist/esm/parser/update-set-parser.js +2 -2
  116. package/dist/esm/parser/value-parser.d.ts +3 -4
  117. package/dist/esm/parser/value-parser.js +7 -7
  118. package/dist/esm/parser/with-parser.d.ts +1 -2
  119. package/dist/esm/parser/with-parser.js +3 -2
  120. package/dist/esm/query-builder/delete-query-builder.d.ts +23 -19
  121. package/dist/esm/query-builder/delete-query-builder.js +15 -15
  122. package/dist/esm/query-builder/expression-builder.d.ts +4 -13
  123. package/dist/esm/query-builder/expression-builder.js +12 -21
  124. package/dist/esm/query-builder/function-builder.d.ts +2 -6
  125. package/dist/esm/query-builder/function-builder.js +5 -8
  126. package/dist/esm/query-builder/insert-query-builder.d.ts +12 -8
  127. package/dist/esm/query-builder/insert-query-builder.js +8 -7
  128. package/dist/esm/query-builder/join-builder.d.ts +0 -2
  129. package/dist/esm/query-builder/join-builder.js +8 -8
  130. package/dist/esm/query-builder/on-conflict-builder.d.ts +24 -20
  131. package/dist/esm/query-builder/on-conflict-builder.js +17 -17
  132. package/dist/esm/query-builder/returning-interface.d.ts +3 -1
  133. package/dist/esm/query-builder/select-query-builder.d.ts +41 -29
  134. package/dist/esm/query-builder/select-query-builder.js +28 -26
  135. package/dist/esm/query-builder/update-query-builder.d.ts +30 -24
  136. package/dist/esm/query-builder/update-query-builder.js +22 -20
  137. package/dist/esm/query-builder/where-interface.d.ts +20 -16
  138. package/dist/esm/query-compiler/default-query-compiler.js +1 -1
  139. package/dist/esm/query-creator.d.ts +10 -129
  140. package/dist/esm/query-creator.js +5 -136
  141. package/dist/esm/query-executor/default-query-executor.d.ts +8 -1
  142. package/dist/esm/query-executor/default-query-executor.js +13 -13
  143. package/dist/esm/query-executor/noop-query-executor.d.ts +8 -0
  144. package/dist/esm/query-executor/noop-query-executor.js +7 -0
  145. package/dist/esm/query-executor/query-executor-provider.d.ts +8 -0
  146. package/dist/esm/query-executor/query-executor-provider.js +2 -0
  147. package/dist/esm/query-executor/query-executor.d.ts +7 -0
  148. package/dist/esm/raw-builder/raw-builder.d.ts +42 -11
  149. package/dist/esm/raw-builder/raw-builder.js +56 -57
  150. package/dist/esm/raw-builder/sql.d.ts +323 -0
  151. package/dist/esm/raw-builder/sql.js +151 -0
  152. package/dist/esm/schema/alter-table-builder.d.ts +20 -8
  153. package/dist/esm/schema/column-definition-builder.d.ts +20 -8
  154. package/dist/esm/schema/create-index-builder.d.ts +3 -1
  155. package/dist/esm/schema/create-index-builder.js +3 -1
  156. package/dist/esm/schema/create-table-builder.d.ts +6 -2
  157. package/dist/esm/schema/create-table-builder.js +6 -2
  158. package/dist/esm/schema/create-view-builder.d.ts +3 -3
  159. package/dist/esm/schema/create-view-builder.js +3 -3
  160. package/dist/esm/util/query-id.js +10 -4
  161. package/dist/esm/util/random-string.js +65 -2
  162. package/package.json +13 -13
  163. package/dist/cjs/parser/parse-context.d.ts +0 -55
  164. package/dist/cjs/parser/parse-context.js +0 -50
  165. package/dist/cjs/util/private-symbols.d.ts +0 -5
  166. package/dist/cjs/util/private-symbols.js +0 -8
  167. package/dist/esm/parser/parse-context.d.ts +0 -55
  168. package/dist/esm/parser/parse-context.js +0 -47
  169. package/dist/esm/util/private-symbols.d.ts +0 -5
  170. 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
@@ -250,131 +245,5 @@ class QueryCreator {
250
245
  executor: this.#props.executor.withPluginAtFront(new with_schema_plugin_js_1.WithSchemaPlugin(schema)),
251
246
  });
252
247
  }
253
- /**
254
- * Provides a way to pass arbitrary SQL into your query and executing completely
255
- * raw queries.
256
- *
257
- * You can use the strings `?` and `??` in the `sql` to bind parameters such as
258
- * user input to the SQL. You should never EVER concatenate untrusted user
259
- * input to the SQL string to avoid injection vulnerabilities. Instead use `?`
260
- * in place of a value and pass the actual value in the `parameters` list. See
261
- * the examples below.
262
- *
263
- * You should only use `raw` when there is no other way to get the job done. This is
264
- * because Kysely is not able to use type inference when you use raw SQL. For example
265
- * Kysely won't be able to automatically provide you with the correct query result
266
- * type. However, there are ways to manually provide types when you use `raw` in most
267
- * cases. See the examples below.
268
- *
269
- * Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
270
- * `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
271
- *
272
- * ### Examples
273
- *
274
- * Example of using `raw` in a select statement:
275
- *
276
- * ```ts
277
- * const [person] = await db.selectFrom('person')
278
- * .select(db.raw<string>('concat(first_name, ' ', last_name)').as('name'))
279
- * .where('id', '=', 1)
280
- * .execute()
281
- *
282
- * console.log(person.name)
283
- * ```
284
- *
285
- * The generated SQL (PostgreSQL):
286
- *
287
- * ```sql
288
- * select concat(first_name, ' ', last_name) as "name"
289
- * from "person" where "id" = 1
290
- * ```
291
- *
292
- * The above example selects computed column `name` by concatenating the first name
293
- * and last name together.
294
- *
295
- * There are couple of things worth noticing:
296
- *
297
- * 1. You need to provide the output type of your SQL segment for the `raw` method
298
- * so that Kysely knows what type to give for the `name` column. In this case it's
299
- * a `string` since that's the output type of the `concat` function in SQL.
300
- *
301
- * 2. You need to give an alias for the selection using the `as` method so that
302
- * Kysely is able to add a column to the output type. The alias needs to be
303
- * known at compile time! If you pass a string variable whose value is not known
304
- * at compile time, there is no way for Kysely or typescript to add a column to
305
- * the output type. In this case you need to use the `castTo` method on the query
306
- * to specify a return type for the query.
307
- *
308
- * We could've also used a `??` placeholder to provide `first_name` and `last_name` like
309
- * this:
310
- *
311
- * ```ts
312
- * db.raw<string>('concat(??, ' ', ??)', ['first_name', 'last_name'])
313
- * ```
314
- *
315
- * or this:
316
- *
317
- * ```ts
318
- * db.raw<string>('concat(??, ' ', ??)', ['person.first_name', 'person.last_name'])
319
- * ```
320
- *
321
- * But it's often cleaner to just write the column names in the SQL. Again remember to
322
- * never concatenate column names or any other untrusted user input to the SQL string or you
323
- * are going to create an injection vulnerability. All user input should go to the parameters
324
- * array, never to the SQL string directly. But if the column names or values are trusted
325
- * and known at compile time, there is no reason to use parameters.
326
- *
327
- * Example of using `raw` in `where`:
328
- *
329
- * ```ts
330
- * function getPersonsOlderThan(ageLimit: number) {
331
- * return await db.selectFrom('person')
332
- * .selectAll()
333
- * .where(
334
- * db.raw('now() - birth_date'),
335
- * '>',
336
- * db.raw('interval ? year', [ageLimit.toString()])
337
- * )
338
- * .execute()
339
- * }
340
- * ```
341
- *
342
- * The generated SQL (PostgreSQL):
343
- *
344
- * ```sql
345
- * select * from "person" where now() - birth_date > interval $1 year
346
- * ```
347
- *
348
- * The function in the above example returns people that are older than the given number of
349
- * years. The number of years in this example is an untrusted user input, and therefore we use
350
- * a `?` placeholder for it.
351
- *
352
- * Example of creating a completely raw query from scratch:
353
- *
354
- * ```ts
355
- * const result = await db.raw<Person>('select p.* from person p').execute()
356
- * const persons = result.rows
357
- * ```
358
- *
359
- * For a raw query, you need to specify the type of the returned __row__. In
360
- * this case we know the resulting items will be of type `Person` se specify that.
361
- * The result of `execute()` method is always an array. In this case the type of
362
- * the `persons` variable is `Person[]`.
363
- *
364
- * @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
365
- * placeholders. `?` for values and `??` for identifiers such as column names
366
- * or `column.table` references.
367
- *
368
- * @param params - The parameters that will be bound to the `?` and `??` placeholders in
369
- * the sql string.
370
- */
371
- raw(sql, parameters) {
372
- return new raw_builder_js_1.RawBuilder({
373
- queryId: (0, query_id_js_1.createQueryId)(),
374
- executor: this.#props.executor,
375
- sql,
376
- parameters,
377
- });
378
- }
379
248
  }
380
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
- }