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.
- 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/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +6 -0
- package/dist/cjs/dialect/mysql/mysql-query-compiler.js +10 -1
- 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 +10 -0
- package/dist/cjs/dialect/postgres/postgres-driver.js +5 -0
- 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-query-compiler.d.ts +1 -0
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +4 -0
- package/dist/cjs/helpers/mssql.d.ts +3 -1
- package/dist/cjs/helpers/mssql.js +4 -2
- package/dist/cjs/kysely.d.ts +50 -3
- package/dist/cjs/kysely.js +90 -30
- package/dist/cjs/migration/migrator.js +22 -19
- package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -6
- package/dist/cjs/query-builder/insert-query-builder.d.ts +0 -4
- package/dist/cjs/query-builder/join-builder.d.ts +2 -2
- package/dist/cjs/query-builder/json-path-builder.js +8 -0
- package/dist/cjs/query-builder/merge-query-builder.d.ts +0 -8
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -2
- package/dist/cjs/query-builder/returning-interface.d.ts +0 -4
- package/dist/cjs/query-builder/select-query-builder.d.ts +28 -6
- package/dist/cjs/query-builder/update-query-builder.d.ts +1 -5
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +16 -8
- 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/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +6 -0
- package/dist/esm/dialect/mysql/mysql-query-compiler.js +10 -1
- 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 +10 -0
- package/dist/esm/dialect/postgres/postgres-driver.js +5 -0
- 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-query-compiler.d.ts +1 -0
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +4 -0
- package/dist/esm/helpers/mssql.d.ts +3 -1
- package/dist/esm/helpers/mssql.js +4 -2
- package/dist/esm/kysely.d.ts +50 -3
- package/dist/esm/kysely.js +90 -30
- package/dist/esm/migration/migrator.js +22 -19
- package/dist/esm/query-builder/delete-query-builder.d.ts +2 -6
- package/dist/esm/query-builder/insert-query-builder.d.ts +0 -4
- package/dist/esm/query-builder/join-builder.d.ts +2 -2
- package/dist/esm/query-builder/json-path-builder.js +8 -0
- package/dist/esm/query-builder/merge-query-builder.d.ts +0 -8
- package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -2
- package/dist/esm/query-builder/returning-interface.d.ts +0 -4
- package/dist/esm/query-builder/select-query-builder.d.ts +28 -6
- package/dist/esm/query-builder/update-query-builder.d.ts +1 -5
- package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -0
- package/dist/esm/query-compiler/default-query-compiler.js +16 -8
- 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
|
@@ -27,7 +27,8 @@ Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
|
|
|
27
27
|
* The main Kysely class.
|
|
28
28
|
*
|
|
29
29
|
* You should create one instance of `Kysely` per database using the {@link Kysely}
|
|
30
|
-
* constructor.
|
|
30
|
+
* constructor. The dialect manages database connections and may use a shared
|
|
31
|
+
* pool or database instance.
|
|
31
32
|
*
|
|
32
33
|
* ### Examples
|
|
33
34
|
*
|
|
@@ -436,7 +437,16 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
436
437
|
/**
|
|
437
438
|
* Releases all resources and disconnects from the database.
|
|
438
439
|
*
|
|
439
|
-
*
|
|
440
|
+
* Call this when you are done using the `Kysely` instance and Kysely is
|
|
441
|
+
* responsible for closing its connections, pool, or database instance.
|
|
442
|
+
*
|
|
443
|
+
* If the pool or database instance is shared with other consumers or its
|
|
444
|
+
* lifecycle is managed elsewhere, skip this call. Let the application close it
|
|
445
|
+
* once all consumers, including Kysely, have finished. No separate cleanup of
|
|
446
|
+
* the `Kysely` instance is needed.
|
|
447
|
+
*
|
|
448
|
+
* The driver is initialized lazily. If initialization hasn't started, this
|
|
449
|
+
* method does nothing.
|
|
440
450
|
*/
|
|
441
451
|
async destroy() {
|
|
442
452
|
await this.#props.driver.destroy();
|
|
@@ -613,10 +623,17 @@ class ControlledTransactionBuilder {
|
|
|
613
623
|
const settings = { isolationLevel, accessMode };
|
|
614
624
|
(0, driver_js_1.validateTransactionSettings)(settings);
|
|
615
625
|
const connection = await (0, provide_controlled_connection_js_1.provideControlledConnection)(this.#props.executor);
|
|
616
|
-
|
|
626
|
+
try {
|
|
627
|
+
await this.#props.driver.beginTransaction(connection.connection, settings);
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
connection.release();
|
|
631
|
+
throw error;
|
|
632
|
+
}
|
|
617
633
|
return new ControlledTransaction({
|
|
618
634
|
...props,
|
|
619
635
|
connection,
|
|
636
|
+
state: { isCommitted: false, isRolledBack: false },
|
|
620
637
|
executor: this.#props.executor.withConnectionProvider(new single_connection_provider_js_1.SingleConnectionProvider(connection.connection)),
|
|
621
638
|
});
|
|
622
639
|
}
|
|
@@ -625,29 +642,35 @@ exports.ControlledTransactionBuilder = ControlledTransactionBuilder;
|
|
|
625
642
|
class ControlledTransaction extends Transaction {
|
|
626
643
|
#props;
|
|
627
644
|
#compileQuery;
|
|
628
|
-
#state;
|
|
629
645
|
constructor(props) {
|
|
630
|
-
const
|
|
631
|
-
|
|
646
|
+
const transactionState = props.state ?? {
|
|
647
|
+
isCommitted: false,
|
|
648
|
+
isRolledBack: false,
|
|
649
|
+
};
|
|
650
|
+
const controlledProps = {
|
|
632
651
|
...props,
|
|
633
|
-
|
|
652
|
+
state: transactionState,
|
|
653
|
+
executor: new NotCommittedOrRolledBackAssertingExecutor(props.executor, transactionState),
|
|
634
654
|
};
|
|
635
|
-
const { connection, ...transactionProps } =
|
|
655
|
+
const { connection, state, ...transactionProps } = controlledProps;
|
|
636
656
|
super(transactionProps);
|
|
637
|
-
this.#props = (0, object_utils_js_1.freeze)(
|
|
638
|
-
this.#state = state;
|
|
657
|
+
this.#props = (0, object_utils_js_1.freeze)(controlledProps);
|
|
639
658
|
const queryId = (0, query_id_js_1.createQueryId)();
|
|
640
|
-
this.#compileQuery = (node) =>
|
|
659
|
+
this.#compileQuery = (node) => controlledProps.executor.compileQuery(node, queryId);
|
|
641
660
|
}
|
|
642
661
|
get isCommitted() {
|
|
643
|
-
return this.#state.isCommitted;
|
|
662
|
+
return this.#props.state.isCommitted;
|
|
644
663
|
}
|
|
645
664
|
get isRolledBack() {
|
|
646
|
-
return this.#state.isRolledBack;
|
|
665
|
+
return this.#props.state.isRolledBack;
|
|
647
666
|
}
|
|
648
667
|
/**
|
|
649
668
|
* Commits the transaction.
|
|
650
669
|
*
|
|
670
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
671
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
672
|
+
* exit the loop before committing.
|
|
673
|
+
*
|
|
651
674
|
* See {@link rollback}.
|
|
652
675
|
*
|
|
653
676
|
* ### Examples
|
|
@@ -670,16 +693,24 @@ class ControlledTransaction extends Transaction {
|
|
|
670
693
|
* ```
|
|
671
694
|
*/
|
|
672
695
|
commit() {
|
|
673
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
674
696
|
return new Command(async () => {
|
|
675
|
-
await this.#props.
|
|
676
|
-
|
|
677
|
-
|
|
697
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
698
|
+
await this.#props.driver.commitTransaction(connection);
|
|
699
|
+
this.#props.state.isCommitted = true;
|
|
700
|
+
this.#props.connection.release();
|
|
701
|
+
});
|
|
678
702
|
});
|
|
679
703
|
}
|
|
680
704
|
/**
|
|
681
705
|
* Rolls back the transaction.
|
|
682
706
|
*
|
|
707
|
+
* The connection is released even if the rollback fails, so the transaction
|
|
708
|
+
* cannot be used afterwards in either case.
|
|
709
|
+
*
|
|
710
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
711
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
712
|
+
* exit the loop before rolling back.
|
|
713
|
+
*
|
|
683
714
|
* See {@link commit} and {@link rollbackToSavepoint}.
|
|
684
715
|
*
|
|
685
716
|
* ### Examples
|
|
@@ -702,16 +733,26 @@ class ControlledTransaction extends Transaction {
|
|
|
702
733
|
* ```
|
|
703
734
|
*/
|
|
704
735
|
rollback() {
|
|
705
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
706
736
|
return new Command(async () => {
|
|
707
|
-
await this.#props.
|
|
708
|
-
|
|
709
|
-
|
|
737
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
738
|
+
// A failed rollback can't be meaningfully retried (e.g. dead connection).
|
|
739
|
+
try {
|
|
740
|
+
await this.#props.driver.rollbackTransaction(connection);
|
|
741
|
+
}
|
|
742
|
+
finally {
|
|
743
|
+
this.#props.state.isRolledBack = true;
|
|
744
|
+
this.#props.connection.release();
|
|
745
|
+
}
|
|
746
|
+
});
|
|
710
747
|
});
|
|
711
748
|
}
|
|
712
749
|
/**
|
|
713
750
|
* Creates a savepoint with a given name.
|
|
714
751
|
*
|
|
752
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
753
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
754
|
+
* exit the loop before creating a savepoint.
|
|
755
|
+
*
|
|
715
756
|
* See {@link rollbackToSavepoint} and {@link releaseSavepoint}.
|
|
716
757
|
*
|
|
717
758
|
* For a type-safe experience, you should use the returned instance from now on.
|
|
@@ -739,15 +780,20 @@ class ControlledTransaction extends Transaction {
|
|
|
739
780
|
* ```
|
|
740
781
|
*/
|
|
741
782
|
savepoint(savepointName) {
|
|
742
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
743
783
|
return new Command(async () => {
|
|
744
|
-
await this.#props.
|
|
784
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
785
|
+
await this.#props.driver.savepoint?.(connection, savepointName, this.#compileQuery);
|
|
786
|
+
});
|
|
745
787
|
return new ControlledTransaction({ ...this.#props });
|
|
746
788
|
});
|
|
747
789
|
}
|
|
748
790
|
/**
|
|
749
791
|
* Rolls back to a savepoint with a given name.
|
|
750
792
|
*
|
|
793
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
794
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
795
|
+
* exit the loop before rolling back to a savepoint.
|
|
796
|
+
*
|
|
751
797
|
* See {@link savepoint} and {@link releaseSavepoint}.
|
|
752
798
|
*
|
|
753
799
|
* You must use the same instance returned by {@link savepoint}, or
|
|
@@ -776,15 +822,20 @@ class ControlledTransaction extends Transaction {
|
|
|
776
822
|
* ```
|
|
777
823
|
*/
|
|
778
824
|
rollbackToSavepoint(savepointName) {
|
|
779
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
780
825
|
return new Command(async () => {
|
|
781
|
-
await this.#props.
|
|
826
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
827
|
+
await this.#props.driver.rollbackToSavepoint?.(connection, savepointName, this.#compileQuery);
|
|
828
|
+
});
|
|
782
829
|
return new ControlledTransaction({ ...this.#props });
|
|
783
830
|
});
|
|
784
831
|
}
|
|
785
832
|
/**
|
|
786
833
|
* Releases a savepoint with a given name.
|
|
787
834
|
*
|
|
835
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
836
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
837
|
+
* exit the loop before releasing a savepoint.
|
|
838
|
+
*
|
|
788
839
|
* See {@link savepoint} and {@link rollbackToSavepoint}.
|
|
789
840
|
*
|
|
790
841
|
* You must use the same instance returned by {@link savepoint}, or
|
|
@@ -818,9 +869,10 @@ class ControlledTransaction extends Transaction {
|
|
|
818
869
|
* ```
|
|
819
870
|
*/
|
|
820
871
|
releaseSavepoint(savepointName) {
|
|
821
|
-
assertNotCommittedOrRolledBack(this.#state);
|
|
822
872
|
return new Command(async () => {
|
|
823
|
-
await this.#props.
|
|
873
|
+
await this.#props.executor.provideConnection(async (connection) => {
|
|
874
|
+
await this.#props.driver.releaseSavepoint?.(connection, savepointName, this.#compileQuery);
|
|
875
|
+
});
|
|
824
876
|
return new ControlledTransaction({ ...this.#props });
|
|
825
877
|
});
|
|
826
878
|
}
|
|
@@ -899,15 +951,23 @@ class NotCommittedOrRolledBackAssertingExecutor {
|
|
|
899
951
|
return this.#executor.compileQuery(node, queryId);
|
|
900
952
|
}
|
|
901
953
|
provideConnection(consumer) {
|
|
902
|
-
|
|
954
|
+
assertNotCommittedOrRolledBack(this.#state);
|
|
955
|
+
return this.#executor.provideConnection(async (connection) => {
|
|
956
|
+
assertNotCommittedOrRolledBack(this.#state);
|
|
957
|
+
return await consumer(connection);
|
|
958
|
+
});
|
|
903
959
|
}
|
|
904
960
|
executeQuery(compiledQuery) {
|
|
905
961
|
assertNotCommittedOrRolledBack(this.#state);
|
|
906
|
-
return this.#executor
|
|
962
|
+
return this.#executor
|
|
963
|
+
.withConnectionProvider(this)
|
|
964
|
+
.executeQuery(compiledQuery);
|
|
907
965
|
}
|
|
908
966
|
stream(compiledQuery, chunkSize) {
|
|
909
967
|
assertNotCommittedOrRolledBack(this.#state);
|
|
910
|
-
return this.#executor
|
|
968
|
+
return this.#executor
|
|
969
|
+
.withConnectionProvider(this)
|
|
970
|
+
.stream(compiledQuery, chunkSize);
|
|
911
971
|
}
|
|
912
972
|
withConnectionProvider(connectionProvider) {
|
|
913
973
|
return new NotCommittedOrRolledBackAssertingExecutor(this.#executor.withConnectionProvider(connectionProvider), this.#state);
|
|
@@ -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();
|
|
@@ -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
|
|
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
|
|
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
|
*/
|
|
@@ -9,6 +9,7 @@ const json_path_node_js_1 = require("../operation-node/json-path-node.js");
|
|
|
9
9
|
const json_reference_node_js_1 = require("../operation-node/json-reference-node.js");
|
|
10
10
|
const operation_node_source_js_1 = require("../operation-node/operation-node-source.js");
|
|
11
11
|
const value_node_js_1 = require("../operation-node/value-node.js");
|
|
12
|
+
const HASH_NEGATIVE_INDEX_REGEX = /^#-\d+$/;
|
|
12
13
|
class JSONPathBuilder {
|
|
13
14
|
#node;
|
|
14
15
|
constructor(node) {
|
|
@@ -82,6 +83,13 @@ class JSONPathBuilder {
|
|
|
82
83
|
* ```
|
|
83
84
|
*/
|
|
84
85
|
at(index) {
|
|
86
|
+
if ((typeof index !== 'number' && typeof index !== 'string') ||
|
|
87
|
+
(typeof index === 'number' && !Number.isInteger(index)) ||
|
|
88
|
+
(typeof index === 'string' &&
|
|
89
|
+
index !== 'last' &&
|
|
90
|
+
!HASH_NEGATIVE_INDEX_REGEX.test(index))) {
|
|
91
|
+
throw new Error(`Unexpected index value in .at(...): ${index}`);
|
|
92
|
+
}
|
|
85
93
|
return this.#createBuilderWithPathLeg('ArrayLocation', index);
|
|
86
94
|
}
|
|
87
95
|
/**
|
|
@@ -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<
|
|
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;
|
|
@@ -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
|
|
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;
|
|
@@ -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;
|
|
@@ -13,6 +13,7 @@ const set_operation_node_js_1 = require("../operation-node/set-operation-node.js
|
|
|
13
13
|
const when_node_js_1 = require("../operation-node/when-node.js");
|
|
14
14
|
const log_once_js_1 = require("../util/log-once.js");
|
|
15
15
|
const LIT_WRAP_REGEX = /'/g;
|
|
16
|
+
const JSON_PATH_MEMBER_WRAP_REGEX = /['"]/g;
|
|
16
17
|
class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisitor {
|
|
17
18
|
#sql = '';
|
|
18
19
|
#parameters = [];
|
|
@@ -261,6 +262,10 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
261
262
|
this.append(' ');
|
|
262
263
|
this.visitNode(node.where);
|
|
263
264
|
}
|
|
265
|
+
if (node.returning) {
|
|
266
|
+
this.append(' ');
|
|
267
|
+
this.visitNode(node.returning);
|
|
268
|
+
}
|
|
264
269
|
if (node.orderBy) {
|
|
265
270
|
this.append(' ');
|
|
266
271
|
this.visitNode(node.orderBy);
|
|
@@ -269,10 +274,6 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
269
274
|
this.append(' ');
|
|
270
275
|
this.visitNode(node.limit);
|
|
271
276
|
}
|
|
272
|
-
if (node.returning) {
|
|
273
|
-
this.append(' ');
|
|
274
|
-
this.visitNode(node.returning);
|
|
275
|
-
}
|
|
276
277
|
if (wrapInParens) {
|
|
277
278
|
this.append(')');
|
|
278
279
|
}
|
|
@@ -1168,13 +1169,17 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
1168
1169
|
}
|
|
1169
1170
|
visitJSONPathLeg(node) {
|
|
1170
1171
|
const isArrayLocation = node.type === 'ArrayLocation';
|
|
1171
|
-
|
|
1172
|
-
this.append(typeof node.value === 'string'
|
|
1173
|
-
? this.sanitizeStringLiteral(node.value)
|
|
1174
|
-
: String(node.value));
|
|
1172
|
+
const value = String(node.value);
|
|
1175
1173
|
if (isArrayLocation) {
|
|
1174
|
+
this.append('[');
|
|
1175
|
+
this.append(this.sanitizeStringLiteral(value));
|
|
1176
1176
|
this.append(']');
|
|
1177
1177
|
}
|
|
1178
|
+
else {
|
|
1179
|
+
this.append('."');
|
|
1180
|
+
this.append(this.sanitizeJSONPathMemberValue(value));
|
|
1181
|
+
this.append('"');
|
|
1182
|
+
}
|
|
1178
1183
|
}
|
|
1179
1184
|
visitJSONOperatorChain(node) {
|
|
1180
1185
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
@@ -1321,6 +1326,9 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
1321
1326
|
sanitizeStringLiteral(value) {
|
|
1322
1327
|
return value.replace(LIT_WRAP_REGEX, "''");
|
|
1323
1328
|
}
|
|
1329
|
+
sanitizeJSONPathMemberValue(value) {
|
|
1330
|
+
return value.replace(JSON_PATH_MEMBER_WRAP_REGEX, (char) => char === "'" ? "''" : '\\"');
|
|
1331
|
+
}
|
|
1324
1332
|
addParameter(parameter) {
|
|
1325
1333
|
this.#parameters.push(parameter);
|
|
1326
1334
|
}
|
|
@@ -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},
|