kysely 0.28.16 → 0.28.18

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 (66) 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/mysql/mysql-dialect-config.d.ts +10 -0
  5. package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +6 -0
  6. package/dist/cjs/dialect/mysql/mysql-query-compiler.js +10 -1
  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 +10 -0
  10. package/dist/cjs/dialect/postgres/postgres-driver.js +5 -0
  11. package/dist/cjs/dialect/postgres/postgres-introspector.js +18 -12
  12. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
  13. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +1 -0
  14. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +4 -0
  15. package/dist/cjs/helpers/mssql.d.ts +3 -1
  16. package/dist/cjs/helpers/mssql.js +4 -2
  17. package/dist/cjs/kysely.d.ts +50 -3
  18. package/dist/cjs/kysely.js +90 -30
  19. package/dist/cjs/migration/migrator.js +22 -19
  20. package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -6
  21. package/dist/cjs/query-builder/insert-query-builder.d.ts +0 -4
  22. package/dist/cjs/query-builder/join-builder.d.ts +2 -2
  23. package/dist/cjs/query-builder/json-path-builder.js +8 -0
  24. package/dist/cjs/query-builder/merge-query-builder.d.ts +0 -8
  25. package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -2
  26. package/dist/cjs/query-builder/returning-interface.d.ts +0 -4
  27. package/dist/cjs/query-builder/select-query-builder.d.ts +28 -6
  28. package/dist/cjs/query-builder/update-query-builder.d.ts +1 -5
  29. package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -0
  30. package/dist/cjs/query-compiler/default-query-compiler.js +16 -8
  31. package/dist/cjs/schema/create-type-builder.d.ts +1 -1
  32. package/dist/cjs/schema/create-type-builder.js +1 -1
  33. package/dist/cjs/util/column-type.d.ts +1 -1
  34. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +3 -2
  35. package/dist/esm/dialect/mssql/mssql-adapter.js +13 -7
  36. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +10 -0
  37. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +6 -0
  38. package/dist/esm/dialect/mysql/mysql-query-compiler.js +10 -1
  39. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +1 -1
  40. package/dist/esm/dialect/postgres/postgres-adapter.js +13 -7
  41. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +10 -0
  42. package/dist/esm/dialect/postgres/postgres-driver.js +5 -0
  43. package/dist/esm/dialect/postgres/postgres-introspector.js +18 -12
  44. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
  45. package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +1 -0
  46. package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +4 -0
  47. package/dist/esm/helpers/mssql.d.ts +3 -1
  48. package/dist/esm/helpers/mssql.js +4 -2
  49. package/dist/esm/kysely.d.ts +50 -3
  50. package/dist/esm/kysely.js +90 -30
  51. package/dist/esm/migration/migrator.js +22 -19
  52. package/dist/esm/query-builder/delete-query-builder.d.ts +2 -6
  53. package/dist/esm/query-builder/insert-query-builder.d.ts +0 -4
  54. package/dist/esm/query-builder/join-builder.d.ts +2 -2
  55. package/dist/esm/query-builder/json-path-builder.js +8 -0
  56. package/dist/esm/query-builder/merge-query-builder.d.ts +0 -8
  57. package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -2
  58. package/dist/esm/query-builder/returning-interface.d.ts +0 -4
  59. package/dist/esm/query-builder/select-query-builder.d.ts +28 -6
  60. package/dist/esm/query-builder/update-query-builder.d.ts +1 -5
  61. package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -0
  62. package/dist/esm/query-compiler/default-query-compiler.js +16 -8
  63. package/dist/esm/schema/create-type-builder.d.ts +1 -1
  64. package/dist/esm/schema/create-type-builder.js +1 -1
  65. package/dist/esm/util/column-type.d.ts +1 -1
  66. package/package.json +36 -34
@@ -24,7 +24,8 @@ Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
24
24
  * The main Kysely class.
25
25
  *
26
26
  * You should create one instance of `Kysely` per database using the {@link Kysely}
27
- * constructor. Each `Kysely` instance maintains its own connection pool.
27
+ * constructor. The dialect manages database connections and may use a shared
28
+ * pool or database instance.
28
29
  *
29
30
  * ### Examples
30
31
  *
