kysely 0.16.6 → 0.16.10

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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +139 -14
  3. package/dist/cjs/dialect/mysql/mysql-dialect.js +2 -1
  4. package/dist/cjs/dialect/mysql/mysql-driver.js +1 -1
  5. package/dist/cjs/dialect/postgres/postgres-dialect.js +2 -1
  6. package/dist/cjs/dialect/postgres/postgres-driver.js +1 -1
  7. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +2 -1
  8. package/dist/cjs/dialect/sqlite/sqlite-driver.js +1 -1
  9. package/dist/cjs/driver/dummy-driver.d.ts +28 -0
  10. package/dist/cjs/driver/dummy-driver.js +31 -3
  11. package/dist/cjs/index-nodeless.d.ts +1 -0
  12. package/dist/cjs/index-nodeless.js +1 -0
  13. package/dist/cjs/kysely.d.ts +8 -6
  14. package/dist/cjs/kysely.js +10 -8
  15. package/dist/cjs/migration/migrator.d.ts +22 -0
  16. package/dist/cjs/migration/migrator.js +23 -1
  17. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
  18. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +19 -0
  19. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
  20. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +42 -0
  21. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
  22. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
  23. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
  24. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
  25. package/dist/cjs/query-builder/delete-query-builder.d.ts +52 -12
  26. package/dist/cjs/query-builder/delete-query-builder.js +56 -8
  27. package/dist/cjs/query-builder/function-builder.d.ts +29 -5
  28. package/dist/cjs/query-builder/function-builder.js +31 -7
  29. package/dist/cjs/query-builder/insert-query-builder.d.ts +54 -14
  30. package/dist/cjs/query-builder/insert-query-builder.js +58 -10
  31. package/dist/cjs/query-builder/returning-interface.d.ts +1 -3
  32. package/dist/cjs/query-builder/select-query-builder.d.ts +65 -24
  33. package/dist/cjs/query-builder/select-query-builder.js +64 -15
  34. package/dist/cjs/query-builder/update-query-builder.d.ts +53 -12
  35. package/dist/cjs/query-builder/update-query-builder.js +57 -8
  36. package/dist/cjs/query-compiler/default-query-compiler.js +8 -7
  37. package/dist/cjs/query-creator.d.ts +2 -2
  38. package/dist/cjs/query-creator.js +2 -2
  39. package/dist/cjs/query-executor/query-executor.js +3 -2
  40. package/dist/cjs/raw-builder/raw-builder.d.ts +2 -2
  41. package/dist/cjs/raw-builder/raw-builder.js +8 -1
  42. package/dist/cjs/schema/alter-table-builder.js +3 -3
  43. package/dist/cjs/schema/create-table-builder.js +2 -2
  44. package/dist/cjs/schema/create-view-builder.js +1 -1
  45. package/dist/cjs/util/object-utils.d.ts +1 -0
  46. package/dist/cjs/util/object-utils.js +50 -1
  47. package/dist/cjs/util/type-utils.d.ts +21 -0
  48. package/dist/esm/dialect/mysql/mysql-dialect.js +2 -1
  49. package/dist/esm/dialect/mysql/mysql-driver.js +2 -2
  50. package/dist/esm/dialect/postgres/postgres-dialect.js +2 -1
  51. package/dist/esm/dialect/postgres/postgres-driver.js +2 -2
  52. package/dist/esm/dialect/sqlite/sqlite-dialect.js +2 -1
  53. package/dist/esm/dialect/sqlite/sqlite-driver.js +2 -2
  54. package/dist/esm/driver/dummy-driver.d.ts +28 -0
  55. package/dist/esm/driver/dummy-driver.js +31 -3
  56. package/dist/esm/index-nodeless.d.ts +1 -0
  57. package/dist/esm/index-nodeless.js +1 -0
  58. package/dist/esm/kysely.d.ts +8 -6
  59. package/dist/esm/kysely.js +10 -8
  60. package/dist/esm/migration/migrator.d.ts +22 -0
  61. package/dist/esm/migration/migrator.js +23 -1
  62. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
  63. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +16 -0
  64. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
  65. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +39 -0
  66. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
  67. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
  68. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
  69. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
  70. package/dist/esm/query-builder/delete-query-builder.d.ts +52 -12
  71. package/dist/esm/query-builder/delete-query-builder.js +56 -8
  72. package/dist/esm/query-builder/function-builder.d.ts +29 -5
  73. package/dist/esm/query-builder/function-builder.js +31 -7
  74. package/dist/esm/query-builder/insert-query-builder.d.ts +54 -14
  75. package/dist/esm/query-builder/insert-query-builder.js +58 -10
  76. package/dist/esm/query-builder/returning-interface.d.ts +1 -3
  77. package/dist/esm/query-builder/select-query-builder.d.ts +65 -24
  78. package/dist/esm/query-builder/select-query-builder.js +64 -15
  79. package/dist/esm/query-builder/update-query-builder.d.ts +53 -12
  80. package/dist/esm/query-builder/update-query-builder.js +57 -8
  81. package/dist/esm/query-compiler/default-query-compiler.js +8 -7
  82. package/dist/esm/query-creator.d.ts +2 -2
  83. package/dist/esm/query-creator.js +2 -2
  84. package/dist/esm/query-executor/query-executor.js +3 -2
  85. package/dist/esm/raw-builder/raw-builder.d.ts +2 -2
  86. package/dist/esm/raw-builder/raw-builder.js +8 -1
  87. package/dist/esm/schema/alter-table-builder.js +3 -3
  88. package/dist/esm/schema/create-table-builder.js +2 -2
  89. package/dist/esm/schema/create-view-builder.js +1 -1
  90. package/dist/esm/util/object-utils.d.ts +1 -0
  91. package/dist/esm/util/object-utils.js +48 -0
  92. package/dist/esm/util/type-utils.d.ts +21 -0
  93. package/package.json +4 -2
