kysely 0.28.17 → 0.28.19

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 (70) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +3 -2
  3. package/dist/cjs/dialect/mssql/mssql-adapter.js +13 -7
  4. package/dist/cjs/dialect/mssql/mssql-driver.d.ts +3 -0
  5. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +10 -0
  6. package/dist/cjs/dialect/mysql/mysql-driver.d.ts +3 -0
  7. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +1 -1
  8. package/dist/cjs/dialect/postgres/postgres-adapter.js +13 -7
  9. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +12 -2
  10. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +6 -1
  11. package/dist/cjs/dialect/postgres/postgres-driver.js +35 -3
  12. package/dist/cjs/dialect/postgres/postgres-introspector.js +18 -12
  13. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
  14. package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +3 -0
  15. package/dist/cjs/driver/aborted-transaction-error.d.ts +11 -0
  16. package/dist/cjs/driver/aborted-transaction-error.js +20 -0
  17. package/dist/cjs/driver/driver.d.ts +3 -0
  18. package/dist/cjs/driver/dummy-driver.d.ts +3 -0
  19. package/dist/cjs/driver/runtime-driver.d.ts +3 -0
  20. package/dist/cjs/helpers/mssql.d.ts +3 -1
  21. package/dist/cjs/helpers/mssql.js +4 -2
  22. package/dist/cjs/index.d.ts +1 -0
  23. package/dist/cjs/index.js +1 -0
  24. package/dist/cjs/kysely.d.ts +55 -3
  25. package/dist/cjs/kysely.js +136 -54
  26. package/dist/cjs/migration/migrator.js +22 -19
  27. package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -2
  28. package/dist/cjs/query-builder/join-builder.d.ts +2 -2
  29. package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -2
  30. package/dist/cjs/query-builder/select-query-builder.d.ts +28 -6
  31. package/dist/cjs/query-builder/update-query-builder.d.ts +1 -1
  32. package/dist/cjs/query-compiler/default-query-compiler.js +4 -4
  33. package/dist/cjs/schema/create-type-builder.d.ts +1 -1
  34. package/dist/cjs/schema/create-type-builder.js +1 -1
  35. package/dist/cjs/util/column-type.d.ts +1 -1
  36. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +3 -2
  37. package/dist/esm/dialect/mssql/mssql-adapter.js +13 -7
  38. package/dist/esm/dialect/mssql/mssql-driver.d.ts +3 -0
  39. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +10 -0
  40. package/dist/esm/dialect/mysql/mysql-driver.d.ts +3 -0
  41. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +1 -1
  42. package/dist/esm/dialect/postgres/postgres-adapter.js +13 -7
  43. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +12 -2
  44. package/dist/esm/dialect/postgres/postgres-driver.d.ts +6 -1
  45. package/dist/esm/dialect/postgres/postgres-driver.js +35 -3
  46. package/dist/esm/dialect/postgres/postgres-introspector.js +18 -12
  47. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
  48. package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +3 -0
  49. package/dist/esm/driver/aborted-transaction-error.d.ts +11 -0
  50. package/dist/esm/driver/aborted-transaction-error.js +16 -0
  51. package/dist/esm/driver/driver.d.ts +3 -0
  52. package/dist/esm/driver/dummy-driver.d.ts +3 -0
  53. package/dist/esm/driver/runtime-driver.d.ts +3 -0
  54. package/dist/esm/helpers/mssql.d.ts +3 -1
  55. package/dist/esm/helpers/mssql.js +4 -2
  56. package/dist/esm/index.d.ts +1 -0
  57. package/dist/esm/index.js +1 -0
  58. package/dist/esm/kysely.d.ts +55 -3
  59. package/dist/esm/kysely.js +136 -54
  60. package/dist/esm/migration/migrator.js +22 -19
  61. package/dist/esm/query-builder/delete-query-builder.d.ts +2 -2
  62. package/dist/esm/query-builder/join-builder.d.ts +2 -2
  63. package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -2
  64. package/dist/esm/query-builder/select-query-builder.d.ts +28 -6
  65. package/dist/esm/query-builder/update-query-builder.d.ts +1 -1
  66. package/dist/esm/query-compiler/default-query-compiler.js +4 -4
  67. package/dist/esm/schema/create-type-builder.d.ts +1 -1
  68. package/dist/esm/schema/create-type-builder.js +1 -1
  69. package/dist/esm/util/column-type.d.ts +1 -1
  70. package/package.json +36 -34
