kysely 0.17.1 → 0.18.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 (100) hide show
  1. package/README.md +2 -0
  2. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -0
  3. package/dist/cjs/driver/default-connection-provider.d.ts +1 -1
  4. package/dist/cjs/driver/default-connection-provider.js +2 -2
  5. package/dist/cjs/driver/dummy-driver.d.ts +8 -0
  6. package/dist/cjs/driver/dummy-driver.js +8 -0
  7. package/dist/cjs/driver/single-connection-provider.d.ts +1 -1
  8. package/dist/cjs/driver/single-connection-provider.js +2 -2
  9. package/dist/cjs/operation-node/alias-node.d.ts +2 -2
  10. package/dist/cjs/operation-node/alias-node.js +1 -2
  11. package/dist/cjs/operation-node/operation-node-transformer.js +1 -0
  12. package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
  13. package/dist/cjs/operation-node/update-query-node.js +11 -2
  14. package/dist/cjs/parser/complex-expression-parser.d.ts +1 -1
  15. package/dist/cjs/parser/reference-parser.js +2 -1
  16. package/dist/cjs/parser/table-parser.js +2 -1
  17. package/dist/cjs/parser/update-set-parser.d.ts +3 -3
  18. package/dist/cjs/parser/with-parser.d.ts +2 -2
  19. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
  20. package/dist/cjs/query-builder/delete-query-builder.d.ts +12 -5
  21. package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
  22. package/dist/cjs/query-builder/expression-builder.js +2 -2
  23. package/dist/cjs/query-builder/function-builder.js +2 -2
  24. package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
  25. package/dist/cjs/query-builder/on-conflict-builder.d.ts +25 -11
  26. package/dist/cjs/query-builder/select-query-builder.d.ts +15 -6
  27. package/dist/cjs/query-builder/select-query-builder.js +5 -5
  28. package/dist/cjs/query-builder/update-query-builder.d.ts +69 -33
  29. package/dist/cjs/query-builder/update-query-builder.js +7 -0
  30. package/dist/cjs/query-builder/where-interface.d.ts +12 -5
  31. package/dist/cjs/query-compiler/default-query-compiler.js +4 -0
  32. package/dist/cjs/query-creator.d.ts +1 -1
  33. package/dist/cjs/query-executor/default-query-executor.d.ts +8 -5
  34. package/dist/cjs/query-executor/default-query-executor.js +2 -2
  35. package/dist/cjs/query-executor/noop-query-executor.d.ts +7 -4
  36. package/dist/cjs/query-executor/noop-query-executor.js +2 -2
  37. package/dist/cjs/query-executor/query-executor-base.d.ts +62 -0
  38. package/dist/cjs/query-executor/query-executor-base.js +46 -0
  39. package/dist/cjs/query-executor/query-executor.d.ts +16 -17
  40. package/dist/cjs/query-executor/query-executor.js +0 -59
  41. package/dist/cjs/raw-builder/raw-builder.d.ts +32 -2
  42. package/dist/cjs/raw-builder/raw-builder.js +5 -26
  43. package/dist/cjs/raw-builder/sql.d.ts +11 -3
  44. package/dist/cjs/raw-builder/sql.js +10 -2
  45. package/dist/cjs/schema/alter-table-builder.d.ts +24 -2
  46. package/dist/cjs/schema/alter-table-builder.js +20 -0
  47. package/dist/cjs/schema/column-definition-builder.d.ts +4 -2
  48. package/dist/cjs/schema/create-table-builder.d.ts +1 -1
  49. package/dist/cjs/schema/create-table-builder.js +1 -1
  50. package/dist/cjs/util/type-utils.d.ts +1 -1
  51. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +19 -0
  52. package/dist/esm/driver/default-connection-provider.d.ts +1 -1
  53. package/dist/esm/driver/default-connection-provider.js +2 -2
  54. package/dist/esm/driver/dummy-driver.d.ts +8 -0
  55. package/dist/esm/driver/dummy-driver.js +8 -0
  56. package/dist/esm/driver/single-connection-provider.d.ts +1 -1
  57. package/dist/esm/driver/single-connection-provider.js +2 -2
  58. package/dist/esm/operation-node/alias-node.d.ts +2 -2
  59. package/dist/esm/operation-node/alias-node.js +1 -2
  60. package/dist/esm/operation-node/operation-node-transformer.js +1 -0
  61. package/dist/esm/operation-node/update-query-node.d.ts +3 -0
  62. package/dist/esm/operation-node/update-query-node.js +11 -2
  63. package/dist/esm/parser/complex-expression-parser.d.ts +1 -1
  64. package/dist/esm/parser/reference-parser.js +2 -1
  65. package/dist/esm/parser/table-parser.js +2 -1
  66. package/dist/esm/parser/update-set-parser.d.ts +3 -3
  67. package/dist/esm/parser/with-parser.d.ts +2 -2
  68. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
  69. package/dist/esm/query-builder/delete-query-builder.d.ts +12 -5
  70. package/dist/esm/query-builder/expression-builder.d.ts +1 -1
  71. package/dist/esm/query-builder/expression-builder.js +1 -1
  72. package/dist/esm/query-builder/function-builder.js +1 -1
  73. package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
  74. package/dist/esm/query-builder/on-conflict-builder.d.ts +25 -11
  75. package/dist/esm/query-builder/select-query-builder.d.ts +15 -6
  76. package/dist/esm/query-builder/select-query-builder.js +5 -5
  77. package/dist/esm/query-builder/update-query-builder.d.ts +69 -33
  78. package/dist/esm/query-builder/update-query-builder.js +7 -0
  79. package/dist/esm/query-builder/where-interface.d.ts +12 -5
  80. package/dist/esm/query-compiler/default-query-compiler.js +4 -0
  81. package/dist/esm/query-creator.d.ts +1 -1
  82. package/dist/esm/query-executor/default-query-executor.d.ts +8 -5
  83. package/dist/esm/query-executor/default-query-executor.js +2 -2
  84. package/dist/esm/query-executor/noop-query-executor.d.ts +7 -4
  85. package/dist/esm/query-executor/noop-query-executor.js +2 -2
  86. package/dist/esm/query-executor/query-executor-base.d.ts +62 -0
  87. package/dist/esm/query-executor/query-executor-base.js +43 -0
  88. package/dist/esm/query-executor/query-executor.d.ts +16 -17
  89. package/dist/esm/query-executor/query-executor.js +1 -57
  90. package/dist/esm/raw-builder/raw-builder.d.ts +32 -2
  91. package/dist/esm/raw-builder/raw-builder.js +5 -26
  92. package/dist/esm/raw-builder/sql.d.ts +11 -3
  93. package/dist/esm/raw-builder/sql.js +10 -2
  94. package/dist/esm/schema/alter-table-builder.d.ts +24 -2
  95. package/dist/esm/schema/alter-table-builder.js +20 -0
  96. package/dist/esm/schema/column-definition-builder.d.ts +4 -2
  97. package/dist/esm/schema/create-table-builder.d.ts +1 -1
  98. package/dist/esm/schema/create-table-builder.js +1 -1
  99. package/dist/esm/util/type-utils.d.ts +1 -1
  100. package/package.json +9 -9