@@ -7,7 +7,7 @@ import { SelectExpression, QueryBuilderWithSelection, SelectAllQueryBuilder } fr
7
7
  import { ExistsExpression, FilterOperator, FilterValueExpressionOrList, WhereGrouper, HavingGrouper } from '../parser/filter-parser.js';
8
8
  import { ReferenceExpression } from '../parser/reference-parser.js';
9
9
  import { SelectQueryNode } from '../operation-node/select-query-node.js';
10
- import { AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
10
+ import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
11
11
  import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
12
12
  import { Compilable } from '../util/compilable.js';
13
13
  import { QueryExecutor } from '../query-executor/query-executor.js';
@@ -456,7 +456,10 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
456
456
  * same as calling `select(['id', 'first_name']).
457
457
  *
458
458
  * To select all columns of the query or specific tables see the
459
- * {@link SelectQueryBuilder.selectAll | selectAll} method.
459
+ * {@link selectAll} method.
460
+ *
461
+ * See the {@link if} method if you are looking for a way to add selections
462
+ * based on a runtime condition.
460
463
  *
461
464
  * ### Examples
462
465
  *
@@ -971,14 +974,12 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
971
974
  * `groupBy` also accepts an array:
972
975
  *
973
976
  * ```ts
974
- * const { raw } = db
975
- *
976
977
  * await db
977
978
  * .selectFrom('person')
978
979
  * .select([
979
980
  * 'first_name',
980
981
  * 'last_name',
981
- * raw('max(id)').as('max_id')
982
+ * db.raw('max(id)').as('max_id')
982
983
  * ])
983
984
  * .groupBy([
984
985
  * 'first_name',
@@ -999,17 +1000,15 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
999
1000
  * raw expressions:
1000
1001
  *
1001
1002
  * ```ts
1002
- * const { raw } = db
1003
- *
1004
1003
  * await db
1005
1004
  * .selectFrom('person')
1006
1005
  * .select([
1007
1006
  * 'first_name',
1008
1007
  * 'last_name',
1009
- * raw('max(id)').as('max_id')
1008
+ * db.raw('max(id)').as('max_id')
1010
1009
  * ])
1011
1010
  * .groupBy([
1012
- * raw('concat(first_name, last_name)'),
1011
+ * db.raw('concat(first_name, last_name)'),
1013
1012
  * (qb) => qb.selectFrom('pet').select('id').limit(1)
1014
1013
  * ])
1015
1014
  * .execute()
@@ -1116,27 +1115,15 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1116
1115
  /**
1117
1116
  * Simply calls the given function passing `this` as the only argument.
1118
1117
  *
1119
- * This method can be useful when adding optional method calls:
1118
+ * If you want to conditionally call a method on `this`, see
1119
+ * the {@link if} method.
1120
1120
  *
1121
1121
  * ### Examples
1122
1122
  *
1123
- * ```ts
1124
- * db.selectFrom('person')
1125
- * .selectAll()
1126
- * .call((qb) => {
1127
- * if (something) {
1128
- * return qb.where('something', '=', something)
1129
- * } else {
1130
- * return qb.where('somethingElse', '=', somethingElse)
1131
- * }
1132
- * })
1133
- * .execute()
1134
- * ```
1135
- *
1136
1123
  * The next example uses a helper funtion `log` to log a query:
1137
1124
  *
1138
1125
  * ```ts
1139
- * function log<T extends AnyQueryBuilder>(qb: T): T {
1126
+ * function log<T extends Compilable>(qb: T): T {
1140
1127
  * console.log(qb.compile())
1141
1128
  * return qb
1142
1129
  * }
@@ -1148,6 +1135,57 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1148
1135
  * ```
1149
1136
  */
1150
1137
  call<T>(func: (qb: this) => T): T;
1138
+ /**
1139
+ * Call `func(this)` if `condition` is true.
1140
+ *
1141
+ * This method is especially handy with optional selects. Any `select` or `selectAll`
1142
+ * method calls add columns as optional fields to the output type when called inside
1143
+ * the `func` callback. This is because we can't know if those selections were actually
1144
+ * made before running the code.
1145
+ *
1146
+ * Also see [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/conditional-selects.md)
1147
+ *
1148
+ * ### Examples
1149
+ *
1150
+ * ```ts
1151
+ * async function getPerson(id: number, withLastName: boolean) {
1152
+ * return await db
1153
+ * .selectFrom('person')
1154
+ * .select(['id', 'first_name'])
1155
+ * .if(withLastName, (qb) => qb.select('last_name'))
1156
+ * .where('id', '=', id)
1157
+ * .executeTakeFirstOrThrow()
1158
+ * }
1159
+ * ```
1160
+ *
1161
+ * Any selections added inside the `if` callback will be added as optional fields to the
1162
+ * output type since we can't know if the selections were actually made before running
1163
+ * the code. In the example above the return type of the `getPerson` function is:
1164
+ *
1165
+ * ```ts
1166
+ * {
1167
+ * id: number
1168
+ * first_name: string
1169
+ * last_name?: string
1170
+ * }
1171
+ * ```
1172
+ *
1173
+ * You can also call any other methods inside the callback:
1174
+ *
1175
+ * ```ts
1176
+ * const { count } = db.fn
1177
+ *
1178
+ * db.selectFrom('person')
1179
+ * .select('person.id')
1180
+ * .if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
1181
+ * .if(filterByPetCount, (qb) => qb
1182
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
1183
+ * .having(count('pet.id'), '>', petCountLimit)
1184
+ * .groupBy('person.id')
1185
+ * )
1186
+ * ```
1187
+ */
1188
+ if<O2 extends O>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O2>): SelectQueryBuilder<DB, TB, MergePartial<O, O2>>;
1151
1189
  /**
1152
1190
  * Gives an alias for the query. This method is only useful for sub queries.
1153
1191
  *
@@ -1173,6 +1211,9 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1173
1211
  * don't support your use case.
1174
1212
  */
1175
1213
  castTo<T>(): SelectQueryBuilder<DB, TB, T>;
1214
+ /**
1215
+ * Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
1216
+ */
1176
1217
  withPlugin(plugin: KyselyPlugin): SelectQueryBuilder<DB, TB, O>;
1177
1218
  toOperationNode(): SelectQueryNode;
1178
1219
  compile(): CompiledQuery;
@@ -427,27 +427,15 @@ class SelectQueryBuilder {
427
427
  /**
428
428
  * Simply calls the given function passing `this` as the only argument.
429
429
  *
430
- * This method can be useful when adding optional method calls:
430
+ * If you want to conditionally call a method on `this`, see
431
+ * the {@link if} method.
431
432
  *
432
433
  * ### Examples
433
434
  *
434
- * ```ts
435
- * db.selectFrom('person')
436
- * .selectAll()
437
- * .call((qb) => {
438
- * if (something) {
439
- * return qb.where('something', '=', something)
440
- * } else {
441
- * return qb.where('somethingElse', '=', somethingElse)
442
- * }
443
- * })
444
- * .execute()
445
- * ```
446
- *
447
435
  * The next example uses a helper funtion `log` to log a query:
448
436
  *
449
437
  * ```ts
450
- * function log<T extends AnyQueryBuilder>(qb: T): T {
438
+ * function log<T extends Compilable>(qb: T): T {
451
439
  * console.log(qb.compile())
452
440
  * return qb
453
441
  * }
@@ -461,6 +449,64 @@ class SelectQueryBuilder {
461
449
  call(func) {
462
450
  return func(this);
463
451
  }
452
+ /**
453
+ * Call `func(this)` if `condition` is true.
454
+ *
455
+ * This method is especially handy with optional selects. Any `select` or `selectAll`
456
+ * method calls add columns as optional fields to the output type when called inside
457
+ * the `func` callback. This is because we can't know if those selections were actually
458
+ * made before running the code.
459
+ *
460
+ * Also see [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/conditional-selects.md)
461
+ *
462
+ * ### Examples
463
+ *
464
+ * ```ts
465
+ * async function getPerson(id: number, withLastName: boolean) {
466
+ * return await db
467
+ * .selectFrom('person')
468
+ * .select(['id', 'first_name'])
469
+ * .if(withLastName, (qb) => qb.select('last_name'))
470
+ * .where('id', '=', id)
471
+ * .executeTakeFirstOrThrow()
472
+ * }
473
+ * ```
474
+ *
475
+ * Any selections added inside the `if` callback will be added as optional fields to the
476
+ * output type since we can't know if the selections were actually made before running
477
+ * the code. In the example above the return type of the `getPerson` function is:
478
+ *
479
+ * ```ts
480
+ * {
481
+ * id: number
482
+ * first_name: string
483
+ * last_name?: string
484
+ * }
485
+ * ```
486
+ *
487
+ * You can also call any other methods inside the callback:
488
+ *
489
+ * ```ts
490
+ * const { count } = db.fn
491
+ *
492
+ * db.selectFrom('person')
493
+ * .select('person.id')
494
+ * .if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
495
+ * .if(filterByPetCount, (qb) => qb
496
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
497
+ * .having(count('pet.id'), '>', petCountLimit)
498
+ * .groupBy('person.id')
499
+ * )
500
+ * ```
501
+ */
502
+ if(condition, func) {
503
+ if (condition) {
504
+ return func(this);
505
+ }
506
+ return new SelectQueryBuilder({
507
+ ...this.#props,
508
+ });
509
+ }
464
510
  /**
465
511
  * Gives an alias for the query. This method is only useful for sub queries.
466
512
  *
@@ -490,6 +536,9 @@ class SelectQueryBuilder {
490
536
  castTo() {
491
537
  return new SelectQueryBuilder(this.#props);
492
538
  }
539
+ /**
540
+ * Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
541
+ */
493
542
  withPlugin(plugin) {
494
543
  return new SelectQueryBuilder({
495
544
  ...this.#props,
@@ -6,13 +6,14 @@ import { SelectExpression } from '../parser/select-parser.js';
6
6
  import { ExistsExpression, FilterOperator, FilterValueExpressionOrList, WhereGrouper } from '../parser/filter-parser.js';
7
7
  import { ReturningRow } from '../parser/returning-parser.js';
8
8
  import { ReferenceExpression } from '../parser/reference-parser.js';
9
- import { AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
9
+ import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
10
10
  import { UpdateQueryNode } from '../operation-node/update-query-node.js';
11
11
  import { MutationObject } from '../parser/update-set-parser.js';
12
12
  import { Compilable } from '../util/compilable.js';
13
13
  import { QueryExecutor } from '../query-executor/query-executor.js';
14
14
  import { QueryId } from '../util/query-id.js';
15
15
  import { ParseContext } from '../parser/parse-context.js';
16
+ import { UpdateResult } from './update-result.js';
16
17
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
17
18
  import { WhereInterface } from './where-interface.js';
18
19
  import { JoinInterface } from './join-interface.js';
@@ -650,8 +651,6 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
650
651
  * Return arbitrary expressions:
651
652
  *
652
653
  * ```ts
653
- * const { raw } = db
654
- *
655
654
  * const { id, full_name, first_pet_id } = await db
656
655
  * .insertInto('person')
657
656
  * .values({
@@ -660,7 +659,7 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
660
659
  * })
661
660
  * .returning([
662
661
  * 'id as id',
663
- * raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
662
+ * db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
664
663
  * (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
665
664
  * ])
666
665
  * .executeTakeFirst()
@@ -676,24 +675,63 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
676
675
  /**
677
676
  * Simply calls the given function passing `this` as the only argument.
678
677
  *
679
- * This method can be useful when adding optional method calls:
678
+ * If you want to conditionally call a method on `this`, see
679
+ * the {@link if} method.
680
680
  *
681
681
  * ### Examples
682
682
  *
683
+ * The next example uses a helper funtion `log` to log a query:
684
+ *
683
685
  * ```ts
686
+ * function log<T extends Compilable>(qb: T): T {
687
+ * console.log(qb.compile())
688
+ * return qb
689
+ * }
690
+ *
684
691
  * db.updateTable('person')
685
692
  * .set(values)
686
- * .call((qb) => {
687
- * if (something) {
688
- * return qb.where('something', '=', something)
689
- * } else {
690
- * return qb.where('somethingElse', '=', somethingElse)
691
- * }
692
- * })
693
+ * .call(log)
693
694
  * .execute()
694
695
  * ```
695
696
  */
696
697
  call<T>(func: (qb: this) => T): T;
698
+ /**
699
+ * Call `func(this)` if `condition` is true.
700
+ *
701
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
702
+ * method calls add columns as optional fields to the output type when called inside
703
+ * the `func` callback. This is because we can't know if those selections were actually
704
+ * made before running the code.
705
+ *
706
+ * You can also call any other methods inside the callback.
707
+ *
708
+ * ### Examples
709
+ *
710
+ * ```ts
711
+ * async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
712
+ * return await db
713
+ * .updateTable('person')
714
+ * .set(updates)
715
+ * .where('id', '=', id)
716
+ * .returning(['id', 'first_name'])
717
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
718
+ * .executeTakeFirstOrThrow()
719
+ * }
720
+ * ```
721
+ *
722
+ * Any selections added inside the `if` callback will be added as optional fields to the
723
+ * output type since we can't know if the selections were actually made before running
724
+ * the code. In the example above the return type of the `updatePerson` function is:
725
+ *
726
+ * ```ts
727
+ * {
728
+ * id: number
729
+ * first_name: string
730
+ * last_name?: string
731
+ * }
732
+ * ```
733
+ */
734
+ 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>>;
697
735
  /**
698
736
  * Change the output type of the query.
699
737
  *
@@ -701,6 +739,9 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
701
739
  * don't support your use case.
702
740
  */
703
741
  castTo<T>(): UpdateQueryBuilder<DB, TB, T>;
742
+ /**
743
+ * Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
744
+ */
704
745
  withPlugin(plugin: KyselyPlugin): UpdateQueryBuilder<DB, TB, O>;
705
746
  toOperationNode(): UpdateQueryNode;
706
747
  compile(): CompiledQuery;
@@ -194,26 +194,72 @@ class UpdateQueryBuilder {
194
194
  /**
195
195
  * Simply calls the given function passing `this` as the only argument.
196
196
  *
197
- * This method can be useful when adding optional method calls:
197
+ * If you want to conditionally call a method on `this`, see
198
+ * the {@link if} method.
198
199
  *
199
200
  * ### Examples
200
201
  *
202
+ * The next example uses a helper funtion `log` to log a query:
203
+ *
201
204
  * ```ts
205
+ * function log<T extends Compilable>(qb: T): T {
206
+ * console.log(qb.compile())
207
+ * return qb
208
+ * }
209
+ *
202
210
  * db.updateTable('person')
203
211
  * .set(values)
204
- * .call((qb) => {
205
- * if (something) {
206
- * return qb.where('something', '=', something)
207
- * } else {
208
- * return qb.where('somethingElse', '=', somethingElse)
209
- * }
210
- * })
212
+ * .call(log)
211
213
  * .execute()
212
214
  * ```
213
215
  */
214
216
  call(func) {
215
217
  return func(this);
216
218
  }
219
+ /**
220
+ * Call `func(this)` if `condition` is true.
221
+ *
222
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
223
+ * method calls add columns as optional fields to the output type when called inside
224
+ * the `func` callback. This is because we can't know if those selections were actually
225
+ * made before running the code.
226
+ *
227
+ * You can also call any other methods inside the callback.
228
+ *
229
+ * ### Examples
230
+ *
231
+ * ```ts
232
+ * async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
233
+ * return await db
234
+ * .updateTable('person')
235
+ * .set(updates)
236
+ * .where('id', '=', id)
237
+ * .returning(['id', 'first_name'])
238
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
239
+ * .executeTakeFirstOrThrow()
240
+ * }
241
+ * ```
242
+ *
243
+ * Any selections added inside the `if` callback will be added as optional fields to the
244
+ * output type since we can't know if the selections were actually made before running
245
+ * the code. In the example above the return type of the `updatePerson` function is:
246
+ *
247
+ * ```ts
248
+ * {
249
+ * id: number
250
+ * first_name: string
251
+ * last_name?: string
252
+ * }
253
+ * ```
254
+ */
255
+ if(condition, func) {
256
+ if (condition) {
257
+ return func(this);
258
+ }
259
+ return new UpdateQueryBuilder({
260
+ ...this.#props,
261
+ });
262
+ }
217
263
  /**
218
264
  * Change the output type of the query.
219
265
  *
@@ -223,6 +269,9 @@ class UpdateQueryBuilder {
223
269
  castTo() {
224
270
  return new UpdateQueryBuilder(this.#props);
225
271
  }
272
+ /**
273
+ * Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
274
+ */
226
275
  withPlugin(plugin) {
227
276
  return new UpdateQueryBuilder({
228
277
  ...this.#props,
@@ -29,9 +29,9 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
29
29
  }
30
30
  visitSelectQuery(node) {
31
31
  const wrapInParens = this.parentNode !== undefined &&
32
+ !insert_query_node_js_1.InsertQueryNode.is(this.parentNode) &&
32
33
  !create_view_node_js_1.CreateViewNode.is(this.parentNode) &&
33
- !union_node_js_1.UnionNode.is(this.parentNode) &&
34
- !insert_query_node_js_1.InsertQueryNode.is(this.parentNode);
34
+ !union_node_js_1.UnionNode.is(this.parentNode);
35
35
  if (wrapInParens) {
36
36
  this.append('(');
37
37
  }
@@ -285,12 +285,13 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
285
285
  this.visitNode(node.on);
286
286
  }
287
287
  visitRaw(node) {
288
- node.sqlFragments.forEach((sql, i) => {
289
- this.append(sql);
290
- if (node.params.length > i) {
291
- this.visitNode(node.params[i]);
288
+ const { sqlFragments, params } = node;
289
+ for (let i = 0; i < sqlFragments.length; ++i) {
290
+ this.append(sqlFragments[i]);
291
+ if (params.length > i) {
292
+ this.visitNode(params[i]);
292
293
  }
293
- });
294
+ }
294
295
  }
295
296
  visitOperator(node) {
296
297
  this.append(node.operator);
@@ -162,7 +162,7 @@ export declare class QueryCreator<DB> {
162
162
  /**
163
163
  * Creates a delete query.
164
164
  *
165
- * See the {@link WhereInterface.where} method for examples on how to specify
165
+ * See the {@link DeleteQueryBuilder.where} method for examples on how to specify
166
166
  * a where clause for the delete operation.
167
167
  *
168
168
  * The return value of the query is an instance of {@link DeleteResult}.
@@ -182,7 +182,7 @@ export declare class QueryCreator<DB> {
182
182
  /**
183
183
  * Creates an update query.
184
184
  *
185
- * See the {@link WhereInterface.where} method for examples on how to specify
185
+ * See the {@link UpdateQueryBuilder.where} method for examples on how to specify
186
186
  * a where clause for the update operation.
187
187
  *
188
188
  * See the {@link UpdateQueryBuilder.set} method for examples on how to
@@ -78,7 +78,7 @@ class QueryCreator {
78
78
  /**
79
79
  * Creates a delete query.
80
80
  *
81
- * See the {@link WhereInterface.where} method for examples on how to specify
81
+ * See the {@link DeleteQueryBuilder.where} method for examples on how to specify
82
82
  * a where clause for the delete operation.
83
83
  *
84
84
  * The return value of the query is an instance of {@link DeleteResult}.
@@ -105,7 +105,7 @@ class QueryCreator {
105
105
  /**
106
106
  * Creates an update query.
107
107
  *
108
- * See the {@link WhereInterface.where} method for examples on how to specify
108
+ * See the {@link UpdateQueryBuilder.where} method for examples on how to specify
109
109
  * a where clause for the update operation.
110
110
  *
111
111
  * See the {@link UpdateQueryBuilder.set} method for examples on how to
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryExecutor = void 0;
4
4
  const object_utils_js_1 = require("../util/object-utils.js");
5
+ const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
5
6
  /**
6
7
  * This class abstracts away the details of how to compile a query into SQL
7
8
  * and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
@@ -10,8 +11,8 @@ const object_utils_js_1 = require("../util/object-utils.js");
10
11
  */
11
12
  class QueryExecutor {
12
13
  #plugins;
13
- constructor(plugins = []) {
14
- this.#plugins = (0, object_utils_js_1.freeze)([...plugins]);
14
+ constructor(plugins) {
15
+ this.#plugins = plugins ?? NO_PLUGINS;
15
16
  }
16
17
  get plugins() {
17
18
  return this.#plugins;
@@ -40,8 +40,8 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
40
40
  constructor(rawBuilder: RawBuilder<O>, alias: A);
41
41
  }
42
42
  export interface RawBuilderProps {
43
- readonly queryId: QueryId;
44
- readonly executor: QueryExecutor;
43
+ readonly queryId?: QueryId;
44
+ readonly executor?: QueryExecutor;
45
45
  readonly sql: string;
46
46
  readonly parameters?: ReadonlyArray<unknown>;
47
47
  }
@@ -7,11 +7,18 @@ const raw_node_js_1 = require("../operation-node/raw-node.js");
7
7
  const value_node_js_1 = require("../operation-node/value-node.js");
8
8
  const reference_parser_js_1 = require("../parser/reference-parser.js");
9
9
  const prevent_await_js_1 = require("../util/prevent-await.js");
10
+ const query_id_js_1 = require("../util/query-id.js");
10
11
  const object_utils_js_1 = require("../util/object-utils.js");
12
+ const noop_query_executor_js_1 = require("../query-executor/noop-query-executor.js");
11
13
  class RawBuilder {
12
14
  #props;
13
15
  constructor(props) {
14
- this.#props = (0, object_utils_js_1.freeze)(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
+ });
15
22
  }
16
23
  as(alias) {
17
24
  return new AliasedRawBuilder(this, alias);
@@ -9,7 +9,6 @@ const data_type_node_js_1 = require("../operation-node/data-type-node.js");
9
9
  const drop_column_node_js_1 = require("../operation-node/drop-column-node.js");
10
10
  const identifier_node_js_1 = require("../operation-node/identifier-node.js");
11
11
  const rename_column_node_js_1 = require("../operation-node/rename-column-node.js");
12
- const table_node_js_1 = require("../operation-node/table-node.js");
13
12
  const object_utils_js_1 = require("../util/object-utils.js");
14
13
  const prevent_await_js_1 = require("../util/prevent-await.js");
15
14
  const column_definition_builder_js_1 = require("./column-definition-builder.js");
@@ -22,6 +21,7 @@ const check_constraint_node_js_1 = require("../operation-node/check-constraint-n
22
21
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
23
22
  const column_node_js_1 = require("../operation-node/column-node.js");
24
23
  const default_value_parser_js_1 = require("../parser/default-value-parser.js");
24
+ const table_parser_js_1 = require("../parser/table-parser.js");
25
25
  /**
26
26
  * This builder can be used to create a `alter table` query.
27
27
  */
@@ -34,7 +34,7 @@ class AlterTableBuilder {
34
34
  return new AlterTableExecutor({
35
35
  ...this.#props,
36
36
  alterTableNode: alter_table_node_js_1.AlterTableNode.cloneWith(this.#props.alterTableNode, {
37
- renameTo: table_node_js_1.TableNode.create(newTableName),
37
+ renameTo: (0, table_parser_js_1.parseTable)(newTableName),
38
38
  }),
39
39
  });
40
40
  }
@@ -104,7 +104,7 @@ class AlterTableBuilder {
104
104
  addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns) {
105
105
  return new AlterTableAddForeignKeyConstraintBuilder({
106
106
  ...this.#props,
107
- constraintBuilder: new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)),
107
+ constraintBuilder: new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), (0, table_parser_js_1.parseTable)(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)),
108
108
  });
109
109
  }
110
110
  }
@@ -8,12 +8,12 @@ const column_definition_builder_js_1 = require("./column-definition-builder.js")
8
8
  const object_utils_js_1 = require("../util/object-utils.js");
9
9
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
10
10
  const column_node_js_1 = require("../operation-node/column-node.js");
11
- const table_node_js_1 = require("../operation-node/table-node.js");
12
11
  const foreign_key_constraint_builder_js_1 = require("./foreign-key-constraint-builder.js");
13
12
  const data_type_parser_js_1 = require("../parser/data-type-parser.js");
14
13
  const primary_constraint_node_js_1 = require("../operation-node/primary-constraint-node.js");
15
14
  const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
16
15
  const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
16
+ const table_parser_js_1 = require("../parser/table-parser.js");
17
17
  /**
18
18
  * This builder can be used to create a `create table` query.
19
19
  */
@@ -195,7 +195,7 @@ class CreateTableBuilder {
195
195
  * ```
196
196
  */
197
197
  addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build = object_utils_js_1.noop) {
198
- const builder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
198
+ const builder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), (0, table_parser_js_1.parseTable)(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
199
199
  return new CreateTableBuilder({
200
200
  ...this.#props,
201
201
  createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, builder.toOperationNode()),
@@ -5,7 +5,7 @@ const prevent_await_js_1 = require("../util/prevent-await.js");
5
5
  const object_utils_js_1 = require("../util/object-utils.js");
6
6
  const create_view_node_js_1 = require("../operation-node/create-view-node.js");
7
7
  const reference_parser_js_1 = require("../parser/reference-parser.js");
8
- const immediate_value_plugin_js_1 = require("../plugin/immediate-value.ts/immediate-value-plugin.js");
8
+ const immediate_value_plugin_js_1 = require("../plugin/immediate-value/immediate-value-plugin.js");
9
9
  class CreateViewBuilder {
10
10
  #props;
11
11
  constructor(props) {
@@ -16,3 +16,4 @@ export declare function asArray<T>(arg: T | T[]): T[];
16
16
  export declare function asReadonlyArray<T>(arg: T | ReadonlyArray<T>): ReadonlyArray<T>;
17
17
  export declare function isReadonlyArray(arg: unknown): arg is ReadonlyArray<unknown>;
18
18
  export declare function noop<T>(obj: T): T;
19
+ export declare function compare(obj1: unknown, obj2: unknown): boolean;