@@ -6,6 +6,7 @@ import { QueryCreator } from './query-creator.js';
6
6
  import { DefaultQueryExecutor } from './query-executor/default-query-executor.js';
7
7
  import { freeze, isObject, isUndefined } from './util/object-utils.js';
8
8
  import { RuntimeDriver } from './driver/runtime-driver.js';
9
+ import { isAbortedTransactionError, } from './driver/aborted-transaction-error.js';
9
10
  import { SingleConnectionProvider } from './driver/single-connection-provider.js';
10
11
  import { validateTransactionSettings, } from './driver/driver.js';
11
12
  import { createFunctionModule, } from './query-builder/function-module.js';
@@ -24,7 +25,8 @@ Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
24
25
  * The main Kysely class.
25
26
  *
26
27
  * You should create one instance of `Kysely` per database using the {@link Kysely}
27
- * constructor. Each `Kysely` instance maintains its own connection pool.
28
+ * constructor. The dialect manages database connections and may use a shared
29
+ * pool or database instance.
28
30
  *
29
31
  * ### Examples
30
32
  *
@@ -433,7 +435,16 @@ export class Kysely extends QueryCreator {
433
435
  /**
434
436
  * Releases all resources and disconnects from the database.
435
437
  *
436
- * You need to call this when you are done using the `Kysely` instance.
438
+ * Call this when you are done using the `Kysely` instance and Kysely is
439
+ * responsible for closing its connections, pool, or database instance.
440
+ *
441
+ * If the pool or database instance is shared with other consumers or its
442
+ * lifecycle is managed elsewhere, skip this call. Let the application close it
443
+ * once all consumers, including Kysely, have finished. No separate cleanup of
444
+ * the `Kysely` instance is needed.
445
+ *
446
+ * The driver is initialized lazily. If initialization hasn't started, this
447
+ * method does nothing.
437
448
  */
438
449
  async destroy() {
439
450
  await this.#props.driver.destroy();
@@ -559,8 +570,10 @@ export class TransactionBuilder {
559
570
  const settings = { isolationLevel, accessMode };
560
571
  validateTransactionSettings(settings);
561
572
  return this.#props.executor.provideConnection(async (connection) => {
562
- const state = { isCommitted: false, isRolledBack: false };
563
- const executor = new NotCommittedOrRolledBackAssertingExecutor(this.#props.executor.withConnectionProvider(new SingleConnectionProvider(connection)), state);
573
+ const transactionState = {
574
+ status: 'active',
575
+ };
576
+ const executor = new NotCommittedOrRolledBackAssertingExecutor(this.#props.executor.withConnectionProvider(new SingleConnectionProvider(connection)), transactionState);
564
577
  const transaction = new Transaction({
565
578
  ...kyselyProps,
566
579
  executor,
@@ -571,13 +584,16 @@ export class TransactionBuilder {
571
584
  transactionBegun = true;
572
585
  const result = await callback(transaction);
573
586
  await this.#props.driver.commitTransaction(connection);
574
- state.isCommitted = true;
587
+ transactionState.status = 'committed';
575
588
  return result;
576
589
  }
577
590
  catch (error) {
578
- if (transactionBegun) {
591
+ if (isAbortedTransactionError(error)) {
592
+ transactionState.status = 'aborted';
593
+ }
594
+ else if (transactionBegun) {
579
595
  await this.#props.driver.rollbackTransaction(connection);
580
- state.isRolledBack = true;
596
+ transactionState.status = 'rolled back';
581
597
  }
582
598
  throw error;
583
599
  }
@@ -606,10 +622,17 @@ export class ControlledTransactionBuilder {
606
622
  const settings = { isolationLevel, accessMode };
607
623
  validateTransactionSettings(settings);
608
624
  const connection = await provideControlledConnection(this.#props.executor);
609
- await this.#props.driver.beginTransaction(connection.connection, settings);
625
+ try {
626
+ await this.#props.driver.beginTransaction(connection.connection, settings);
627
+ }
628
+ catch (error) {
629
+ connection.release();
630
+ throw error;
631
+ }
610
632
  return new ControlledTransaction({
611
633
  ...props,
612
634
  connection,
635
+ transactionState: { status: 'active' },
613
636
  executor: this.#props.executor.withConnectionProvider(new SingleConnectionProvider(connection.connection)),
614
637
  });
615
638
  }
@@ -617,29 +640,38 @@ export class ControlledTransactionBuilder {
617
640
  export class ControlledTransaction extends Transaction {
618
641
  #props;
619
642
  #compileQuery;
620
- #state;
621
643
  constructor(props) {
622
- const state = { isCommitted: false, isRolledBack: false };
623
- props = {
644
+ const controlledState = props.transactionState ?? {
645
+ status: 'active',
646
+ };
647
+ const controlledProps = {
624
648
  ...props,
625
- executor: new NotCommittedOrRolledBackAssertingExecutor(props.executor, state),
649
+ transactionState: controlledState,
650
+ executor: new NotCommittedOrRolledBackAssertingExecutor(props.executor, controlledState),
626
651
  };
627
- const { connection, ...transactionProps } = props;
652
+ const { connection, transactionState, ...transactionProps } = controlledProps;
628
653
  super(transactionProps);
629
- this.#props = freeze(props);
630
- this.#state = state;
654
+ this.#props = freeze(controlledProps);
631
655
  const queryId = createQueryId();
632
- this.#compileQuery = (node) => props.executor.compileQuery(node, queryId);
656
+ this.#compileQuery = (node) => controlledProps.executor.compileQuery(node, queryId);
633
657
  }
634
658
  get isCommitted() {
635
- return this.#state.isCommitted;
659
+ return this.#props.transactionState.status === 'committed';
636
660
  }
637
661
  get isRolledBack() {
638
- return this.#state.isRolledBack;
662
+ return (this.#props.transactionState.status === 'rolled back' ||
663
+ this.#props.transactionState.status === 'aborted');
639
664
  }
640
665
  /**
641
666
  * Commits the transaction.
642
667
  *
668
+ * If the database rolls back instead of committing, marks the transaction
669
+ * as rolled back, releases the connection and throws {@link AbortedTransactionError}.
670
+ *
671
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
672
+ * before awaiting this command. When consuming a stream with `for await`,
673
+ * exit the loop before committing.
674
+ *
643
675
  * See {@link rollback}.
644
676
  *
645
677
  * ### Examples
@@ -662,16 +694,36 @@ export class ControlledTransaction extends Transaction {
662
694
  * ```
663
695
  */
664
696
  commit() {
665
- assertNotCommittedOrRolledBack(this.#state);
666
697
  return new Command(async () => {
667
- await this.#props.driver.commitTransaction(this.#props.connection.connection);
668
- this.#state.isCommitted = true;
669
- this.#props.connection.release();
698
+ await this.#props.executor.provideConnection(async (connection) => {
699
+ try {
700
+ await this.#props.driver.commitTransaction(connection);
701
+ }
702
+ catch (error) {
703
+ if (isAbortedTransactionError(error)) {
704
+ this.#props.transactionState.status = 'aborted';
705
+ this.#props.connection.release();
706
+ }
707
+ throw error;
708
+ }
709
+ this.#props.transactionState.status = 'committed';
710
+ this.#props.connection.release();
711
+ });
670
712
  });
671
713
  }
672
714
  /**
673
715
  * Rolls back the transaction.
674
716
  *
717
+ * If a commit already reported that the database rolled back the transaction,
718
+ * succeeds without sending another rollback command.
719
+ *
720
+ * The connection is released even if the rollback fails, so the transaction
721
+ * cannot be used afterwards in either case.
722
+ *
723
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
724
+ * before awaiting this command. When consuming a stream with `for await`,
725
+ * exit the loop before rolling back.
726
+ *
675
727
  * See {@link commit} and {@link rollbackToSavepoint}.
676
728
  *
677
729
  * ### Examples
@@ -694,16 +746,29 @@ export class ControlledTransaction extends Transaction {
694
746
  * ```
695
747
  */
696
748
  rollback() {
697
- assertNotCommittedOrRolledBack(this.#state);
698
749
  return new Command(async () => {
699
- await this.#props.driver.rollbackTransaction(this.#props.connection.connection);
700
- this.#state.isRolledBack = true;
701
- this.#props.connection.release();
750
+ if (this.#props.transactionState.status === 'aborted') {
751
+ return;
752
+ }
753
+ await this.#props.executor.provideConnection(async (connection) => {
754
+ // A failed rollback can't be meaningfully retried (e.g. dead connection).
755
+ try {
756
+ await this.#props.driver.rollbackTransaction(connection);
757
+ }
758
+ finally {
759
+ this.#props.transactionState.status = 'rolled back';
760
+ this.#props.connection.release();
761
+ }
762
+ });
702
763
  });
703
764
  }
704
765
  /**
705
766
  * Creates a savepoint with a given name.
706
767
  *
768
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
769
+ * before awaiting this command. When consuming a stream with `for await`,
770
+ * exit the loop before creating a savepoint.
771
+ *
707
772
  * See {@link rollbackToSavepoint} and {@link releaseSavepoint}.
708
773
  *
709
774
  * For a type-safe experience, you should use the returned instance from now on.
@@ -731,15 +796,20 @@ export class ControlledTransaction extends Transaction {
731
796
  * ```
732
797
  */
733
798
  savepoint(savepointName) {
734
- assertNotCommittedOrRolledBack(this.#state);
735
799
  return new Command(async () => {
736
- await this.#props.driver.savepoint?.(this.#props.connection.connection, savepointName, this.#compileQuery);
800
+ await this.#props.executor.provideConnection(async (connection) => {
801
+ await this.#props.driver.savepoint?.(connection, savepointName, this.#compileQuery);
802
+ });
737
803
  return new ControlledTransaction({ ...this.#props });
738
804
  });
739
805
  }
740
806
  /**
741
807
  * Rolls back to a savepoint with a given name.
742
808
  *
809
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
810
+ * before awaiting this command. When consuming a stream with `for await`,
811
+ * exit the loop before rolling back to a savepoint.
812
+ *
743
813
  * See {@link savepoint} and {@link releaseSavepoint}.
744
814
  *
745
815
  * You must use the same instance returned by {@link savepoint}, or
@@ -768,15 +838,20 @@ export class ControlledTransaction extends Transaction {
768
838
  * ```
769
839
  */
770
840
  rollbackToSavepoint(savepointName) {
771
- assertNotCommittedOrRolledBack(this.#state);
772
841
  return new Command(async () => {
773
- await this.#props.driver.rollbackToSavepoint?.(this.#props.connection.connection, savepointName, this.#compileQuery);
842
+ await this.#props.executor.provideConnection(async (connection) => {
843
+ await this.#props.driver.rollbackToSavepoint?.(connection, savepointName, this.#compileQuery);
844
+ });
774
845
  return new ControlledTransaction({ ...this.#props });
775
846
  });
776
847
  }
777
848
  /**
778
849
  * Releases a savepoint with a given name.
779
850
  *
851
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
852
+ * before awaiting this command. When consuming a stream with `for await`,
853
+ * exit the loop before releasing a savepoint.
854
+ *
780
855
  * See {@link savepoint} and {@link rollbackToSavepoint}.
781
856
  *
782
857
  * You must use the same instance returned by {@link savepoint}, or
@@ -810,9 +885,10 @@ export class ControlledTransaction extends Transaction {
810
885
  * ```
811
886
  */
812
887
  releaseSavepoint(savepointName) {
813
- assertNotCommittedOrRolledBack(this.#state);
814
888
  return new Command(async () => {
815
- await this.#props.driver.releaseSavepoint?.(this.#props.connection.connection, savepointName, this.#compileQuery);
889
+ await this.#props.executor.provideConnection(async (connection) => {
890
+ await this.#props.driver.releaseSavepoint?.(connection, savepointName, this.#compileQuery);
891
+ });
816
892
  return new ControlledTransaction({ ...this.#props });
817
893
  });
818
894
  }
@@ -850,11 +926,11 @@ export class Command {
850
926
  return await this.#cb();
851
927
  }
852
928
  }
853
- function assertNotCommittedOrRolledBack(state) {
854
- if (state.isCommitted) {
929
+ function assertNotCommittedOrRolledBack(transactionState) {
930
+ if (transactionState.status === 'committed') {
855
931
  throw new Error('Transaction is already committed');
856
932
  }
857
- if (state.isRolledBack) {
933
+ if (transactionState.status !== 'active') {
858
934
  throw new Error('Transaction is already rolled back');
859
935
  }
860
936
  }
@@ -866,15 +942,13 @@ function assertNotCommittedOrRolledBack(state) {
866
942
  */
867
943
  class NotCommittedOrRolledBackAssertingExecutor {
868
944
  #executor;
869
- #state;
870
- constructor(executor, state) {
871
- if (executor instanceof NotCommittedOrRolledBackAssertingExecutor) {
872
- this.#executor = executor.#executor;
873
- }
874
- else {
875
- this.#executor = executor;
876
- }
877
- this.#state = state;
945
+ #transactionState;
946
+ constructor(executor, transactionState) {
947
+ this.#executor =
948
+ executor instanceof NotCommittedOrRolledBackAssertingExecutor
949
+ ? executor.#executor
950
+ : executor;
951
+ this.#transactionState = transactionState;
878
952
  }
879
953
  get adapter() {
880
954
  return this.#executor.adapter;
@@ -889,29 +963,37 @@ class NotCommittedOrRolledBackAssertingExecutor {
889
963
  return this.#executor.compileQuery(node, queryId);
890
964
  }
891
965
  provideConnection(consumer) {
892
- return this.#executor.provideConnection(consumer);
966
+ assertNotCommittedOrRolledBack(this.#transactionState);
967
+ return this.#executor.provideConnection(async (connection) => {
968
+ assertNotCommittedOrRolledBack(this.#transactionState);
969
+ return await consumer(connection);
970
+ });
893
971
  }
894
972
  executeQuery(compiledQuery) {
895
- assertNotCommittedOrRolledBack(this.#state);
896
- return this.#executor.executeQuery(compiledQuery);
973
+ assertNotCommittedOrRolledBack(this.#transactionState);
974
+ return this.#executor
975
+ .withConnectionProvider(this)
976
+ .executeQuery(compiledQuery);
897
977
  }
898
978
  stream(compiledQuery, chunkSize) {
899
- assertNotCommittedOrRolledBack(this.#state);
900
- return this.#executor.stream(compiledQuery, chunkSize);
979
+ assertNotCommittedOrRolledBack(this.#transactionState);
980
+ return this.#executor
981
+ .withConnectionProvider(this)
982
+ .stream(compiledQuery, chunkSize);
901
983
  }
902
984
  withConnectionProvider(connectionProvider) {
903
- return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withConnectionProvider(connectionProvider), this.#state);
985
+ return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withConnectionProvider(connectionProvider), this.#transactionState);
904
986
  }
905
987
  withPlugin(plugin) {
906
- return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugin(plugin), this.#state);
988
+ return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugin(plugin), this.#transactionState);
907
989
  }
908
990
  withPlugins(plugins) {
909
- return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugins(plugins), this.#state);
991
+ return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugins(plugins), this.#transactionState);
910
992
  }
911
993
  withPluginAtFront(plugin) {
912
- return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPluginAtFront(plugin), this.#state);
994
+ return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPluginAtFront(plugin), this.#transactionState);
913
995
  }
914
996
  withoutPlugins() {
915
- return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withoutPlugins(), this.#state);
997
+ return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withoutPlugins(), this.#transactionState);
916
998
  }
917
999
  }
@@ -410,34 +410,37 @@ export class Migrator {
410
410
  lockTableSchema: this.#props.migrationTableSchema,
411
411
  });
412
412
  const run = async (db) => {
413
+ const state = await this.#getState(db);
414
+ if (state.migrations.length === 0) {
415
+ return { results: [] };
416
+ }
417
+ const { direction, step } = getMigrationDirectionAndStep(state);
418
+ if (step <= 0) {
419
+ return { results: [] };
420
+ }
421
+ if (direction === 'Down') {
422
+ return await this.#migrateDown(db, state, step);
423
+ }
424
+ else if (direction === 'Up') {
425
+ return await this.#migrateUp(db, state, step);
426
+ }
427
+ return { results: [] };
428
+ };
429
+ const runWithLock = async (db, cb) => {
413
430
  try {
414
431
  await adapter.acquireMigrationLock(db, lockOptions);
415
- const state = await this.#getState(db);
416
- if (state.migrations.length === 0) {
417
- return { results: [] };
418
- }
419
- const { direction, step } = getMigrationDirectionAndStep(state);
420
- if (step <= 0) {
421
- return { results: [] };
422
- }
423
- if (direction === 'Down') {
424
- return await this.#migrateDown(db, state, step);
425
- }
426
- else if (direction === 'Up') {
427
- return await this.#migrateUp(db, state, step);
428
- }
429
- return { results: [] };
432
+ return await cb(db);
430
433
  }
431
434
  finally {
432
435
  await adapter.releaseMigrationLock(db, lockOptions);
433
436
  }
434
437
  };
435
438
  if (adapter.supportsTransactionalDdl && !this.#props.disableTransactions) {
436
- return this.#props.db.transaction().execute(run);
437
- }
438
- else {
439
- return this.#props.db.connection().execute(run);
439
+ return this.#props.db
440
+ .connection()
441
+ .execute((db) => runWithLock(db, (db) => db.transaction().execute((trx) => run(trx))));
440
442
  }
443
+ return this.#props.db.connection().execute((db) => runWithLock(db, run));
441
444
  }
442
445
  async #getState(db) {
443
446
  const migrations = await this.#resolveMigrations();
@@ -1039,7 +1039,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
1039
1039
  * delete from `pet` order by `created_at` limit ?
1040
1040
  * ```
1041
1041
  */
1042
- limit(limit: ValueExpression<DB, TB, number>): DeleteQueryBuilder<DB, TB, O>;
1042
+ limit<VE extends ValueExpression<DB, TB, number>>(limit: VE): DeleteQueryBuilder<DB, TB, O>;
1043
1043
  /**
1044
1044
  * This can be used to add any additional SQL to the end of the query.
1045
1045
  *
@@ -1122,7 +1122,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
1122
1122
  * }>
1123
1123
  * ```
1124
1124
  */
1125
- $if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<any, any, O2>): O2 extends DeleteResult ? DeleteQueryBuilder<DB, TB, DeleteResult> : O2 extends O & infer E ? DeleteQueryBuilder<DB, TB, O & Partial<E>> : DeleteQueryBuilder<DB, TB, Partial<O2>>;
1125
+ $if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<any, any, O2>): unknown extends O2 ? DeleteQueryBuilder<any, any, O2> : O2 extends DeleteResult ? DeleteQueryBuilder<DB, TB, DeleteResult> : O2 extends O & infer E ? DeleteQueryBuilder<DB, TB, O & Partial<E>> : DeleteQueryBuilder<DB, TB, Partial<O2>>;
1126
1126
  /**
1127
1127
  * Change the output type of the query.
1128
1128
  *
@@ -14,14 +14,14 @@ export declare class JoinBuilder<DB, TB extends keyof DB> implements OperationNo
14
14
  * See {@link WhereInterface.where} for documentation and examples.
15
15
  */
16
16
  on<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): JoinBuilder<DB, TB>;
17
- on(expression: ExpressionOrFactory<DB, TB, SqlBool>): JoinBuilder<DB, TB>;
17
+ on<E extends ExpressionOrFactory<DB, TB, SqlBool>>(expression: E): JoinBuilder<DB, TB>;
18
18
  /**
19
19
  * Just like {@link WhereInterface.whereRef} but adds an item to the join's
20
20
  * `on` clause instead.
21
21
  *
22
22
  * See {@link WhereInterface.whereRef} for documentation and examples.
23
23
  */
24
- onRef(lhs: ReferenceExpression<DB, TB>, op: ComparisonOperatorExpression, rhs: ReferenceExpression<DB, TB>): JoinBuilder<DB, TB>;
24
+ onRef<LRE extends ReferenceExpression<DB, TB>, RRE extends ReferenceExpression<DB, TB>>(lhs: LRE, op: ComparisonOperatorExpression, rhs: RRE): JoinBuilder<DB, TB>;
25
25
  /**
26
26
  * Adds `on true`.
27
27
  */
@@ -506,7 +506,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
506
506
  * "last_name" = excluded."last_name"
507
507
  * ```
508
508
  */
509
- doUpdateSet(update: UpdateObjectExpression<OnConflictDatabase<DB, TB>, OnConflictTables<TB>, OnConflictTables<TB>>): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>;
509
+ doUpdateSet(update: UpdateObjectExpression<OnConflictUpdateDatabase<DB, TB>, OnConflictTables<TB>, OnConflictTables<TB>>): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>;
510
510
  /**
511
511
  * Simply calls the provided function passing `this` as the only argument. `$call` returns
512
512
  * what the provided function returns.
@@ -516,9 +516,12 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
516
516
  export interface OnConflictBuilderProps {
517
517
  readonly onConflictNode: OnConflictNode;
518
518
  }
519
- export type OnConflictDatabase<DB, TB extends keyof DB> = {
519
+ export type OnConflictUpdateDatabase<DB, TB extends keyof DB> = {
520
520
  [K in keyof DB | 'excluded']: Updateable<K extends keyof DB ? DB[K] : DB[TB]>;
521
521
  };
522
+ export type OnConflictDatabase<DB, TB extends keyof DB> = {
523
+ [K in keyof DB | 'excluded']: K extends keyof DB ? DB[K] : DB[TB];
524
+ };
522
525
  export type OnConflictTables<TB> = TB | 'excluded';
523
526
  export declare class OnConflictDoNothingBuilder<DB, TB extends keyof DB> implements OperationNodeSource {
524
527
  #private;
@@ -1377,7 +1377,7 @@ export interface SelectQueryBuilder<DB, TB extends keyof DB, O> extends WhereInt
1377
1377
  * select "first_name" from "person" limit $1 offset $2
1378
1378
  * ```
1379
1379
  */
1380
- limit(limit: ValueExpression<DB, TB, number | bigint | null>): SelectQueryBuilder<DB, TB, O>;
1380
+ limit<VE extends ValueExpression<DB, TB, number | bigint | null>>(limit: VE): SelectQueryBuilder<DB, TB, O>;
1381
1381
  /**
1382
1382
  * Adds an `offset` clause to the query.
1383
1383
  *
@@ -1400,7 +1400,7 @@ export interface SelectQueryBuilder<DB, TB extends keyof DB, O> extends WhereInt
1400
1400
  * select "first_name" from "person" limit $1 offset $2
1401
1401
  * ```
1402
1402
  */
1403
- offset(offset: ValueExpression<DB, TB, number | bigint>): SelectQueryBuilder<DB, TB, O>;
1403
+ offset<VE extends ValueExpression<DB, TB, number | bigint>>(offset: VE): SelectQueryBuilder<DB, TB, O>;
1404
1404
  /**
1405
1405
  * Adds a `fetch` clause to the query.
1406
1406
  *
@@ -2367,22 +2367,44 @@ export interface SelectQueryBuilderProps {
2367
2367
  export interface AliasedSelectQueryBuilder<O = undefined, A extends string = never> extends AliasedExpression<O, A> {
2368
2368
  get isAliasedSelectQueryBuilder(): true;
2369
2369
  }
2370
- export type SelectQueryBuilderWithInnerJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? InnerJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? SelectQueryBuilder<DB, TB | TE, O> : TE extends AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : never;
2370
+ /**
2371
+ * Guards a join's result type against being computed for an unknown table
2372
+ * expression.
2373
+ *
2374
+ * `TableExpression<DB, TB> extends TE` only holds when `TE` is as wide as its
2375
+ * own constraint, which never happens at a call site - there `TE` is a specific
2376
+ * table name, alias or subquery. It does happen when the compiler asks what a
2377
+ * join returns for an arbitrary table expression, which is what it has to do
2378
+ * whenever it compares two `SelectQueryBuilder` types structurally.
2379
+ *
2380
+ * Without this guard that comparison never terminates on its own: computing the
2381
+ * join result yields a `SelectQueryBuilder` over a wider `DB`, whose own join
2382
+ * methods yield a wider one still, so the compiler only stops when it hits its
2383
+ * internal recursion limit. That limit differs between TypeScript versions - 7.0
2384
+ * searches a level deeper than 5.9 and 6.0 - which made comparing two select
2385
+ * query builders over 6x more expensive there.
2386
+ *
2387
+ * Collapsing to a fixed type makes the recursion converge instead: joining an
2388
+ * unknown table expression again produces the same type, so the compiler
2389
+ * recognises the pair it is already comparing and stops immediately.
2390
+ */
2391
+ type JoinResultForUnknownTable<O> = SelectQueryBuilder<any, any, O>;
2392
+ export type SelectQueryBuilderWithInnerJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TableExpression<DB, TB> extends TE ? JoinResultForUnknownTable<O> : TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? InnerJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? SelectQueryBuilder<DB, TB | TE, O> : TE extends AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : never;
2371
2393
  type InnerJoinedBuilder<DB, TB extends keyof DB, O, A extends string, R> = A extends keyof DB ? SelectQueryBuilder<InnerJoinedDB<DB, A, R>, TB | A, O> : SelectQueryBuilder<DB & ShallowRecord<A, R>, TB | A, O>;
2372
2394
  type InnerJoinedDB<DB, A extends string, R> = DrainOuterGeneric<{
2373
2395
  [C in keyof DB | A]: C extends A ? R : C extends keyof DB ? DB[C] : never;
2374
2396
  }>;
2375
- export type SelectQueryBuilderWithLeftJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? LeftJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? LeftJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : never;
2397
+ export type SelectQueryBuilderWithLeftJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TableExpression<DB, TB> extends TE ? JoinResultForUnknownTable<O> : TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? LeftJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? LeftJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : never;
2376
2398
  type LeftJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = A extends keyof DB ? SelectQueryBuilder<LeftJoinedDB<DB, A, R>, TB | A, O> : SelectQueryBuilder<DB & ShallowRecord<A, Nullable<R>>, TB | A, O>;
2377
2399
  type LeftJoinedDB<DB, A extends keyof any, R> = DrainOuterGeneric<{
2378
2400
  [C in keyof DB | A]: C extends A ? Nullable<R> : C extends keyof DB ? DB[C] : never;
2379
2401
  }>;
2380
- export type SelectQueryBuilderWithRightJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? RightJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? RightJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : never;
2402
+ export type SelectQueryBuilderWithRightJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TableExpression<DB, TB> extends TE ? JoinResultForUnknownTable<O> : TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? RightJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? RightJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : never;
2381
2403
  type RightJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = SelectQueryBuilder<RightJoinedDB<DB, TB, A, R>, TB | A, O>;
2382
2404
  type RightJoinedDB<DB, TB extends keyof DB, A extends keyof any, R> = DrainOuterGeneric<{
2383
2405
  [C in keyof DB | A]: C extends A ? R : C extends TB ? Nullable<DB[C]> : C extends keyof DB ? DB[C] : never;
2384
2406
  }>;
2385
- export type SelectQueryBuilderWithFullJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? OuterJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? OuterJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : never;
2407
+ export type SelectQueryBuilderWithFullJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TableExpression<DB, TB> extends TE ? JoinResultForUnknownTable<O> : TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? OuterJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? OuterJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : never;
2386
2408
  type OuterJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = SelectQueryBuilder<OuterJoinedBuilderDB<DB, TB, A, R>, TB | A, O>;
2387
2409
  type OuterJoinedBuilderDB<DB, TB extends keyof DB, A extends keyof any, R> = DrainOuterGeneric<{
2388
2410
  [C in keyof DB | A]: C extends A ? Nullable<R> : C extends TB ? Nullable<DB[C]> : C extends keyof DB ? DB[C] : never;
@@ -1148,7 +1148,7 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
1148
1148
  * }>
1149
1149
  * ```
1150
1150
  */
1151
- $if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<any, any, any, O2>): O2 extends UpdateResult ? UpdateQueryBuilder<DB, UT, TB, UpdateResult> : O2 extends O & infer E ? UpdateQueryBuilder<DB, UT, TB, O & Partial<E>> : UpdateQueryBuilder<DB, UT, TB, Partial<O2>>;
1151
+ $if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<any, any, any, O2>): unknown extends O2 ? UpdateQueryBuilder<any, any, any, O2> : O2 extends UpdateResult ? UpdateQueryBuilder<DB, UT, TB, UpdateResult> : O2 extends O & infer E ? UpdateQueryBuilder<DB, UT, TB, O & Partial<E>> : UpdateQueryBuilder<DB, UT, TB, Partial<O2>>;
1152
1152
  /**
1153
1153
  * Change the output type of the query.
1154
1154
  *
@@ -260,6 +260,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
260
260
  this.append(' ');
261
261
  this.visitNode(node.where);
262
262
  }
263
+ if (node.returning) {
264
+ this.append(' ');
265
+ this.visitNode(node.returning);
266
+ }
263
267
  if (node.orderBy) {
264
268
  this.append(' ');
265
269
  this.visitNode(node.orderBy);
@@ -268,10 +272,6 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
268
272
  this.append(' ');
269
273
  this.visitNode(node.limit);
270
274
  }
271
- if (node.returning) {
272
- this.append(' ');
273
- this.visitNode(node.returning);
274
- }
275
275
  if (wrapInParens) {
276
276
  this.append(')');
277
277
  }
@@ -9,7 +9,7 @@ export declare class CreateTypeBuilder implements OperationNodeSource, Compilabl
9
9
  constructor(props: CreateTypeBuilderProps);
10
10
  toOperationNode(): CreateTypeNode;
11
11
  /**
12
- * Creates an anum type.
12
+ * Creates an enum type.
13
13
  *
14
14
  * ### Examples
15
15
  *
@@ -10,7 +10,7 @@ export class CreateTypeBuilder {
10
10
  return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
11
11
  }
12
12
  /**
13
- * Creates an anum type.
13
+ * Creates an enum type.
14
14
  *
15
15
  * ### Examples
16
16
  *
@@ -170,7 +170,7 @@ export type Insertable<R> = DrainOuterGeneric<object & {
170
170
  * ```
171
171
  */
172
172
  export type Updateable<R> = DrainOuterGeneric<{
173
- [K in UpdateKeys<R>]?: UpdateType<R[K]>;
173
+ [K in UpdateKeys<R>]?: UpdateType<R[K]> | undefined;
174
174
  }>;
175
175
  /**
176
176
  * Wrap your column type to opt-out from dehydration by {@link ShallowDehydrateValue},