@@ -19,7 +19,7 @@ import { JoinInterface } from './join-interface.js';
19
19
  import { ReturningInterface } from './returning-interface.js';
20
20
  import { NoResultErrorConstructor } from './no-result-error.js';
21
21
  import { Selectable } from '../util/column-type.js';
22
- export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, JoinInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
22
+ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, JoinInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
23
23
  #private;
24
24
  constructor(props: UpdateQueryBuilderProps);
25
25
  /**
@@ -46,7 +46,13 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
46
46
  * ```
47
47
  *
48
48
  * Operator can be any supported operator or if the typings don't support it
49
- * you can always use `sql\`your operator\``.
49
+ * you can always use
50
+ *
51
+ * ```ts
52
+ * sql`your operator`
53
+ * ```
54
+ *
55
+ * The next example uses the `>` operator:
50
56
  *
51
57
  * ```ts
52
58
  * const persons = await db
@@ -62,8 +68,8 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
62
68
  * select * from "person" where "id" > $1
63
69
  * ```
64
70
  *
65
- * The `where` methods don't change the type of the query. You can add
66
- * conditional `where`s easily by doing something like this:
71
+ * `where` methods don't change the type of the query. You can add
72
+ * conditional statements easily by doing something like this:
67
73
  *
68
74
  * ```ts
69
75
  * let query = db
@@ -71,6 +77,8 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
71
77
  * .selectAll()
72
78
  *
73
79
  * if (firstName) {
80
+ * // The query builder is immutable. Remember to reassign
81
+ * // the result back to the query variable.
74
82
  * query = query.where('first_name', '=', firstName)
75
83
  * }
76
84
  *
@@ -78,8 +86,7 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
78
86
  * ```
79
87
  *
80
88
  * This is true for basically all methods execpt the `select` and
81
- * `returning` methods, which DO change the return type of the
82
- * query.
89
+ * `returning`, that __do__ change the return type of the query.
83
90
  *
84
91
  * Both the first and third argument can also be subqueries.
85
92
  * A subquery is defined by passing a function and calling
@@ -199,9 +206,9 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
199
206
  * .execute()
200
207
  * ```
201
208
  */
