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.
- package/LICENSE +21 -0
- package/README.md +23 -11
- package/dist/cjs/dialect/mysql/mysql-adapter.js +3 -4
- package/dist/cjs/dialect/mysql/mysql-introspector.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-adapter.js +3 -2
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +2 -1
- package/dist/cjs/index-nodeless.d.ts +2 -0
- package/dist/cjs/index-nodeless.js +2 -0
- package/dist/cjs/kysely.d.ts +3 -6
- package/dist/cjs/kysely.js +9 -14
- package/dist/cjs/migration/migrator.d.ts +22 -0
- package/dist/cjs/migration/migrator.js +27 -5
- package/dist/cjs/operation-node/delete-query-node.d.ts +7 -0
- package/dist/cjs/operation-node/delete-query-node.js +15 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +1 -1
- package/dist/cjs/operation-node/raw-node.d.ts +4 -2
- package/dist/cjs/operation-node/raw-node.js +8 -2
- package/dist/cjs/parser/complex-expression-parser.d.ts +5 -5
- package/dist/cjs/parser/complex-expression-parser.js +5 -4
- package/dist/cjs/parser/filter-parser.d.ts +9 -9
- package/dist/cjs/parser/filter-parser.js +31 -30
- package/dist/cjs/parser/group-by-parser.d.ts +1 -2
- package/dist/cjs/parser/group-by-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.d.ts +1 -2
- package/dist/cjs/parser/insert-values-parser.js +6 -6
- package/dist/cjs/parser/join-parser.d.ts +1 -2
- package/dist/cjs/parser/join-parser.js +8 -7
- package/dist/cjs/parser/order-by-parser.d.ts +1 -2
- package/dist/cjs/parser/order-by-parser.js +4 -4
- package/dist/cjs/parser/parse-utils.d.ts +10 -0
- package/dist/cjs/parser/parse-utils.js +38 -0
- package/dist/cjs/parser/reference-parser.d.ts +2 -3
- package/dist/cjs/parser/reference-parser.js +5 -5
- package/dist/cjs/parser/select-parser.d.ts +1 -2
- package/dist/cjs/parser/select-parser.js +5 -5
- package/dist/cjs/parser/table-parser.d.ts +2 -3
- package/dist/cjs/parser/table-parser.js +5 -5
- package/dist/cjs/parser/update-set-parser.d.ts +1 -2
- package/dist/cjs/parser/update-set-parser.js +2 -2
- package/dist/cjs/parser/value-parser.d.ts +3 -4
- package/dist/cjs/parser/value-parser.js +7 -7
- package/dist/cjs/parser/with-parser.d.ts +1 -2
- package/dist/cjs/parser/with-parser.js +3 -2
- package/dist/cjs/query-builder/delete-query-builder.d.ts +77 -20
- package/dist/cjs/query-builder/delete-query-builder.js +81 -14
- package/dist/cjs/query-builder/expression-builder.d.ts +4 -13
- package/dist/cjs/query-builder/expression-builder.js +12 -21
- package/dist/cjs/query-builder/function-builder.d.ts +2 -6
- package/dist/cjs/query-builder/function-builder.js +5 -8
- package/dist/cjs/query-builder/insert-query-builder.d.ts +11 -9
- package/dist/cjs/query-builder/insert-query-builder.js +8 -7
- package/dist/cjs/query-builder/join-builder.d.ts +0 -2
- package/dist/cjs/query-builder/join-builder.js +8 -8
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +24 -20
- package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
- package/dist/cjs/query-builder/returning-interface.d.ts +2 -2
- package/dist/cjs/query-builder/select-query-builder.d.ts +43 -32
- package/dist/cjs/query-builder/select-query-builder.js +28 -26
- package/dist/cjs/query-builder/update-query-builder.d.ts +29 -25
- package/dist/cjs/query-builder/update-query-builder.js +22 -20
- package/dist/cjs/query-builder/where-interface.d.ts +20 -16
- package/dist/cjs/query-compiler/default-query-compiler.js +16 -7
- package/dist/cjs/query-creator.d.ts +24 -138
- package/dist/cjs/query-creator.js +19 -145
- package/dist/cjs/query-executor/default-query-executor.d.ts +8 -1
- package/dist/cjs/query-executor/default-query-executor.js +13 -13
- package/dist/cjs/query-executor/noop-query-executor.d.ts +8 -0
- package/dist/cjs/query-executor/noop-query-executor.js +8 -1
- package/dist/cjs/query-executor/query-executor-provider.d.ts +8 -0
- package/dist/cjs/query-executor/query-executor-provider.js +2 -0
- package/dist/cjs/query-executor/query-executor.d.ts +7 -0
- package/dist/cjs/raw-builder/raw-builder.d.ts +42 -11
- package/dist/cjs/raw-builder/raw-builder.js +55 -56
- package/dist/cjs/raw-builder/sql.d.ts +323 -0
- package/dist/cjs/raw-builder/sql.js +153 -0
- package/dist/cjs/schema/alter-table-builder.d.ts +20 -8
- package/dist/cjs/schema/alter-table-builder.js +3 -3
- package/dist/cjs/schema/column-definition-builder.d.ts +20 -8
- package/dist/cjs/schema/create-index-builder.d.ts +3 -1
- package/dist/cjs/schema/create-index-builder.js +3 -1
- package/dist/cjs/schema/create-table-builder.d.ts +6 -2
- package/dist/cjs/schema/create-table-builder.js +8 -4
- package/dist/cjs/schema/create-view-builder.d.ts +3 -3
- package/dist/cjs/schema/create-view-builder.js +3 -3
- package/dist/cjs/util/query-id.js +10 -4
- package/dist/cjs/util/random-string.js +65 -2
- package/dist/esm/dialect/mysql/mysql-adapter.js +3 -4
- package/dist/esm/dialect/mysql/mysql-introspector.js +2 -1
- package/dist/esm/dialect/postgres/postgres-adapter.js +3 -2
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -1
- package/dist/esm/index-nodeless.d.ts +2 -0
- package/dist/esm/index-nodeless.js +2 -0
- package/dist/esm/kysely.d.ts +3 -6
- package/dist/esm/kysely.js +9 -14
- package/dist/esm/migration/migrator.d.ts +22 -0
- package/dist/esm/migration/migrator.js +27 -5
- package/dist/esm/operation-node/delete-query-node.d.ts +7 -0
- package/dist/esm/operation-node/delete-query-node.js +15 -0
- package/dist/esm/operation-node/operation-node-transformer.js +1 -1
- package/dist/esm/operation-node/raw-node.d.ts +4 -2
- package/dist/esm/operation-node/raw-node.js +8 -2
- package/dist/esm/parser/complex-expression-parser.d.ts +5 -5
- package/dist/esm/parser/complex-expression-parser.js +5 -4
- package/dist/esm/parser/filter-parser.d.ts +9 -9
- package/dist/esm/parser/filter-parser.js +31 -30
- package/dist/esm/parser/group-by-parser.d.ts +1 -2
- package/dist/esm/parser/group-by-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.d.ts +1 -2
- package/dist/esm/parser/insert-values-parser.js +6 -6
- package/dist/esm/parser/join-parser.d.ts +1 -2
- package/dist/esm/parser/join-parser.js +8 -7
- package/dist/esm/parser/order-by-parser.d.ts +1 -2
- package/dist/esm/parser/order-by-parser.js +4 -4
- package/dist/esm/parser/parse-utils.d.ts +10 -0
- package/dist/esm/parser/parse-utils.js +32 -0
- package/dist/esm/parser/reference-parser.d.ts +2 -3
- package/dist/esm/parser/reference-parser.js +5 -5
- package/dist/esm/parser/select-parser.d.ts +1 -2
- package/dist/esm/parser/select-parser.js +5 -5
- package/dist/esm/parser/table-parser.d.ts +2 -3
- package/dist/esm/parser/table-parser.js +5 -5
- package/dist/esm/parser/update-set-parser.d.ts +1 -2
- package/dist/esm/parser/update-set-parser.js +2 -2
- package/dist/esm/parser/value-parser.d.ts +3 -4
- package/dist/esm/parser/value-parser.js +7 -7
- package/dist/esm/parser/with-parser.d.ts +1 -2
- package/dist/esm/parser/with-parser.js +3 -2
- package/dist/esm/query-builder/delete-query-builder.d.ts +77 -20
- package/dist/esm/query-builder/delete-query-builder.js +81 -14
- package/dist/esm/query-builder/expression-builder.d.ts +4 -13
- package/dist/esm/query-builder/expression-builder.js +12 -21
- package/dist/esm/query-builder/function-builder.d.ts +2 -6
- package/dist/esm/query-builder/function-builder.js +5 -8
- package/dist/esm/query-builder/insert-query-builder.d.ts +11 -9
- package/dist/esm/query-builder/insert-query-builder.js +8 -7
- package/dist/esm/query-builder/join-builder.d.ts +0 -2
- package/dist/esm/query-builder/join-builder.js +8 -8
- package/dist/esm/query-builder/on-conflict-builder.d.ts +24 -20
- package/dist/esm/query-builder/on-conflict-builder.js +17 -17
- package/dist/esm/query-builder/returning-interface.d.ts +2 -2
- package/dist/esm/query-builder/select-query-builder.d.ts +43 -32
- package/dist/esm/query-builder/select-query-builder.js +28 -26
- package/dist/esm/query-builder/update-query-builder.d.ts +29 -25
- package/dist/esm/query-builder/update-query-builder.js +22 -20
- package/dist/esm/query-builder/where-interface.d.ts +20 -16
- package/dist/esm/query-compiler/default-query-compiler.js +16 -7
- package/dist/esm/query-creator.d.ts +24 -138
- package/dist/esm/query-creator.js +19 -145
- package/dist/esm/query-executor/default-query-executor.d.ts +8 -1
- package/dist/esm/query-executor/default-query-executor.js +13 -13
- package/dist/esm/query-executor/noop-query-executor.d.ts +8 -0
- package/dist/esm/query-executor/noop-query-executor.js +7 -0
- package/dist/esm/query-executor/query-executor-provider.d.ts +8 -0
- package/dist/esm/query-executor/query-executor-provider.js +2 -0
- package/dist/esm/query-executor/query-executor.d.ts +7 -0
- package/dist/esm/raw-builder/raw-builder.d.ts +42 -11
- package/dist/esm/raw-builder/raw-builder.js +56 -57
- package/dist/esm/raw-builder/sql.d.ts +323 -0
- package/dist/esm/raw-builder/sql.js +151 -0
- package/dist/esm/schema/alter-table-builder.d.ts +20 -8
- package/dist/esm/schema/alter-table-builder.js +3 -3
- package/dist/esm/schema/column-definition-builder.d.ts +20 -8
- package/dist/esm/schema/create-index-builder.d.ts +3 -1
- package/dist/esm/schema/create-index-builder.js +3 -1
- package/dist/esm/schema/create-table-builder.d.ts +6 -2
- package/dist/esm/schema/create-table-builder.js +8 -4
- package/dist/esm/schema/create-view-builder.d.ts +3 -3
- package/dist/esm/schema/create-view-builder.js +3 -3
- package/dist/esm/util/query-id.js +10 -4
- package/dist/esm/util/random-string.js +65 -2
- package/package.json +13 -13
- package/dist/cjs/parser/parse-context.d.ts +0 -55
- package/dist/cjs/parser/parse-context.js +0 -50
- package/dist/cjs/util/private-symbols.d.ts +0 -5
- package/dist/cjs/util/private-symbols.js +0 -8
- package/dist/esm/parser/parse-context.d.ts +0 -55
- package/dist/esm/parser/parse-context.js +0 -47
- package/dist/esm/util/private-symbols.d.ts +0 -5
- package/dist/esm/util/private-symbols.js +0 -6
|
@@ -8,7 +8,6 @@ import { InsertQueryNode } from './operation-node/insert-query-node.js';
|
|
|
8
8
|
import { SelectQueryNode } from './operation-node/select-query-node.js';
|
|
9
9
|
import { UpdateQueryNode } from './operation-node/update-query-node.js';
|
|
10
10
|
import { parseTable, parseTableExpression, parseTableExpressionOrList, } from './parser/table-parser.js';
|
|
11
|
-
import { RawBuilder } from './raw-builder/raw-builder.js';
|
|
12
11
|
import { parseCommonTableExpression, } from './parser/with-parser.js';
|
|
13
12
|
import { WithNode } from './operation-node/with-node.js';
|
|
14
13
|
import { createQueryId } from './util/query-id.js';
|
|
@@ -23,8 +22,7 @@ export class QueryCreator {
|
|
|
23
22
|
return new SelectQueryBuilder({
|
|
24
23
|
queryId: createQueryId(),
|
|
25
24
|
executor: this.#props.executor,
|
|
26
|
-
|
|
27
|
-
queryNode: SelectQueryNode.create(parseTableExpressionOrList(this.#props.parseContext, from), this.#props.withNode),
|
|
25
|
+
queryNode: SelectQueryNode.create(parseTableExpressionOrList(from), this.#props.withNode),
|
|
28
26
|
});
|
|
29
27
|
}
|
|
30
28
|
/**
|
|
@@ -69,7 +67,6 @@ export class QueryCreator {
|
|
|
69
67
|
return new InsertQueryBuilder({
|
|
70
68
|
queryId: createQueryId(),
|
|
71
69
|
executor: this.#props.executor,
|
|
72
|
-
parseContext: this.#props.parseContext,
|
|
73
70
|
queryNode: InsertQueryNode.create(parseTable(table), this.#props.withNode),
|
|
74
71
|
});
|
|
75
72
|
}
|
|
@@ -96,8 +93,7 @@ export class QueryCreator {
|
|
|
96
93
|
return new DeleteQueryBuilder({
|
|
97
94
|
queryId: createQueryId(),
|
|
98
95
|
executor: this.#props.executor,
|
|
99
|
-
|
|
100
|
-
queryNode: DeleteQueryNode.create(parseTableExpression(this.#props.parseContext, table), this.#props.withNode),
|
|
96
|
+
queryNode: DeleteQueryNode.create(parseTableExpression(table), this.#props.withNode),
|
|
101
97
|
});
|
|
102
98
|
}
|
|
103
99
|
/**
|
|
@@ -127,8 +123,7 @@ export class QueryCreator {
|
|
|
127
123
|
return new UpdateQueryBuilder({
|
|
128
124
|
queryId: createQueryId(),
|
|
129
125
|
executor: this.#props.executor,
|
|
130
|
-
|
|
131
|
-
queryNode: UpdateQueryNode.create(parseTableExpression(this.#props.parseContext, table), this.#props.withNode),
|
|
126
|
+
queryNode: UpdateQueryNode.create(parseTableExpression(table), this.#props.withNode),
|
|
132
127
|
});
|
|
133
128
|
}
|
|
134
129
|
/**
|
|
@@ -173,7 +168,7 @@ export class QueryCreator {
|
|
|
173
168
|
* ```
|
|
174
169
|
*/
|
|
175
170
|
with(name, expression) {
|
|
176
|
-
const cte = parseCommonTableExpression(
|
|
171
|
+
const cte = parseCommonTableExpression(name, expression);
|
|
177
172
|
return new QueryCreator({
|
|
178
173
|
...this.#props,
|
|
179
174
|
withNode: this.#props.withNode
|
|
@@ -187,7 +182,7 @@ export class QueryCreator {
|
|
|
187
182
|
* See the {@link with} method for examples and more documentation.
|
|
188
183
|
*/
|
|
189
184
|
withRecursive(name, expression) {
|
|
190
|
-
const cte = parseCommonTableExpression(
|
|
185
|
+
const cte = parseCommonTableExpression(name, expression);
|
|
191
186
|
return new QueryCreator({
|
|
192
187
|
...this.#props,
|
|
193
188
|
withNode: this.#props.withNode
|
|
@@ -202,14 +197,18 @@ export class QueryCreator {
|
|
|
202
197
|
* This only affects the query created through the builder returned from
|
|
203
198
|
* this method and doesn't modify the `db` instance.
|
|
204
199
|
*
|
|
200
|
+
* See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/schemas.md)
|
|
201
|
+
* for a more detailed explanation.
|
|
202
|
+
*
|
|
205
203
|
* ### Examples
|
|
206
204
|
*
|
|
207
205
|
* ```
|
|
208
|
-
* await db
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
206
|
+
* await db
|
|
207
|
+
* .withSchema('mammals')
|
|
208
|
+
* .selectFrom('pet')
|
|
209
|
+
* .selectAll()
|
|
210
|
+
* .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
|
|
211
|
+
* .execute()
|
|
213
212
|
* ```
|
|
214
213
|
*
|
|
215
214
|
* The generated SQL (PostgreSQL):
|
|
@@ -225,10 +224,11 @@ export class QueryCreator {
|
|
|
225
224
|
* doesn't belong to:
|
|
226
225
|
*
|
|
227
226
|
* ```
|
|
228
|
-
* await db
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
227
|
+
* await db
|
|
228
|
+
* .withSchema('mammals')
|
|
229
|
+
* .selectFrom('pet as p')
|
|
230
|
+
* .select('p.name')
|
|
231
|
+
* .execute()
|
|
232
232
|
* ```
|
|
233
233
|
*
|
|
234
234
|
* The generated SQL (PostgreSQL):
|
|
@@ -243,130 +243,4 @@ export class QueryCreator {
|
|
|
243
243
|
executor: this.#props.executor.withPluginAtFront(new WithSchemaPlugin(schema)),
|
|
244
244
|
});
|
|
245
245
|
}
|
|
246
|
-
/**
|
|
247
|
-
* Provides a way to pass arbitrary SQL into your query and executing completely
|
|
248
|
-
* raw queries.
|
|
249
|
-
*
|
|
250
|
-
* You can use the strings `?` and `??` in the `sql` to bind parameters such as
|
|
251
|
-
* user input to the SQL. You should never EVER concatenate untrusted user
|
|
252
|
-
* input to the SQL string to avoid injection vulnerabilities. Instead use `?`
|
|
253
|
-
* in place of a value and pass the actual value in the `parameters` list. See
|
|
254
|
-
* the examples below.
|
|
255
|
-
*
|
|
256
|
-
* You should only use `raw` when there is no other way to get the job done. This is
|
|
257
|
-
* because Kysely is not able to use type inference when you use raw SQL. For example
|
|
258
|
-
* Kysely won't be able to automatically provide you with the correct query result
|
|
259
|
-
* type. However, there are ways to manually provide types when you use `raw` in most
|
|
260
|
-
* cases. See the examples below.
|
|
261
|
-
*
|
|
262
|
-
* Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
|
|
263
|
-
* `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
|
|
264
|
-
*
|
|
265
|
-
* ### Examples
|
|
266
|
-
*
|
|
267
|
-
* Example of using `raw` in a select statement:
|
|
268
|
-
*
|
|
269
|
-
* ```ts
|
|
270
|
-
* const [person] = await db.selectFrom('person')
|
|
271
|
-
* .select(db.raw<string>('concat(first_name, ' ', last_name)').as('name'))
|
|
272
|
-
* .where('id', '=', 1)
|
|
273
|
-
* .execute()
|
|
274
|
-
*
|
|
275
|
-
* console.log(person.name)
|
|
276
|
-
* ```
|
|
277
|
-
*
|
|
278
|
-
* The generated SQL (PostgreSQL):
|
|
279
|
-
*
|
|
280
|
-
* ```sql
|
|
281
|
-
* select concat(first_name, ' ', last_name) as "name"
|
|
282
|
-
* from "person" where "id" = 1
|
|
283
|
-
* ```
|
|
284
|
-
*
|
|
285
|
-
* The above example selects computed column `name` by concatenating the first name
|
|
286
|
-
* and last name together.
|
|
287
|
-
*
|
|
288
|
-
* There are couple of things worth noticing:
|
|
289
|
-
*
|
|
290
|
-
* 1. You need to provide the output type of your SQL segment for the `raw` method
|
|
291
|
-
* so that Kysely knows what type to give for the `name` column. In this case it's
|
|
292
|
-
* a `string` since that's the output type of the `concat` function in SQL.
|
|
293
|
-
*
|
|
294
|
-
* 2. You need to give an alias for the selection using the `as` method so that
|
|
295
|
-
* Kysely is able to add a column to the output type. The alias needs to be
|
|
296
|
-
* known at compile time! If you pass a string variable whose value is not known
|
|
297
|
-
* at compile time, there is no way for Kysely or typescript to add a column to
|
|
298
|
-
* the output type. In this case you need to use the `castTo` method on the query
|
|
299
|
-
* to specify a return type for the query.
|
|
300
|
-
*
|
|
301
|
-
* We could've also used a `??` placeholder to provide `first_name` and `last_name` like
|
|
302
|
-
* this:
|
|
303
|
-
*
|
|
304
|
-
* ```ts
|
|
305
|
-
* db.raw<string>('concat(??, ' ', ??)', ['first_name', 'last_name'])
|
|
306
|
-
* ```
|
|
307
|
-
*
|
|
308
|
-
* or this:
|
|
309
|
-
*
|
|
310
|
-
* ```ts
|
|
311
|
-
* db.raw<string>('concat(??, ' ', ??)', ['person.first_name', 'person.last_name'])
|
|
312
|
-
* ```
|
|
313
|
-
*
|
|
314
|
-
* But it's often cleaner to just write the column names in the SQL. Again remember to
|
|
315
|
-
* never concatenate column names or any other untrusted user input to the SQL string or you
|
|
316
|
-
* are going to create an injection vulnerability. All user input should go to the parameters
|
|
317
|
-
* array, never to the SQL string directly. But if the column names or values are trusted
|
|
318
|
-
* and known at compile time, there is no reason to use parameters.
|
|
319
|
-
*
|
|
320
|
-
* Example of using `raw` in `where`:
|
|
321
|
-
*
|
|
322
|
-
* ```ts
|
|
323
|
-
* function getPersonsOlderThan(ageLimit: number) {
|
|
324
|
-
* return await db.selectFrom('person')
|
|
325
|
-
* .selectAll()
|
|
326
|
-
* .where(
|
|
327
|
-
* db.raw('now() - birth_date'),
|
|
328
|
-
* '>',
|
|
329
|
-
* db.raw('interval ? year', [ageLimit.toString()])
|
|
330
|
-
* )
|
|
331
|
-
* .execute()
|
|
332
|
-
* }
|
|
333
|
-
* ```
|
|
334
|
-
*
|
|
335
|
-
* The generated SQL (PostgreSQL):
|
|
336
|
-
*
|
|
337
|
-
* ```sql
|
|
338
|
-
* select * from "person" where now() - birth_date > interval $1 year
|
|
339
|
-
* ```
|
|
340
|
-
*
|
|
341
|
-
* The function in the above example returns people that are older than the given number of
|
|
342
|
-
* years. The number of years in this example is an untrusted user input, and therefore we use
|
|
343
|
-
* a `?` placeholder for it.
|
|
344
|
-
*
|
|
345
|
-
* Example of creating a completely raw query from scratch:
|
|
346
|
-
*
|
|
347
|
-
* ```ts
|
|
348
|
-
* const result = await db.raw<Person>('select p.* from person p').execute()
|
|
349
|
-
* const persons = result.rows
|
|
350
|
-
* ```
|
|
351
|
-
*
|
|
352
|
-
* For a raw query, you need to specify the type of the returned __row__. In
|
|
353
|
-
* this case we know the resulting items will be of type `Person` se specify that.
|
|
354
|
-
* The result of `execute()` method is always an array. In this case the type of
|
|
355
|
-
* the `persons` variable is `Person[]`.
|
|
356
|
-
*
|
|
357
|
-
* @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
|
|
358
|
-
* placeholders. `?` for values and `??` for identifiers such as column names
|
|
359
|
-
* or `column.table` references.
|
|
360
|
-
*
|
|
361
|
-
* @param params - The parameters that will be bound to the `?` and `??` placeholders in
|
|
362
|
-
* the sql string.
|
|
363
|
-
*/
|
|
364
|
-
raw(sql, parameters) {
|
|
365
|
-
return new RawBuilder({
|
|
366
|
-
queryId: createQueryId(),
|
|
367
|
-
executor: this.#props.executor,
|
|
368
|
-
sql,
|
|
369
|
-
parameters,
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
246
|
}
|
|
@@ -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.
|
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
import { QueryExecutor } from './query-executor.js';
|
|
3
3
|
export class DefaultQueryExecutor extends QueryExecutor {
|
|
4
4
|
#compiler;
|
|
5
|
+
#adapter;
|
|
5
6
|
#connectionProvider;
|
|
6
|
-
constructor(compiler, connectionProvider, plugins = []) {
|
|
7
|
+
constructor(compiler, adapter, connectionProvider, plugins = []) {
|
|
7
8
|
super(plugins);
|
|
8
9
|
this.#compiler = compiler;
|
|
10
|
+
this.#adapter = adapter;
|
|
9
11
|
this.#connectionProvider = connectionProvider;
|
|
10
12
|
}
|
|
13
|
+
get adapter() {
|
|
14
|
+
return this.#adapter;
|
|
15
|
+
}
|
|
11
16
|
compileQuery(node) {
|
|
12
17
|
return this.#compiler.compileQuery(node);
|
|
13
18
|
}
|
|
@@ -16,24 +21,19 @@ export class DefaultQueryExecutor extends QueryExecutor {
|
|
|
16
21
|
return connection.executeQuery(compiledQuery);
|
|
17
22
|
});
|
|
18
23
|
}
|
|
24
|
+
withPlugins(plugins) {
|
|
25
|
+
return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, ...plugins]);
|
|
26
|
+
}
|
|
19
27
|
withPlugin(plugin) {
|
|
20
|
-
return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
|
|
21
|
-
...this.plugins,
|
|
22
|
-
plugin,
|
|
23
|
-
]);
|
|
28
|
+
return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, plugin]);
|
|
24
29
|
}
|
|
25
30
|
withPluginAtFront(plugin) {
|
|
26
|
-
return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
|
|
27
|
-
plugin,
|
|
28
|
-
...this.plugins,
|
|
29
|
-
]);
|
|
31
|
+
return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [plugin, ...this.plugins]);
|
|
30
32
|
}
|
|
31
33
|
withConnectionProvider(connectionProvider) {
|
|
32
|
-
return new DefaultQueryExecutor(this.#compiler, connectionProvider, [
|
|
33
|
-
...this.plugins,
|
|
34
|
-
]);
|
|
34
|
+
return new DefaultQueryExecutor(this.#compiler, this.#adapter, connectionProvider, [...this.plugins]);
|
|
35
35
|
}
|
|
36
36
|
withoutPlugins() {
|
|
37
|
-
return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, []);
|
|
37
|
+
return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, []);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -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;
|
|
@@ -6,6 +6,9 @@ import { QueryExecutor } from './query-executor.js';
|
|
|
6
6
|
* other needed things to actually execute queries.
|
|
7
7
|
*/
|
|
8
8
|
export class NoopQueryExecutor extends QueryExecutor {
|
|
9
|
+
get adapter() {
|
|
10
|
+
throw new Error('this query cannot be compiled to SQL');
|
|
11
|
+
}
|
|
9
12
|
compileQuery() {
|
|
10
13
|
throw new Error('this query cannot be compiled to SQL');
|
|
11
14
|
}
|
|
@@ -18,6 +21,9 @@ export class NoopQueryExecutor extends QueryExecutor {
|
|
|
18
21
|
withPlugin(plugin) {
|
|
19
22
|
return new NoopQueryExecutor([...this.plugins, plugin]);
|
|
20
23
|
}
|
|
24
|
+
withPlugins(plugins) {
|
|
25
|
+
return new NoopQueryExecutor([...this.plugins, ...plugins]);
|
|
26
|
+
}
|
|
21
27
|
withPluginAtFront(plugin) {
|
|
22
28
|
return new NoopQueryExecutor([plugin, ...this.plugins]);
|
|
23
29
|
}
|
|
@@ -25,3 +31,4 @@ export class NoopQueryExecutor extends QueryExecutor {
|
|
|
25
31
|
return new NoopQueryExecutor([]);
|
|
26
32
|
}
|
|
27
33
|
}
|
|
34
|
+
export const NOOP_QUERY_EXECUTOR = new NoopQueryExecutor();
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
44
|
-
readonly
|
|
45
|
-
readonly
|
|
46
|
-
readonly parameters?: ReadonlyArray<unknown>;
|
|
75
|
+
readonly queryId: QueryId;
|
|
76
|
+
readonly rawNode: RawNode;
|
|
77
|
+
readonly plugins?: ReadonlyArray<KyselyPlugin>;
|
|
47
78
|
}
|
|
@@ -1,74 +1,84 @@
|
|
|
1
1
|
/// <reference types="./raw-builder.d.ts" />
|
|
2
2
|
import { AliasNode } from '../operation-node/alias-node.js';
|
|
3
|
-
import { isOperationNodeSource, } from '../operation-node/operation-node-source.js';
|
|
4
|
-
import { RawNode } from '../operation-node/raw-node.js';
|
|
5
|
-
import { ValueNode } from '../operation-node/value-node.js';
|
|
6
|
-
import { parseStringReference } from '../parser/reference-parser.js';
|
|
7
3
|
import { preventAwait } from '../util/prevent-await.js';
|
|
8
|
-
import { createQueryId } from '../util/query-id.js';
|
|
9
4
|
import { freeze } from '../util/object-utils.js';
|
|
10
|
-
import {
|
|
5
|
+
import { NOOP_QUERY_EXECUTOR } from '../query-executor/noop-query-executor.js';
|
|
6
|
+
/**
|
|
7
|
+
* An instance of this class can be used to create raw SQL snippets or queries.
|
|
8
|
+
*
|
|
9
|
+
* You shouldn't need to create `RawBuilder` instances directly. Instead you should
|
|
10
|
+
* use the {@link sql} template tag.
|
|
11
|
+
*/
|
|
11
12
|
export class RawBuilder {
|
|
12
13
|
#props;
|
|
13
14
|
constructor(props) {
|
|
14
|
-
this.#props = freeze(
|
|
15
|
-
queryId: props.queryId ?? createQueryId(),
|
|
16
|
-
executor: props.executor ?? new NoopQueryExecutor(),
|
|
17
|
-
sql: props.sql,
|
|
18
|
-
parameters: props.parameters,
|
|
19
|
-
});
|
|
15
|
+
this.#props = freeze(props);
|
|
20
16
|
}
|
|
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
|
+
*/
|
|
21
42
|
as(alias) {
|
|
22
43
|
return new AliasedRawBuilder(this, alias);
|
|
23
44
|
}
|
|
24
45
|
/**
|
|
25
46
|
* Change the output type of the raw expression.
|
|
26
47
|
*
|
|
27
|
-
* This doesn't
|
|
28
|
-
* of this `RawBuilder` with a new output type.
|
|
48
|
+
* This method call doesn't change the SQL in any way. This methods simply
|
|
49
|
+
* returns a copy of this `RawBuilder` with a new output type.
|
|
29
50
|
*/
|
|
30
51
|
castTo() {
|
|
31
|
-
return new RawBuilder({
|
|
32
|
-
...this.#props,
|
|
33
|
-
});
|
|
52
|
+
return new RawBuilder({ ...this.#props });
|
|
34
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Adds a plugin for this SQL snippet.
|
|
56
|
+
*/
|
|
35
57
|
withPlugin(plugin) {
|
|
36
58
|
return new RawBuilder({
|
|
37
59
|
...this.#props,
|
|
38
|
-
|
|
60
|
+
plugins: this.#props.plugins !== undefined
|
|
61
|
+
? freeze([...this.#props.plugins, plugin])
|
|
62
|
+
: freeze([plugin]),
|
|
39
63
|
});
|
|
40
64
|
}
|
|
41
65
|
toOperationNode() {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const argNodes = [];
|
|
47
|
-
let paramIdx = 0;
|
|
48
|
-
let sqlIdx = 0;
|
|
49
|
-
let match = null;
|
|
50
|
-
while ((match = parameterPlaceholderRegex.exec(sql))) {
|
|
51
|
-
const str = match[1];
|
|
52
|
-
if (paramIdx >= parameters.length) {
|
|
53
|
-
throw new Error(`value not provided for all placeholders in string ${sql}`);
|
|
54
|
-
}
|
|
55
|
-
if (match.index > 0 && sql[match.index - 1] === '\\') {
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
sqlFragments.push(sql.slice(sqlIdx, match.index).replace(/\\\?/g, '?'));
|
|
59
|
-
argNodes.push(parseRawArg(str, parameters[paramIdx]));
|
|
60
|
-
sqlIdx = match.index + str.length;
|
|
61
|
-
++paramIdx;
|
|
62
|
-
}
|
|
63
|
-
sqlFragments.push(sql.slice(sqlIdx));
|
|
64
|
-
const rawNode = RawNode.create(sqlFragments, argNodes);
|
|
65
|
-
return this.#props.executor.transformQuery(rawNode, this.#props.queryId);
|
|
66
|
+
const executor = this.#props.plugins !== undefined
|
|
67
|
+
? NOOP_QUERY_EXECUTOR.withPlugins(this.#props.plugins)
|
|
68
|
+
: NOOP_QUERY_EXECUTOR;
|
|
69
|
+
return this.#toOperationNode(executor);
|
|
66
70
|
}
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
async execute(executorProvider) {
|
|
72
|
+
const executor = this.#props.plugins !== undefined
|
|
73
|
+
? executorProvider.getExecutor().withPlugins(this.#props.plugins)
|
|
74
|
+
: executorProvider.getExecutor();
|
|
75
|
+
return executor.executeQuery(this.#compile(executor), this.#props.queryId);
|
|
69
76
|
}
|
|
70
|
-
|
|
71
|
-
return
|
|
77
|
+
#toOperationNode(executor) {
|
|
78
|
+
return executor.transformQuery(this.#props.rawNode, this.#props.queryId);
|
|
79
|
+
}
|
|
80
|
+
#compile(executor) {
|
|
81
|
+
return executor.compileQuery(this.#toOperationNode(executor), this.#props.queryId);
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
84
|
preventAwait(RawBuilder, "don't await RawBuilder instances directly. To execute the query you need to call `execute`");
|
|
@@ -97,14 +107,3 @@ export class AliasedRawBuilder {
|
|
|
97
107
|
this.#alias = alias;
|
|
98
108
|
}
|
|
99
109
|
}
|
|
100
|
-
function parseRawArg(match, arg) {
|
|
101
|
-
if (isOperationNodeSource(arg)) {
|
|
102
|
-
return arg.toOperationNode();
|
|
103
|
-
}
|
|
104
|
-
else if (match === '??') {
|
|
105
|
-
return parseStringReference(arg);
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
return ValueNode.create(arg);
|
|
109
|
-
}
|
|
110
|
-
}
|