MCPcopy
hub / github.com/kysely-org/kysely / TransactionBuilder

Class TransactionBuilder

src/kysely.ts:862–922  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

860interface ConnectionBuilderProps extends KyselyProps {}
861
862export class TransactionBuilder<DB> {
863 readonly #props: TransactionBuilderProps
864
865 constructor(props: TransactionBuilderProps) {
866 this.#props = freeze(props)
867 }
868
869 setAccessMode(accessMode: AccessMode): TransactionBuilder<DB> {
870 return new TransactionBuilder({
871 ...this.#props,
872 accessMode,
873 })
874 }
875
876 setIsolationLevel(isolationLevel: IsolationLevel): TransactionBuilder<DB> {
877 return new TransactionBuilder({
878 ...this.#props,
879 isolationLevel,
880 })
881 }
882
883 async execute<T>(callback: (trx: Transaction<DB>) => Promise<T>): Promise<T> {
884 const { isolationLevel, accessMode, ...kyselyProps } = this.#props
885 const settings = { isolationLevel, accessMode }
886
887 validateTransactionSettings(settings)
888
889 return this.#props.executor.provideConnection(async (connection) => {
890 const state = { isCommitted: false, isRolledBack: false }
891
892 const executor = new NotCommittedOrRolledBackAssertingExecutor(
893 this.#props.executor.withConnectionProvider(
894 new SingleConnectionProvider(connection),
895 ),
896 state,
897 )
898
899 const transaction = new Transaction<DB>({ ...kyselyProps, executor })
900
901 let transactionBegun = false
902 try {
903 await this.#props.driver.beginTransaction(connection, settings)
904 transactionBegun = true
905
906 const result = await callback(transaction)
907
908 await this.#props.driver.commitTransaction(connection)
909 state.isCommitted = true
910
911 return result
912 } catch (error) {
913 if (transactionBegun) {
914 await this.#props.driver.rollbackTransaction(connection)
915 state.isRolledBack = true
916 }
917
918 throw error
919 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…