202
- where<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: FilterValueExpressionOrList<DB, TB, RE>): UpdateQueryBuilder<DB, TB, O>;
203
- where(grouper: WhereGrouper<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
204
- where(raw: AnyRawBuilder): UpdateQueryBuilder<DB, TB, O>;
209
+ where<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: FilterValueExpressionOrList<DB, TB, RE>): UpdateQueryBuilder<DB, UT, TB, O>;
210
+ where(grouper: WhereGrouper<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
211
+ where(raw: AnyRawBuilder): UpdateQueryBuilder<DB, UT, TB, O>;
205
212
  /**
206
213
  * Adds a `where` clause where both sides of the operator are references
207
214
  * to columns.
@@ -253,7 +260,7 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
253
260
  * ) as "pet_name"
254
261
  * from "person"
255
262
  */
256
- whereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
263
+ whereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
257
264
  /**
258
265
  * Adds an `or where` clause to the query. Otherwise works just like {@link where}.
259
266
  *
@@ -317,15 +324,15 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
317
324
  * select * from "person" (where "id" = 1 or "id" = 2)
318
325
  * ```
319
326
  */
320
- orWhere<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: FilterValueExpressionOrList<DB, TB, RE>): UpdateQueryBuilder<DB, TB, O>;
321
- orWhere(grouper: WhereGrouper<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
322
- orWhere(raw: AnyRawBuilder): UpdateQueryBuilder<DB, TB, O>;
327
+ orWhere<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: FilterValueExpressionOrList<DB, TB, RE>): UpdateQueryBuilder<DB, UT, TB, O>;
328
+ orWhere(grouper: WhereGrouper<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
329
+ orWhere(raw: AnyRawBuilder): UpdateQueryBuilder<DB, UT, TB, O>;
323
330
  /**
324
331
  * Adds an `or where` clause to the query. Otherwise works just like {@link whereRef}.
325
332
  *
326
333
  * Also see {@link orWhere} and {@link where}.
327
334
  */
328
- orWhereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
335
+ orWhereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
329
336
  /**
330
337
  * Adds a `where exists` clause to the query.
331
338
  *
@@ -386,19 +393,48 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
386
393
  * )
387
394
  * ```
388
395
  */
389
- whereExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
396
+ whereExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
390
397
  /**
391
398
  * Just like {@link whereExists} but creates a `not exists` clause.
392
399
  */
393
- whereNotExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
400
+ whereNotExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
394
401
  /**
395
402
  * Just like {@link whereExists} but creates a `or exists` clause.
396
403
  */
397
- orWhereExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
404
+ orWhereExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
398
405
  /**
399
406
  * Just like {@link whereExists} but creates a `or not exists` clause.
400
407
  */
401
- orWhereNotExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
408
+ orWhereNotExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, UT, TB, O>;
409
+ /**
410
+ * Adds a from clause to the update query.
411
+ *
412
+ * This is supported only on some databases like PostgreSQL.
413
+ *
414
+ * The API is the same as {@link QueryCreator.selectFrom}.
415
+ *
416
+ * ### Examples
417
+ *
418
+ * ```ts
419
+ * db.updateTable('person')
420
+ * .from('pet')
421
+ * .set({
422
+ * first_name: (eb) => eb.ref('pet.name')
423
+ * })
424
+ * .whereRef('pet.owner_id', '=', 'person.id')
425
+ * ```
426
+ *
427
+ * The generated SQL (PostgreSQL):
428
+ *
429
+ * ```sql
430
+ * update "person"
431
+ * set "first_name" = "pet"."name"
432
+ * from "pet"
433
+ * where "pet"."owner_id" = "person"."id"
434
+ * ```
435
+ */
436
+ from<TE extends TableExpression<DB, TB>>(table: TE): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
437
+ from<TE extends TableExpression<DB, TB>>(table: TE[]): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
402
438
  /**
403
439
  * Joins another table to the query using an inner join.
404
440
  *
@@ -508,23 +544,23 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
508
544
  * on "doggos"."owner_id" = "person"."id"
509
545
  * ```
510
546
  */
511
- innerJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
512
- innerJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
547
+ innerJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
548
+ innerJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
513
549
  /**
514
550
  * Just like {@link innerJoin} but adds a left join nstead of an inner join.
515
551
  */
516
- leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
517
- leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
552
+ leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
553
+ leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
518
554
  /**
519
555
  * Just like {@link innerJoin} but adds a right join instead of an inner join.
520
556
  */
521
- rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
522
- rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
557
+ rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
558
+ rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
523
559
  /**
524
560
  * Just like {@link innerJoin} but adds a full join instead of an inner join.
525
561
  */
526
- fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
527
- fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
562
+ fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
563
+ fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, UT, TableExpressionTables<DB, TB, TE>, O>;
528
564
  /**
529
565
  * Sets the values to update for an {@link Kysely.updateTable | update} query.
530
566
  *
@@ -612,7 +648,7 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
612
648
  * where "id" = $4
613
649
  * ```
614
650
  */
615
- set(row: MutationObject<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
651
+ set(row: MutationObject<DB, TB, UT>): UpdateQueryBuilder<DB, UT, TB, O>;
616
652
  /**
617
653
  * Allows you to return data from modified rows.
618
654
  *
@@ -672,13 +708,13 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
672
708
  * .executeTakeFirst()
673
709
  * ```
674
710
  */
675
- returning<SE extends SelectExpression<DB, TB>>(selections: ReadonlyArray<SE>): UpdateQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>;
676
- returning<SE extends SelectExpression<DB, TB>>(selection: SE): UpdateQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>;
711
+ returning<SE extends SelectExpression<DB, TB>>(selections: ReadonlyArray<SE>): UpdateQueryBuilder<DB, UT, TB, ReturningRow<DB, TB, O, SE>>;
712
+ returning<SE extends SelectExpression<DB, TB>>(selection: SE): UpdateQueryBuilder<DB, UT, TB, ReturningRow<DB, TB, O, SE>>;
677
713
  /**
678
714
  * Adds a `returning *` to an insert/update/delete query on databases
679
715
  * that support `returning` such as PostgreSQL.
680
716
  */
681
- returningAll(): UpdateQueryBuilder<DB, TB, Selectable<DB[TB]>>;
717
+ returningAll(): UpdateQueryBuilder<DB, UT, TB, Selectable<DB[TB]>>;
682
718
  /**
683
719
  * Simply calls the given function passing `this` as the only argument.
684
720
  *
@@ -738,18 +774,18 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
738
774
  * }
739
775
  * ```
740
776
  */
741
- if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<DB, TB, O2>): UpdateQueryBuilder<DB, TB, O2 extends UpdateResult ? UpdateResult : O extends UpdateResult ? Partial<O2> : MergePartial<O, O2>>;
777
+ if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<DB, UT, TB, O2>): UpdateQueryBuilder<DB, UT, TB, O2 extends UpdateResult ? UpdateResult : O extends UpdateResult ? Partial<O2> : MergePartial<O, O2>>;
742
778
  /**
743
779
  * Change the output type of the query.
744
780
  *
745
781
  * You should only use this method as the last resort if the types
746
782
  * don't support your use case.
747
783
  */