@@ -433,7 +434,16 @@ export class Kysely extends QueryCreator {
433
434
  /**
434
435
  * Releases all resources and disconnects from the database.
435
436
  *
436
- * You need to call this when you are done using the `Kysely` instance.
437
+ * Call this when you are done using the `Kysely` instance and Kysely is
438
+ * responsible for closing its connections, pool, or database instance.
439
+ *
440
+ * If the pool or database instance is shared with other consumers or its
441
+ * lifecycle is managed elsewhere, skip this call. Let the application close it
442
+ * once all consumers, including Kysely, have finished. No separate cleanup of
443
+ * the `Kysely` instance is needed.
444
+ *
445
+ * The driver is initialized lazily. If initialization hasn't started, this
446
+ * method does nothing.
437
447
  */
438
448
  async destroy() {
439
449
  await this.#props.driver.destroy();
@@ -606,10 +616,17 @@ export class ControlledTransactionBuilder {
606
616
  const settings = { isolationLevel, accessMode };
607
617
  validateTransactionSettings(settings);
608
618
  const connection = await provideControlledConnection(this.#props.executor);
609
- await this.#props.driver.beginTransaction(connection.connection, settings);
619
+ try {
620
+ await this.#props.driver.beginTransaction(connection.connection, settings);
621
+ }
622
+ catch (error) {
623
+ connection.release();
624
+ throw error;
625
+ }
610
626
  return new ControlledTransaction({
611
627
  ...props,
612
628
  connection,
629
+ state: { isCommitted: false, isRolledBack: false },
613
630
  executor: this.#props.executor.withConnectionProvider(new SingleConnectionProvider(connection.connection)),
614
631
  });
615
632
  }
@@ -617,29 +634,35 @@ export class ControlledTransactionBuilder {
617
634
  export class ControlledTransaction extends Transaction {
618
635
  #props;
619
636
  #compileQuery;
620
- #state;
621
637
  constructor(props) {
622
- const state = { isCommitted: false, isRolledBack: false };
623
- props = {
638
+ const transactionState = props.state ?? {
639
+ isCommitted: false,
640
+ isRolledBack: false,
641
+ };
642
+ const controlledProps = {
624
643
  ...props,
625
- executor: new NotCommittedOrRolledBackAssertingExecutor(props.executor, state),
644
+ state: transactionState,
645
+ executor: new NotCommittedOrRolledBackAssertingExecutor(props.executor, transactionState),
626
646
  };
627
- const { connection, ...transactionProps } = props;
647
+ const { connection, state, ...transactionProps } = controlledProps;
628
648
  super(transactionProps);
629
- this.#props = freeze(props);
630
- this.#state = state;
649
+ this.#props = freeze(controlledProps);
631
650
  const queryId = createQueryId();
632
- this.#compileQuery = (node) => props.executor.compileQuery(node, queryId);
651
+ this.#compileQuery = (node) => controlledProps.executor.compileQuery(node, queryId);
633
652
  }
634
653
  get isCommitted() {
635
- return this.#state.isCommitted;
654
+ return this.#props.state.isCommitted;
636
655
  }
637
656
  get isRolledBack() {
638
- return this.#state.isRolledBack;
657
+ return this.#props.state.isRolledBack;
639
658
  }
640
659
  /**
641
660
  * Commits the transaction.
642
661
  *
662
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
663
+ * before awaiting this command. When consuming a stream with `for await`,
664
+ * exit the loop before committing.
665
+ *
643
666
  * See {@link rollback}.
644
667
  *
645
668
  * ### Examples
@@ -662,16 +685,24 @@ export class ControlledTransaction extends Transaction {
662
685
  * ```
663
686
  */
664
687
  commit() {
665
- assertNotCommittedOrRolledBack(this.#state);
666
688
  return new Command(async () => {
667
- await this.#props.driver.commitTransaction(this.#props.connection.connection);
668
- this.#state.isCommitted = true;
669
- this.#props.connection.release();
689
+ await this.#props.executor.provideConnection(async (connection) => {
690
+ await this.#props.driver.commitTransaction(connection);
691
+ this.#props.state.isCommitted = true;
692
+ this.#props.connection.release();
693
+ });
670
694
  });
671
695
  }
672
696
  /**
673
697
  * Rolls back the transaction.
674
698
  *
699
+ * The connection is released even if the rollback fails, so the transaction
700
+ * cannot be used afterwards in either case.
701
+ *
702
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
703
+ * before awaiting this command. When consuming a stream with `for await`,
704
+ * exit the loop before rolling back.
705
+ *
675
706
  * See {@link commit} and {@link rollbackToSavepoint}.
676
707
  *
677
708
  * ### Examples
@@ -694,16 +725,26 @@ export class ControlledTransaction extends Transaction {
694
725
  * ```
695
726
  */
696
727
  rollback() {
697
- assertNotCommittedOrRolledBack(this.#state);
698
728
  return new Command(async () => {
699
- await this.#props.driver.rollbackTransaction(this.#props.connection.connection);
700
- this.#state.isRolledBack = true;
701
- this.#props.connection.release();
729
+ await this.#props.executor.provideConnection(async (connection) => {
730
+ // A failed rollback can't be meaningfully retried (e.g. dead connection).
731
+ try {
732
+ await this.#props.driver.rollbackTransaction(connection);
733
+ }
734
+ finally {
735
+ this.#props.state.isRolledBack = true;
736
+ this.#props.connection.release();
737
+ }
738
+ });
702
739
  });
703
740
  }
704
741
  /**
705
742
  * Creates a savepoint with a given name.
706
743
  *
744
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
745
+ * before awaiting this command. When consuming a stream with `for await`,
746
+ * exit the loop before creating a savepoint.
747
+ *
707
748
  * See {@link rollbackToSavepoint} and {@link releaseSavepoint}.
708
749
  *
709
750
  * For a type-safe experience, you should use the returned instance from now on.
@@ -731,15 +772,20 @@ export class ControlledTransaction extends Transaction {
731
772
  * ```
732
773
  */
733
774
  savepoint(savepointName) {
734
- assertNotCommittedOrRolledBack(this.#state);
735
775
  return new Command(async () => {
736
- await this.#props.driver.savepoint?.(this.#props.connection.connection, savepointName, this.#compileQuery);
776
+ await this.#props.executor.provideConnection(async (connection) => {
777
+ await this.#props.driver.savepoint?.(connection, savepointName, this.#compileQuery);
778
+ });
737
779
  return new ControlledTransaction({ ...this.#props });
738
780
  });
739
781
  }
740
782
  /**
741
783
  * Rolls back to a savepoint with a given name.
742
784
  *
785
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
786
+ * before awaiting this command. When consuming a stream with `for await`,
787
+ * exit the loop before rolling back to a savepoint.
788
+ *
743
789
  * See {@link savepoint} and {@link releaseSavepoint}.
744
790
  *
745
791
  * You must use the same instance returned by {@link savepoint}, or
@@ -768,15 +814,20 @@ export class ControlledTransaction extends Transaction {
768
814
  * ```
769
815
  */
770
816
  rollbackToSavepoint(savepointName) {
771
- assertNotCommittedOrRolledBack(this.#state);
772
817
  return new Command(async () => {
773
- await this.#props.driver.rollbackToSavepoint?.(this.#props.connection.connection, savepointName, this.#compileQuery);
818
+ await this.#props.executor.provideConnection(async (connection) => {
819
+ await this.#props.driver.rollbackToSavepoint?.(connection, savepointName, this.#compileQuery);
820
+ });
774
821
  return new ControlledTransaction({ ...this.#props });
775
822
  });
776
823
  }
777
824
  /**
778
825
  * Releases a savepoint with a given name.
779
826
  *
827
+ * Waits for ongoing queries to finish. Streams must be exhausted or closed
828
+ * before awaiting this command. When consuming a stream with `for await`,
829
+ * exit the loop before releasing a savepoint.
830
+ *
780
831
  * See {@link savepoint} and {@link rollbackToSavepoint}.
781
832
  *
782
833
  * You must use the same instance returned by {@link savepoint}, or
@@ -810,9 +861,10 @@ export class ControlledTransaction extends Transaction {
810
861
  * ```
811
862
  */
812
863
  releaseSavepoint(savepointName) {
813
- assertNotCommittedOrRolledBack(this.#state);
814
864
  return new Command(async () => {
815
- await this.#props.driver.releaseSavepoint?.(this.#props.connection.connection, savepointName, this.#compileQuery);
865
+ await this.#props.executor.provideConnection(async (connection) => {
866
+ await this.#props.driver.releaseSavepoint?.(connection, savepointName, this.#compileQuery);
867
+ });
816
868
  return new ControlledTransaction({ ...this.#props });
817
869
  });
818
870
  }
@@ -889,15 +941,23 @@ class NotCommittedOrRolledBackAssertingExecutor {
889
941
  return this.#executor.compileQuery(node, queryId);
890
942
  }
891
943
  provideConnection(consumer) {
892
- return this.#executor.provideConnection(consumer);
944
+ assertNotCommittedOrRolledBack(this.#state);
945
+ return this.#executor.provideConnection(async (connection) => {
946
+ assertNotCommittedOrRolledBack(this.#state);
947
+ return await consumer(connection);
948
+ });
893
949
  }
894
950
  executeQuery(compiledQuery) {
895
951
  assertNotCommittedOrRolledBack(this.#state);
896
- return this.#executor.executeQuery(compiledQuery);
952
+ return this.#executor
953
+ .withConnectionProvider(this)
954
+ .executeQuery(compiledQuery);
897
955
  }
898
956
  stream(compiledQuery, chunkSize) {
899
957
  assertNotCommittedOrRolledBack(this.#state);
900
- return this.#executor.stream(compiledQuery, chunkSize);
958
+ return this.#executor
959
+ .withConnectionProvider(this)
960
+ .stream(compiledQuery, chunkSize);
901
961
  }
902
962
  withConnectionProvider(connectionProvider) {
903
963
  return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withConnectionProvider(connectionProvider), this.#state);
@@ -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();
@@ -653,10 +653,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
653
653
  * On supported databases like PostgreSQL, this method can be chained to
654
654
  * `insert`, `update`, `delete` and `merge` queries to return data.
655
655
  *
656
- * Note that on SQLite you need to give aliases for the expressions to avoid
657
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
658
- * For example `.returning('id as id')`.
659
- *
660
656
  * Also see the {@link returningAll} method.
661
657
  *
662
658
  * ### Examples
@@ -1043,7 +1039,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
1043
1039
  * delete from `pet` order by `created_at` limit ?
1044
1040
  * ```
1045
1041
  */
1046
- limit(limit: ValueExpression<DB, TB, number>): DeleteQueryBuilder<DB, TB, O>;
1042
+ limit<VE extends ValueExpression<DB, TB, number>>(limit: VE): DeleteQueryBuilder<DB, TB, O>;
1047
1043
  /**
1048
1044
  * This can be used to add any additional SQL to the end of the query.
1049
1045
  *
@@ -1126,7 +1122,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
1126
1122
  * }>
1127
1123
  * ```
1128
1124
  */
1129
- $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>>;
1130
1126
  /**
1131
1127
  * Change the output type of the query.
1132
1128
  *
@@ -752,10 +752,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
752
752
  * On supported databases like PostgreSQL, this method can be chained to
753
753
  * `insert`, `update`, `delete` and `merge` queries to return data.
754
754
  *
755
- * Note that on SQLite you need to give aliases for the expressions to avoid
756
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
757
- * For example `.returning('id as id')`.
758
- *
759
755
  * Also see the {@link returningAll} method.
760
756
  *
761
757
  * ### Examples
@@ -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
  */
@@ -7,6 +7,7 @@ import { JSONPathNode } from '../operation-node/json-path-node.js';
7
7
  import { JSONReferenceNode } from '../operation-node/json-reference-node.js';
8
8
  import { isOperationNodeSource } from '../operation-node/operation-node-source.js';
9
9
  import { ValueNode } from '../operation-node/value-node.js';
10
+ const HASH_NEGATIVE_INDEX_REGEX = /^#-\d+$/;
10
11
  export class JSONPathBuilder {
11
12
  #node;
12
13
  constructor(node) {
@@ -80,6 +81,13 @@ export class JSONPathBuilder {
80
81
  * ```
81
82
  */
82
83
  at(index) {
84
+ if ((typeof index !== 'number' && typeof index !== 'string') ||
85
+ (typeof index === 'number' && !Number.isInteger(index)) ||
86
+ (typeof index === 'string' &&
87
+ index !== 'last' &&
88
+ !HASH_NEGATIVE_INDEX_REGEX.test(index))) {
89
+ throw new Error(`Unexpected index value in .at(...): ${index}`);
90
+ }
83
91
  return this.#createBuilderWithPathLeg('ArrayLocation', index);
84
92
  }
85
93
  /**
@@ -130,10 +130,6 @@ export declare class MergeQueryBuilder<DB, TT extends keyof DB, O> implements Mu
130
130
  * On supported databases like PostgreSQL, this method can be chained to
131
131
  * `insert`, `update`, `delete` and `merge` queries to return data.
132
132
  *
133
- * Note that on SQLite you need to give aliases for the expressions to avoid
134
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
135
- * For example `.returning('id as id')`.
136
- *
137
133
  * Also see the {@link returningAll} method.
138
134
  *
139
135
  * ### Examples
@@ -527,10 +523,6 @@ export declare class WheneableMergeQueryBuilder<DB, TT extends keyof DB, ST exte
527
523
  * On supported databases like PostgreSQL, this method can be chained to
528
524
  * `insert`, `update`, `delete` and `merge` queries to return data.
529
525
  *
530
- * Note that on SQLite you need to give aliases for the expressions to avoid
531
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
532
- * For example `.returning('id as id')`.
533
- *
534
526
  * Also see the {@link returningAll} method.
535
527
  *
536
528
  * ### Examples
@@ -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;
@@ -8,10 +8,6 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
8
8
  * On supported databases like PostgreSQL, this method can be chained to
9
9
  * `insert`, `update`, `delete` and `merge` queries to return data.
10
10
  *
11
- * Note that on SQLite you need to give aliases for the expressions to avoid
12
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
13
- * For example `.returning('id as id')`.
14
- *
15
11
  * Also see the {@link returningAll} method.
16
12
  *
17
13
  * ### Examples
@@ -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;
@@ -850,10 +850,6 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
850
850
  * On supported databases like PostgreSQL, this method can be chained to
851
851
  * `insert`, `update`, `delete` and `merge` queries to return data.
852
852
  *
853
- * Note that on SQLite you need to give aliases for the expressions to avoid
854
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
855
- * For example `.returning('id as id')`.
856
- *
857
853
  * Also see the {@link returningAll} method.
858
854
  *
859
855
  * ### Examples
@@ -1152,7 +1148,7 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
1152
1148
  * }>
1153
1149
  * ```
1154
1150
  */
1155
- $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>>;
1156
1152
  /**
1157
1153
  * Change the output type of the query.
1158
1154
  *
@@ -222,6 +222,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
222
222
  protected getRightExplainOptionsWrapper(): string;
223
223
  protected sanitizeIdentifier(identifier: string): string;
224
224
  protected sanitizeStringLiteral(value: string): string;
225
+ protected sanitizeJSONPathMemberValue(value: string): string;
225
226
  protected addParameter(parameter: unknown): void;
226
227
  protected appendImmediateValue(value: unknown): void;
227
228
  protected appendStringLiteral(value: string): void;
@@ -11,6 +11,7 @@ import { SetOperationNode } from '../operation-node/set-operation-node.js';
11
11
  import { WhenNode } from '../operation-node/when-node.js';
12
12
  import { logOnce } from '../util/log-once.js';
13
13
  const LIT_WRAP_REGEX = /'/g;
14
+ const JSON_PATH_MEMBER_WRAP_REGEX = /['"]/g;
14
15
  export class DefaultQueryCompiler extends OperationNodeVisitor {
15
16
  #sql = '';
16
17
  #parameters = [];
@@ -259,6 +260,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
259
260
  this.append(' ');
260
261
  this.visitNode(node.where);
261
262
  }
263
+ if (node.returning) {
264
+ this.append(' ');
265
+ this.visitNode(node.returning);
266
+ }
262
267
  if (node.orderBy) {
263
268
  this.append(' ');
264
269
  this.visitNode(node.orderBy);
@@ -267,10 +272,6 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
267
272
  this.append(' ');
268
273
  this.visitNode(node.limit);
269
274
  }
270
- if (node.returning) {
271
- this.append(' ');
272
- this.visitNode(node.returning);
273
- }
274
275
  if (wrapInParens) {
275
276
  this.append(')');
276
277
  }
@@ -1166,13 +1167,17 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
1166
1167
  }
1167
1168
  visitJSONPathLeg(node) {
1168
1169
  const isArrayLocation = node.type === 'ArrayLocation';
1169
- this.append(isArrayLocation ? '[' : '.');
1170
- this.append(typeof node.value === 'string'
1171
- ? this.sanitizeStringLiteral(node.value)
1172
- : String(node.value));
1170
+ const value = String(node.value);
1173
1171
  if (isArrayLocation) {
1172
+ this.append('[');
1173
+ this.append(this.sanitizeStringLiteral(value));
1174
1174
  this.append(']');
1175
1175
  }
1176
+ else {
1177
+ this.append('."');
1178
+ this.append(this.sanitizeJSONPathMemberValue(value));
1179
+ this.append('"');
1180
+ }
1176
1181
  }
1177
1182
  visitJSONOperatorChain(node) {
1178
1183
  for (let i = 0, len = node.values.length; i < len; i++) {
@@ -1319,6 +1324,9 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
1319
1324
  sanitizeStringLiteral(value) {
1320
1325
  return value.replace(LIT_WRAP_REGEX, "''");
1321
1326
  }
1327
+ sanitizeJSONPathMemberValue(value) {
1328
+ return value.replace(JSON_PATH_MEMBER_WRAP_REGEX, (char) => char === "'" ? "''" : '\\"');
1329
+ }
1322
1330
  addParameter(parameter) {
1323
1331
  this.#parameters.push(parameter);
1324
1332
  }
@@ -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},