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.
- package/README.md +1 -1
- package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +3 -2
- package/dist/cjs/dialect/mssql/mssql-adapter.js +13 -7
- package/dist/cjs/dialect/mssql/mssql-driver.d.ts +3 -0
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +3 -0
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-adapter.js +13 -7
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +12 -2
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +6 -1
- package/dist/cjs/dialect/postgres/postgres-driver.js +35 -3
- package/dist/cjs/dialect/postgres/postgres-introspector.js +18 -12
- package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
- package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +3 -0
- package/dist/cjs/driver/aborted-transaction-error.d.ts +11 -0
- package/dist/cjs/driver/aborted-transaction-error.js +20 -0
- package/dist/cjs/driver/driver.d.ts +3 -0
- package/dist/cjs/driver/dummy-driver.d.ts +3 -0
- package/dist/cjs/driver/runtime-driver.d.ts +3 -0
- package/dist/cjs/helpers/mssql.d.ts +3 -1
- package/dist/cjs/helpers/mssql.js +4 -2
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/kysely.d.ts +55 -3
- package/dist/cjs/kysely.js +136 -54
- package/dist/cjs/migration/migrator.js +22 -19
- package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -2
- package/dist/cjs/query-builder/join-builder.d.ts +2 -2
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -2
- package/dist/cjs/query-builder/select-query-builder.d.ts +28 -6
- package/dist/cjs/query-builder/update-query-builder.d.ts +1 -1
- package/dist/cjs/query-compiler/default-query-compiler.js +4 -4
- package/dist/cjs/schema/create-type-builder.d.ts +1 -1
- package/dist/cjs/schema/create-type-builder.js +1 -1
- package/dist/cjs/util/column-type.d.ts +1 -1
- package/dist/esm/dialect/mssql/mssql-adapter.d.ts +3 -2
- package/dist/esm/dialect/mssql/mssql-adapter.js +13 -7
- package/dist/esm/dialect/mssql/mssql-driver.d.ts +3 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +3 -0
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-adapter.js +13 -7
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +12 -2
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +6 -1
- package/dist/esm/dialect/postgres/postgres-driver.js +35 -3
- package/dist/esm/dialect/postgres/postgres-introspector.js +18 -12
- package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
- package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +3 -0
- package/dist/esm/driver/aborted-transaction-error.d.ts +11 -0
- package/dist/esm/driver/aborted-transaction-error.js +16 -0
- package/dist/esm/driver/driver.d.ts +3 -0
- package/dist/esm/driver/dummy-driver.d.ts +3 -0
- package/dist/esm/driver/runtime-driver.d.ts +3 -0
- package/dist/esm/helpers/mssql.d.ts +3 -1
- package/dist/esm/helpers/mssql.js +4 -2
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/kysely.d.ts +55 -3
- package/dist/esm/kysely.js +136 -54
- package/dist/esm/migration/migrator.js +22 -19
- package/dist/esm/query-builder/delete-query-builder.d.ts +2 -2
- package/dist/esm/query-builder/join-builder.d.ts +2 -2
- package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -2
- package/dist/esm/query-builder/select-query-builder.d.ts +28 -6
- package/dist/esm/query-builder/update-query-builder.d.ts +1 -1
- package/dist/esm/query-compiler/default-query-compiler.js +4 -4
- package/dist/esm/schema/create-type-builder.d.ts +1 -1
- package/dist/esm/schema/create-type-builder.js +1 -1
- package/dist/esm/util/column-type.d.ts +1 -1
- package/package.json +36 -34
package/dist/cjs/kysely.js
CHANGED
|
@@ -9,6 +9,7 @@ const query_creator_js_1 = require("./query-creator.js");
|
|
|
9
9
|
const default_query_executor_js_1 = require("./query-executor/default-query-executor.js");
|
|
10
10
|
const object_utils_js_1 = require("./util/object-utils.js");
|
|
11
11
|
const runtime_driver_js_1 = require("./driver/runtime-driver.js");
|
|
12
|
+
const aborted_transaction_error_js_1 = require("./driver/aborted-transaction-error.js");
|
|
12
13
|
const single_connection_provider_js_1 = require("./driver/single-connection-provider.js");
|
|
13
14
|
const driver_js_1 = require("./driver/driver.js");
|
|
14
15
|
const function_module_js_1 = require("./query-builder/function-module.js");
|
|
@@ -27,7 +28,8 @@ Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
|
|
|
27
28
|
* The main Kysely class.
|
|
28
29
|
*
|
|
29
30
|
* You should create one instance of `Kysely` per database using the {@link Kysely}
|
|
30
|
-
* constructor.
|
|
31
|
+
* constructor. The dialect manages database connections and may use a shared
|
|
32
|
+
* pool or database instance.
|
|
31
33
|
*
|
|
32
34
|
* ### Examples
|
|
33
35
|
*
|
|
@@ -436,7 +438,16 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
436
438
|
/**
|
|
437
439
|
* Releases all resources and disconnects from the database.
|
|
438
440
|
*
|
|
439
|
-
*
|
|
441
|
+
* Call this when you are done using the `Kysely` instance and Kysely is
|
|
442
|
+
* responsible for closing its connections, pool, or database instance.
|
|
443
|
+
*
|
|
444
|
+
* If the pool or database instance is shared with other consumers or its
|
|
445
|
+
* lifecycle is managed elsewhere, skip this call. Let the application close it
|
|
446
|
+
* once all consumers, including Kysely, have finished. No separate cleanup of
|
|
447
|
+
* the `Kysely` instance is needed.
|
|
448
|
+
*
|
|
449
|
+
* The driver is initialized lazily. If initialization hasn't started, this
|
|
450
|
+
* method does nothing.
|
|
440
451
|
*/
|
|
441
452
|
async destroy() {
|
|
442
453
|
await this.#props.driver.destroy();
|
|
@@ -565,8 +576,10 @@ class TransactionBuilder {
|
|
|
565
576
|
const settings = { isolationLevel, accessMode };
|
|
566
577
|
(0, driver_js_1.validateTransactionSettings)(settings);
|
|
567
578
|
return this.#props.executor.provideConnection(async (connection) => {
|
|
568
|
-
const
|
|
569
|
-
|
|
579
|
+
const transactionState = {
|
|
580
|
+
status: 'active',
|
|
581
|
+
};
|
|
582
|
+
const executor = new NotCommittedOrRolledBackAssertingExecutor(this.#props.executor.withConnectionProvider(new single_connection_provider_js_1.SingleConnectionProvider(connection)), transactionState);
|
|
570
583
|
const transaction = new Transaction({
|
|
571
584
|
...kyselyProps,
|
|
572
585
|
executor,
|
|
@@ -577,13 +590,16 @@ class TransactionBuilder {
|
|
|
577
590
|
transactionBegun = true;
|
|
578
591
|
const result = await callback(transaction);
|
|
579
592
|
await this.#props.driver.commitTransaction(connection);
|
|
580
|
-
|
|
593
|
+
transactionState.status = 'committed';
|
|
581
594
|
return result;
|
|
582
595
|
}
|
|
583
596
|
catch (error) {
|
|
584
|
-
if (
|
|
597
|
+
if ((0, aborted_transaction_error_js_1.isAbortedTransactionError)(error)) {
|
|
598
|
+
transactionState.status = 'aborted';
|
|
599
|
+
}
|
|
600
|
+
else if (transactionBegun) {
|
|
585
601
|
await this.#props.driver.rollbackTransaction(connection);
|
|
586
|
-
|
|
602
|
+
transactionState.status = 'rolled back';
|
|
587
603
|
}
|
|
588
604
|
throw error;
|
|
589
605
|
}
|
|
@@ -613,10 +629,17 @@ class ControlledTransactionBuilder {
|
|
|
613
629
|
const settings = { isolationLevel, accessMode };
|
|
614
630
|
(0, driver_js_1.validateTransactionSettings)(settings);
|
|
615
631
|
const connection = await (0, provide_controlled_connection_js_1.provideControlledConnection)(this.#props.executor);
|
|
616
|
-
|
|
632
|
+
try {
|
|
633
|
+
await this.#props.driver.beginTransaction(connection.connection, settings);
|
|
634
|
+
}
|
|
635
|
+
catch (error) {
|
|
636
|
+
connection.release();
|
|
637
|
+
throw error;
|
|
638
|
+
}
|
|
617
639
|
return new ControlledTransaction({
|
|
618
640
|
...props,
|
|
619
641
|
connection,
|
|
642
|
+
transactionState: { status: 'active' },
|
|
620
643
|
executor: this.#props.executor.withConnectionProvider(new single_connection_provider_js_1.SingleConnectionProvider(connection.connection)),
|
|
621
644
|
});
|
|
622
645
|
}
|
|
@@ -625,29 +648,38 @@ exports.ControlledTransactionBuilder = ControlledTransactionBuilder;
|
|
|
625
648
|
class ControlledTransaction extends Transaction {
|
|
626
649
|
#props;
|
|
627
650
|
#compileQuery;
|
|
628
|
-
#state;
|
|
629
651
|
constructor(props) {
|
|
630
|
-
const
|
|
631
|
-
|
|
652
|
+
const controlledState = props.transactionState ?? {
|
|
653
|
+
status: 'active',
|
|
654
|
+
};
|
|
655
|
+
const controlledProps = {
|
|
632
656
|
...props,
|
|
633
|
-
|
|
657
|
+
transactionState: controlledState,
|
|
658
|
+
executor: new NotCommittedOrRolledBackAssertingExecutor(props.executor, controlledState),
|
|
634
659
|
};
|
|
635
|
-
const { connection, ...transactionProps } =
|
|
660
|
+
const { connection, transactionState, ...transactionProps } = controlledProps;
|
|
636
661
|
super(transactionProps);
|
|
637
|
-
this.#props = (0, object_utils_js_1.freeze)(
|
|
638
|
-
this.#state = state;
|
|
662
|
+
this.#props = (0, object_utils_js_1.freeze)(controlledProps);
|
|
639
663
|
const queryId = (0, query_id_js_1.createQueryId)();
|
|
640
|
-
this.#compileQuery = (node) =>
|
|
664
|
+
this.#compileQuery = (node) => controlledProps.executor.compileQuery(node, queryId);
|
|
641
665
|
}
|
|
642
666
|
get isCommitted() {
|
|
643
|
-
return this.#
|
|
667
|
+
return this.#props.transactionState.status === 'committed';
|
|
644
668
|
}
|
|
645
669
|
get isRolledBack() {
|
|
646
|
-
return this.#
|
|
670
|
+
return (this.#props.transactionState.status === 'rolled back' ||
|
|
671
|
+
this.#props.transactionState.status === 'aborted');
|
|
647
672
|
}
|
|
648
673
|
/**
|
|
649
674
|
* Commits the transaction.
|
|
650
675
|
*
|
|
676
|
+
* If the database rolls back instead of committing, marks the transaction
|
|
677
|
+
* as rolled back, releases the connection and throws {@link AbortedTransactionError}.
|
|
678
|
+
*
|
|
679
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
680
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
681
|
+
* exit the loop before committing.
|
|
682
|
+
*
|
|
651
683
|
* See {@link rollback}.
|
|
652
684
|
*
|
|
653
685
|
* ### Examples
|
|
@@ -670,16 +702,36 @@ class ControlledTransaction extends Transaction {
|
|
|
670
702
|
* ```
|
|
671
703
|
*/
|
|
672
704
|
commit() {
|
|
673
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
674
705
|
return new Command(async () => {
|
|
675
|
-
await this.#props.
|
|
676
|
-
|
|
677
|
-
|
|
706
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
707
|
+
try {
|
|
708
|
+
await this.#props.driver.commitTransaction(connection);
|
|
709
|
+
}
|
|
710
|
+
catch (error) {
|
|
711
|
+
if ((0, aborted_transaction_error_js_1.isAbortedTransactionError)(error)) {
|
|
712
|
+
this.#props.transactionState.status = 'aborted';
|
|
713
|
+
this.#props.connection.release();
|
|
714
|
+
}
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
this.#props.transactionState.status = 'committed';
|
|
718
|
+
this.#props.connection.release();
|
|
719
|
+
});
|
|
678
720
|
});
|
|
679
721
|
}
|
|
680
722
|
/**
|
|
681
723
|
* Rolls back the transaction.
|
|
682
724
|
*
|
|
725
|
+
* If a commit already reported that the database rolled back the transaction,
|
|
726
|
+
* succeeds without sending another rollback command.
|
|
727
|
+
*
|
|
728
|
+
* The connection is released even if the rollback fails, so the transaction
|
|
729
|
+
* cannot be used afterwards in either case.
|
|
730
|
+
*
|
|
731
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
732
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
733
|
+
* exit the loop before rolling back.
|
|
734
|
+
*
|
|
683
735
|
* See {@link commit} and {@link rollbackToSavepoint}.
|
|
684
736
|
*
|
|
685
737
|
* ### Examples
|
|
@@ -702,16 +754,29 @@ class ControlledTransaction extends Transaction {
|
|
|
702
754
|
* ```
|
|
703
755
|
*/
|
|
704
756
|
rollback() {
|
|
705
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
706
757
|
return new Command(async () => {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
758
|
+
if (this.#props.transactionState.status === 'aborted') {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
762
|
+
// A failed rollback can't be meaningfully retried (e.g. dead connection).
|
|
763
|
+
try {
|
|
764
|
+
await this.#props.driver.rollbackTransaction(connection);
|
|
765
|
+
}
|
|
766
|
+
finally {
|
|
767
|
+
this.#props.transactionState.status = 'rolled back';
|
|
768
|
+
this.#props.connection.release();
|
|
769
|
+
}
|
|
770
|
+
});
|
|
710
771
|
});
|
|
711
772
|
}
|
|
712
773
|
/**
|
|
713
774
|
* Creates a savepoint with a given name.
|
|
714
775
|
*
|
|
776
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
777
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
778
|
+
* exit the loop before creating a savepoint.
|
|
779
|
+
*
|
|
715
780
|
* See {@link rollbackToSavepoint} and {@link releaseSavepoint}.
|
|
716
781
|
*
|
|
717
782
|
* For a type-safe experience, you should use the returned instance from now on.
|
|
@@ -739,15 +804,20 @@ class ControlledTransaction extends Transaction {
|
|
|
739
804
|
* ```
|
|
740
805
|
*/
|
|
741
806
|
savepoint(savepointName) {
|
|
742
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
743
807
|
return new Command(async () => {
|
|
744
|
-
await this.#props.
|
|
808
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
809
|
+
await this.#props.driver.savepoint?.(connection, savepointName, this.#compileQuery);
|
|
810
|
+
});
|
|
745
811
|
return new ControlledTransaction({ ...this.#props });
|
|
746
812
|
});
|
|
747
813
|
}
|
|
748
814
|
/**
|
|
749
815
|
* Rolls back to a savepoint with a given name.
|
|
750
816
|
*
|
|
817
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
818
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
819
|
+
* exit the loop before rolling back to a savepoint.
|
|
820
|
+
*
|
|
751
821
|
* See {@link savepoint} and {@link releaseSavepoint}.
|
|
752
822
|
*
|
|
753
823
|
* You must use the same instance returned by {@link savepoint}, or
|
|
@@ -776,15 +846,20 @@ class ControlledTransaction extends Transaction {
|
|
|
776
846
|
* ```
|
|
777
847
|
*/
|
|
778
848
|
rollbackToSavepoint(savepointName) {
|
|
779
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
780
849
|
return new Command(async () => {
|
|
781
|
-
await this.#props.
|
|
850
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
851
|
+
await this.#props.driver.rollbackToSavepoint?.(connection, savepointName, this.#compileQuery);
|
|
852
|
+
});
|
|
782
853
|
return new ControlledTransaction({ ...this.#props });
|
|
783
854
|
});
|
|
784
855
|
}
|
|
785
856
|
/**
|
|
786
857
|
* Releases a savepoint with a given name.
|
|
787
858
|
*
|
|
859
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
860
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
861
|
+
* exit the loop before releasing a savepoint.
|
|
862
|
+
*
|
|
788
863
|
* See {@link savepoint} and {@link rollbackToSavepoint}.
|
|
789
864
|
*
|
|
790
865
|
* You must use the same instance returned by {@link savepoint}, or
|
|
@@ -818,9 +893,10 @@ class ControlledTransaction extends Transaction {
|
|
|
818
893
|
* ```
|
|
819
894
|
*/
|
|
820
895
|
releaseSavepoint(savepointName) {
|
|
821
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
822
896
|
return new Command(async () => {
|
|
823
|
-
await this.#props.
|
|
897
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
898
|
+
await this.#props.driver.releaseSavepoint?.(connection, savepointName, this.#compileQuery);
|
|
899
|
+
});
|
|
824
900
|
return new ControlledTransaction({ ...this.#props });
|
|
825
901
|
});
|
|
826
902
|
}
|
|
@@ -860,11 +936,11 @@ class Command {
|
|
|
860
936
|
}
|
|
861
937
|
}
|
|
862
938
|
exports.Command = Command;
|
|
863
|
-
function assertNotCommittedOrRolledBack(
|
|
864
|
-
if (
|
|
939
|
+
function assertNotCommittedOrRolledBack(transactionState) {
|
|
940
|
+
if (transactionState.status === 'committed') {
|
|
865
941
|
throw new Error('Transaction is already committed');
|
|
866
942
|
}
|
|
867
|
-
if (
|
|
943
|
+
if (transactionState.status !== 'active') {
|
|
868
944
|
throw new Error('Transaction is already rolled back');
|
|
869
945
|
}
|
|
870
946
|
}
|
|
@@ -876,15 +952,13 @@ function assertNotCommittedOrRolledBack(state) {
|
|
|
876
952
|
*/
|
|
877
953
|
class NotCommittedOrRolledBackAssertingExecutor {
|
|
878
954
|
#executor;
|
|
879
|
-
#
|
|
880
|
-
constructor(executor,
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
}
|
|
887
|
-
this.#state = state;
|
|
955
|
+
#transactionState;
|
|
956
|
+
constructor(executor, transactionState) {
|
|
957
|
+
this.#executor =
|
|
958
|
+
executor instanceof NotCommittedOrRolledBackAssertingExecutor
|
|
959
|
+
? executor.#executor
|
|
960
|
+
: executor;
|
|
961
|
+
this.#transactionState = transactionState;
|
|
888
962
|
}
|
|
889
963
|
get adapter() {
|
|
890
964
|
return this.#executor.adapter;
|
|
@@ -899,29 +973,37 @@ class NotCommittedOrRolledBackAssertingExecutor {
|
|
|
899
973
|
return this.#executor.compileQuery(node, queryId);
|
|
900
974
|
}
|
|
901
975
|
provideConnection(consumer) {
|
|
902
|
-
|
|
976
|
+
assertNotCommittedOrRolledBack(this.#transactionState);
|
|
977
|
+
return this.#executor.provideConnection(async (connection) => {
|
|
978
|
+
assertNotCommittedOrRolledBack(this.#transactionState);
|
|
979
|
+
return await consumer(connection);
|
|
980
|
+
});
|
|
903
981
|
}
|
|
904
982
|
executeQuery(compiledQuery) {
|
|
905
|
-
assertNotCommittedOrRolledBack(this.#
|
|
906
|
-
return this.#executor
|
|
983
|
+
assertNotCommittedOrRolledBack(this.#transactionState);
|
|
984
|
+
return this.#executor
|
|
985
|
+
.withConnectionProvider(this)
|
|
986
|
+
.executeQuery(compiledQuery);
|
|
907
987
|
}
|
|
908
988
|
stream(compiledQuery, chunkSize) {
|
|
909
|
-
assertNotCommittedOrRolledBack(this.#
|
|
910
|
-
return this.#executor
|
|
989
|
+
assertNotCommittedOrRolledBack(this.#transactionState);
|
|
990
|
+
return this.#executor
|
|
991
|
+
.withConnectionProvider(this)
|
|
992
|
+
.stream(compiledQuery, chunkSize);
|
|
911
993
|
}
|
|
912
994
|
withConnectionProvider(connectionProvider) {
|
|
913
|
-
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withConnectionProvider(connectionProvider), this.#
|
|
995
|
+
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withConnectionProvider(connectionProvider), this.#transactionState);
|
|
914
996
|
}
|
|
915
997
|
withPlugin(plugin) {
|
|
916
|
-
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugin(plugin), this.#
|
|
998
|
+
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugin(plugin), this.#transactionState);
|
|
917
999
|
}
|
|
918
1000
|
withPlugins(plugins) {
|
|
919
|
-
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugins(plugins), this.#
|
|
1001
|
+
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPlugins(plugins), this.#transactionState);
|
|
920
1002
|
}
|
|
921
1003
|
withPluginAtFront(plugin) {
|
|
922
|
-
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPluginAtFront(plugin), this.#
|
|
1004
|
+
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withPluginAtFront(plugin), this.#transactionState);
|
|
923
1005
|
}
|
|
924
1006
|
withoutPlugins() {
|
|
925
|
-
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withoutPlugins(), this.#
|
|
1007
|
+
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withoutPlugins(), this.#transactionState);
|
|
926
1008
|
}
|
|
927
1009
|
}
|
|
@@ -412,34 +412,37 @@ class Migrator {
|
|
|
412
412
|
lockTableSchema: this.#props.migrationTableSchema,
|
|
413
413
|
});
|
|
414
414
|
const run = async (db) => {
|
|
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: [] };
|
|
430
|
+
};
|
|
431
|
+
const runWithLock = async (db, cb) => {
|
|
415
432
|
try {
|
|
416
433
|
await adapter.acquireMigrationLock(db, lockOptions);
|
|
417
|
-
|
|
418
|
-
if (state.migrations.length === 0) {
|
|
419
|
-
return { results: [] };
|
|
420
|
-
}
|
|
421
|
-
const { direction, step } = getMigrationDirectionAndStep(state);
|
|
422
|
-
if (step <= 0) {
|
|
423
|
-
return { results: [] };
|
|
424
|
-
}
|
|
425
|
-
if (direction === 'Down') {
|
|
426
|
-
return await this.#migrateDown(db, state, step);
|
|
427
|
-
}
|
|
428
|
-
else if (direction === 'Up') {
|
|
429
|
-
return await this.#migrateUp(db, state, step);
|
|
430
|
-
}
|
|
431
|
-
return { results: [] };
|
|
434
|
+
return await cb(db);
|
|
432
435
|
}
|
|
433
436
|
finally {
|
|
434
437
|
await adapter.releaseMigrationLock(db, lockOptions);
|
|
435
438
|
}
|
|
436
439
|
};
|
|
437
440
|
if (adapter.supportsTransactionalDdl && !this.#props.disableTransactions) {
|
|
438
|
-
return this.#props.db
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
return this.#props.db.connection().execute(run);
|
|
441
|
+
return this.#props.db
|
|
442
|
+
.connection()
|
|
443
|
+
.execute((db) => runWithLock(db, (db) => db.transaction().execute((trx) => run(trx))));
|
|
442
444
|
}
|
|
445
|
+
return this.#props.db.connection().execute((db) => runWithLock(db, run));
|
|
443
446
|
}
|
|
444
447
|
async #getState(db) {
|
|
445
448
|
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
|
|
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
|
|
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
|
|
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<
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
*
|
|
@@ -262,6 +262,10 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
262
262
|
this.append(' ');
|
|
263
263
|
this.visitNode(node.where);
|
|
264
264
|
}
|
|
265
|
+
if (node.returning) {
|
|
266
|
+
this.append(' ');
|
|
267
|
+
this.visitNode(node.returning);
|
|
268
|
+
}
|
|
265
269
|
if (node.orderBy) {
|
|
266
270
|
this.append(' ');
|
|
267
271
|
this.visitNode(node.orderBy);
|
|
@@ -270,10 +274,6 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
270
274
|
this.append(' ');
|
|
271
275
|
this.visitNode(node.limit);
|
|
272
276
|
}
|
|
273
|
-
if (node.returning) {
|
|
274
|
-
this.append(' ');
|
|
275
|
-
this.visitNode(node.returning);
|
|
276
|
-
}
|
|
277
277
|
if (wrapInParens) {
|
|
278
278
|
this.append(')');
|
|
279
279
|
}
|
|
@@ -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},
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Kysely } from '../../kysely.js';
|
|
2
2
|
import { DialectAdapterBase } from '../dialect-adapter-base.js';
|
|
3
|
+
import type { MigrationLockOptions } from '../dialect-adapter.js';
|
|
3
4
|
export declare class MssqlAdapter extends DialectAdapterBase {
|
|
4
5
|
/**
|
|
5
6
|
* Whether or not this dialect supports `if not exists` in creation of tables/schemas/views/etc.
|
|
@@ -70,7 +71,7 @@ export declare class MssqlAdapter extends DialectAdapterBase {
|
|
|
70
71
|
* `db` is a single connection (session) that will be used to execute the
|
|
71
72
|
* migrations.
|
|
72
73
|
*/
|
|
73
|
-
acquireMigrationLock(db: Kysely<any
|
|
74
|
+
acquireMigrationLock(db: Kysely<any>, _opt: MigrationLockOptions): Promise<void>;
|
|
74
75
|
/**
|
|
75
76
|
* Releases the migration lock. See {@link acquireMigrationLock}.
|
|
76
77
|
*
|
|
@@ -79,5 +80,5 @@ export declare class MssqlAdapter extends DialectAdapterBase {
|
|
|
79
80
|
* is a single connection (session) that was used to execute the migrations
|
|
80
81
|
* and the `acquireMigrationLock` call.
|
|
81
82
|
*/
|
|
82
|
-
releaseMigrationLock(): Promise<void>;
|
|
83
|
+
releaseMigrationLock(db: Kysely<any>, _opt: MigrationLockOptions): Promise<void>;
|
|
83
84
|
}
|