748
- castTo<T>(): UpdateQueryBuilder<DB, TB, T>;
784
+ castTo<T>(): UpdateQueryBuilder<DB, UT, TB, T>;
749
785
  /**
750
786
  * Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
751
787
  */
752
- withPlugin(plugin: KyselyPlugin): UpdateQueryBuilder<DB, TB, O>;
788
+ withPlugin(plugin: KyselyPlugin): UpdateQueryBuilder<DB, UT, TB, O>;
753
789
  toOperationNode(): UpdateQueryNode;
754
790
  compile(): CompiledQuery;
755
791
  /**
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UpdateQueryBuilder = void 0;
4
4
  const join_parser_js_1 = require("../parser/join-parser.js");
5
+ const table_parser_js_1 = require("../parser/table-parser.js");
5
6
  const select_parser_js_1 = require("../parser/select-parser.js");
6
7
  const filter_parser_js_1 = require("../parser/filter-parser.js");
7
8
  const query_node_js_1 = require("../operation-node/query-node.js");
@@ -64,6 +65,12 @@ class UpdateQueryBuilder {
64
65
  queryNode: query_node_js_1.QueryNode.cloneWithOrWhere(this.#props.queryNode, (0, filter_parser_js_1.parseNotExistFilter)(arg)),
65
66
  });
66
67
  }
68
+ from(from) {
69
+ return new UpdateQueryBuilder({
70
+ ...this.#props,
71
+ queryNode: update_query_node_js_1.UpdateQueryNode.cloneWithFromItems(this.#props.queryNode, (0, table_parser_js_1.parseTableExpressionOrList)(from)),
72
+ });
73
+ }
67
74
  innerJoin(...args) {
68
75
  return new UpdateQueryBuilder({
69
76
  ...this.#props,
@@ -26,7 +26,13 @@ export interface WhereInterface<DB, TB extends keyof DB> {
26
26
  * ```
27
27
  *
28
28
  * Operator can be any supported operator or if the typings don't support it
29
- * you can always use `sql\`your operator\``.
29
+ * you can always use
30
+ *
31
+ * ```ts
32
+ * sql`your operator`
33
+ * ```
34
+ *
35
+ * The next example uses the `>` operator:
30
36
  *
31
37
  * ```ts
32
38
  * const persons = await db
@@ -42,8 +48,8 @@ export interface WhereInterface<DB, TB extends keyof DB> {
42
48
  * select * from "person" where "id" > $1
43
49
  * ```
44
50
  *
45
- * The `where` methods don't change the type of the query. You can add
46
- * conditional `where`s easily by doing something like this:
51
+ * `where` methods don't change the type of the query. You can add
52
+ * conditional statements easily by doing something like this:
47
53
  *
48
54
  * ```ts
49
55
  * let query = db
@@ -51,6 +57,8 @@ export interface WhereInterface<DB, TB extends keyof DB> {
51
57
  * .selectAll()
52
58
  *
53
59
  * if (firstName) {
60
+ * // The query builder is immutable. Remember to reassign
61
+ * // the result back to the query variable.
54
62
  * query = query.where('first_name', '=', firstName)
55
63
  * }
56
64
  *
@@ -58,8 +66,7 @@ export interface WhereInterface<DB, TB extends keyof DB> {
58
66
  * ```
59
67
  *
60
68
  * This is true for basically all methods execpt the `select` and
61
- * `returning` methods, which DO change the return type of the
62
- * query.
69
+ * `returning`, that __do__ change the return type of the query.
63
70
  *
64
71
  * Both the first and third argument can also be subqueries.
65
72
  * A subquery is defined by passing a function and calling
@@ -427,6 +427,10 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
427
427
  if (node.updates) {
428
428
  this.compileList(node.updates);
429
429
  }
430
+ if (node.from) {
431
+ this.append(' ');
432
+ this.visitNode(node.from);
433
+ }
430
434
  if (node.joins) {
431
435
  this.append(' ');
432
436
  this.compileList(node.joins, ' ');
@@ -203,7 +203,7 @@ export declare class QueryCreator<DB> {
203
203
  * console.log(result.numUpdatedRows)
204
204
  * ```
205
205
  */
206
- updateTable<TR extends TableReference<DB>>(table: TR): UpdateQueryBuilder<TableExpressionDatabase<DB, TR>, TableExpressionTables<DB, never, TR>, UpdateResult>;
206
+ updateTable<TR extends TableReference<DB>>(table: TR): UpdateQueryBuilder<TableExpressionDatabase<DB, TR>, TableExpressionTables<DB, never, TR>, TableExpressionTables<DB, never, TR>, UpdateResult>;
207
207
  /**
208
208
  * Creates a `with` query (Common Table Expression).
209
209
  *
@@ -3,11 +3,14 @@ import { DatabaseConnection } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode, QueryCompiler } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
- import { QueryExecutor } from './query-executor.js';
7
- import { DialectAdapter } from '../index-nodeless.js';
8
- export declare class DefaultQueryExecutor extends QueryExecutor {
6
+ import { QueryExecutorBase } from './query-executor-base.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
+ export declare class DefaultQueryExecutor extends QueryExecutorBase {
9
9
  #private;
10
10
  constructor(compiler: QueryCompiler, adapter: DialectAdapter, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
11
+ /**
12
+ * Returns the adapter for the current dialect.
13
+ */
11
14
  get adapter(): DialectAdapter;
12
15
  /**
13
16
  * Compiles the transformed query into SQL. You usually want to pass
@@ -38,9 +41,9 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
38
41
  /**
39
42
  * Returns a copy of this executor with a new connection provider.
40
43
  */
41
- withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
44
+ withConnectionProvider(connectionProvider: ConnectionProvider): DefaultQueryExecutor;
42
45
  /**
43
46
  * Returns a copy of this executor without any plugins.
44
47
  */
45
- withoutPlugins(): QueryExecutor;
48
+ withoutPlugins(): DefaultQueryExecutor;
46
49
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultQueryExecutor = void 0;
4
- const query_executor_js_1 = require("./query-executor.js");
5
- class DefaultQueryExecutor extends query_executor_js_1.QueryExecutor {
4
+ const query_executor_base_js_1 = require("./query-executor-base.js");
5
+ class DefaultQueryExecutor extends query_executor_base_js_1.QueryExecutorBase {
6
6
  #compiler;
7
7
  #adapter;
8
8
  #connectionProvider;
@@ -1,13 +1,16 @@
1
1
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
2
- import { QueryExecutor } from './query-executor.js';
3
2
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
4
3
  import { DialectAdapter } from '../dialect/dialect-adapter.js';
4
+ import { QueryExecutorBase } from './query-executor-base.js';
5
5
  /**
6
6
  * A {@link QueryExecutor} subclass that can be used when you don't
7
7
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
8
8
  * other needed things to actually execute queries.
9
9
  */
10
- export declare class NoopQueryExecutor extends QueryExecutor {
10
+ export declare class NoopQueryExecutor extends QueryExecutorBase {
11
+ /**
12
+ * Returns the adapter for the current dialect.
13
+ */
11
14
  get adapter(): DialectAdapter;
12
15
  /**
13
16
  * Compiles the transformed query into SQL. You usually want to pass
@@ -23,7 +26,7 @@ export declare class NoopQueryExecutor extends QueryExecutor {
23
26
  /**
24
27
  * Returns a copy of this executor with a new connection provider.
25
28
  */
26
- withConnectionProvider(): QueryExecutor;
29
+ withConnectionProvider(): NoopQueryExecutor;
27
30
  /**
28
31
  * Returns a copy of this executor with a plugin added as the
29
32
  * last plugin.
@@ -42,6 +45,6 @@ export declare class NoopQueryExecutor extends QueryExecutor {
42
45
  /**
43
46
  * Returns a copy of this executor without any plugins.
44
47
  */
45
- withoutPlugins(): QueryExecutor;
48
+ withoutPlugins(): NoopQueryExecutor;
46
49
  }
47
50
  export declare const NOOP_QUERY_EXECUTOR: NoopQueryExecutor;
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NOOP_QUERY_EXECUTOR = exports.NoopQueryExecutor = void 0;
4
- const query_executor_js_1 = require("./query-executor.js");
4
+ const query_executor_base_js_1 = require("./query-executor-base.js");
5
5
  /**
6
6
  * A {@link QueryExecutor} subclass that can be used when you don't
7
7
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
8
8
  * other needed things to actually execute queries.
9
9
  */
10
- class NoopQueryExecutor extends query_executor_js_1.QueryExecutor {
10
+ class NoopQueryExecutor extends query_executor_base_js_1.QueryExecutorBase {
11
11
  get adapter() {
12
12
  throw new Error('this query cannot be compiled to SQL');
13
13
  }
@@ -0,0 +1,62 @@
1
+ import { ConnectionProvider } from '../driver/connection-provider.js';
2
+ import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
3
+ import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
+ import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
+ import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
+ import { QueryId } from '../util/query-id.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
+ import { QueryExecutor } from './query-executor.js';
9
+ export declare abstract class QueryExecutorBase implements QueryExecutor {
10
+ #private;
11
+ constructor(plugins?: ReadonlyArray<KyselyPlugin>);
12
+ abstract get adapter(): DialectAdapter;
13
+ /**
14
+ * Returns all installed plugins.
15
+ */
16
+ get plugins(): ReadonlyArray<KyselyPlugin>;
17
+ /**
18
+ * Given the query the user has built (expressed as an operation node tree)
19
+ * this method runs it through all plugins' `transformQuery` methods and
20
+ * returns the result.
21
+ */
22
+ transformQuery<T extends RootOperationNode>(node: T, queryId: QueryId): T;
23
+ /**
24
+ * Compiles the transformed query into SQL. You usually want to pass
25
+ * the output of {@link transformQuery} into this method but you can
26
+ * compile any query using this method.
27
+ */
28
+ abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
29
+ /**
30
+ * Provides a connection for the callback and takes care of disposing
31
+ * the connection after the callback has been run.
32
+ */
33
+ abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
34
+ /**
35
+ * Executes a compiled query and runs the result through all plugins'
36
+ * `transformResult` method.
37
+ */
38
+ executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
39
+ /**
40
+ * Returns a copy of this executor with a new connection provider.
41
+ */
42
+ abstract withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutorBase;
43
+ /**
44
+ * Returns a copy of this executor with a plugin added as the
45
+ * last plugin.
46
+ */
47
+ abstract withPlugin(plugin: KyselyPlugin): QueryExecutorBase;
48
+ /**
49
+ * Returns a copy of this executor with a list of plugins added
50
+ * as the last plugins.
51
+ */
52
+ abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutorBase;
53
+ /**
54
+ * Returns a copy of this executor with a plugin added as the
55
+ * first plugin.
56
+ */
57
+ abstract withPluginAtFront(plugin: KyselyPlugin): QueryExecutorBase;
58
+ /**
59
+ * Returns a copy of this executor without any plugins.
60
+ */
61
+ abstract withoutPlugins(): QueryExecutorBase;
62
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryExecutorBase = void 0;
4
+ const object_utils_js_1 = require("../util/object-utils.js");
5
+ const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
6
+ class QueryExecutorBase {
7
+ #plugins;
8
+ constructor(plugins = NO_PLUGINS) {
9
+ this.#plugins = plugins;
10
+ }
11
+ get plugins() {
12
+ return this.#plugins;
13
+ }
14
+ transformQuery(node, queryId) {
15
+ for (const plugin of this.#plugins) {
16
+ const transformedNode = plugin.transformQuery({ node, queryId });
17
+ // We need to do a runtime check here. There is no good way
18
+ // to write types that enforce this constraint.
19
+ if (transformedNode.kind === node.kind) {
20
+ node = transformedNode;
21
+ }
22
+ else {
23
+ throw new Error([
24
+ `KyselyPlugin.transformQuery must return a node`,
25
+ `of the same kind that was given to it.`,
26
+ `The plugin was given a ${node.kind}`,
27
+ `but it returned a ${transformedNode.kind}`,
28
+ ].join(' '));
29
+ }
30
+ }
31
+ return node;
32
+ }
33
+ async executeQuery(compiledQuery, queryId) {
34
+ return await this.provideConnection(async (connection) => {
35
+ const result = await connection.executeQuery(compiledQuery);
36
+ return this.#transformResult(result, queryId);
37
+ });
38
+ }
39
+ async #transformResult(result, queryId) {
40
+ for (const plugin of this.#plugins) {
41
+ result = await plugin.transformResult({ result, queryId });
42
+ }
43
+ return result;
44
+ }
45
+ }
46
+ exports.QueryExecutorBase = QueryExecutorBase;
@@ -1,20 +1,24 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
2
+ import { QueryResult } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
6
  import { QueryId } from '../util/query-id.js';
7
7
  import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
8
  /**
9
- * This class abstracts away the details of how to compile a query into SQL
9
+ * This interface abstracts away the details of how to compile a query into SQL
10
10
  * and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
11
11
  * and other classes that execute queries can just pass around and instance of
12
12
  * `QueryExecutor`.
13
13
  */
14
- export declare abstract class QueryExecutor implements ConnectionProvider {
15
- #private;
16
- constructor(plugins?: KyselyPlugin[]);
17
- abstract get adapter(): DialectAdapter;
14
+ export interface QueryExecutor extends ConnectionProvider {
15
+ /**
16
+ * Returns the adapter for the current dialect.
17
+ */
18
+ get adapter(): DialectAdapter;
19
+ /**
20
+ * Returns all installed plugins.
21
+ */
18
22
  get plugins(): ReadonlyArray<KyselyPlugin>;
19
23
  /**
20
24
  * Given the query the user has built (expressed as an operation node tree)
@@ -27,12 +31,7 @@ export declare abstract class QueryExecutor implements ConnectionProvider {
27
31
  * the output of {@link transformQuery} into this method but you can
28
32
  * compile any query using this method.
29
33
  */
30
- abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
31
- /**
32
- * Provides a connection for the callback and takes care of disposing
33
- * the connection after the callback has been run.
34
- */
35
- abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
34
+ compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
36
35
  /**
37
36
  * Executes a compiled query and runs the result through all plugins'
38
37
  * `transformResult` method.
@@ -41,24 +40,24 @@ export declare abstract class QueryExecutor implements ConnectionProvider {
41
40
  /**
42
41
  * Returns a copy of this executor with a new connection provider.
43
42
  */
44
- abstract withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
43
+ withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
45
44
  /**
46
45
  * Returns a copy of this executor with a plugin added as the
47
46
  * last plugin.
48
47
  */
49
- abstract withPlugin(plugin: KyselyPlugin): QueryExecutor;
48
+ withPlugin(plugin: KyselyPlugin): QueryExecutor;
50
49
  /**
51
50
  * Returns a copy of this executor with a list of plugins added
52
51
  * as the last plugins.
53
52
  */
54
- abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
53
+ withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
55
54
  /**
56
55
  * Returns a copy of this executor with a plugin added as the
57
56
  * first plugin.
58
57
  */
59
- abstract withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
58
+ withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
60
59
  /**
61
60
  * Returns a copy of this executor without any plugins.
62
61
  */
63
- abstract withoutPlugins(): QueryExecutor;
62
+ withoutPlugins(): QueryExecutor;
64
63
  }