Skip to main content

Class: DbTransaction

Defined in: src/transaction/DbTransaction.ts:17

Encapsulates an active database transaction.

Provides methods to commit, rollback, and manage nested savepoints across different database providers.

Accessors​

isCompleted​

Get Signature​

get isCompleted(): boolean

Defined in: src/transaction/DbTransaction.ts:36

Indicates whether this transaction has already been committed or rolled back.

Usecase​

Check if transaction is still active before attempting further operations.

Returns​

boolean

Constructors​

Constructor​

new DbTransaction(driver, isolationLevel?): DbTransaction

Defined in: src/transaction/DbTransaction.ts:26

Initializes a new transaction instance wrapping a database driver transaction.

Parameters​

ParameterTypeDefault valueDescription
driverIDbTransactionDriverundefinedLow-level transaction driver (e.g. SQLite, PostgreSQL client, MSSQL transaction).
isolationLevelIsolationLevelIsolationLevel.ReadCommittedIsolation level for this transaction.

Returns​

DbTransaction

Methods​

commit()​

commit(): Promise<void>

Defined in: src/transaction/DbTransaction.ts:50

Commits all database changes made during this transaction permanently.

Returns​

Promise<void>

Usecase​

Finalize a successful unit-of-work batch of mutations.

Throws​

DbException if the transaction is already finalized.

Example​

await tx.commit();

rollback()​

rollback(): Promise<void>

Defined in: src/transaction/DbTransaction.ts:67

Rolls back and discards all changes made during this transaction.

Returns​

Promise<void>

Usecase​

Revert partial mutations when an error or validation failure occurs.

Example​

await tx.rollback();

savepoint()​

savepoint(name): Promise<void>

Defined in: src/transaction/DbTransaction.ts:85

Creates a named transaction savepoint within the active transaction.

Parameters​

ParameterTypeDescription
namestringIdentifier for the savepoint.

Returns​

Promise<void>

Usecase​

Create intermediate checkpoints to roll back partial operations without aborting the entire transaction.

Example​

await tx.savepoint('step1');

rollbackTo()​

rollbackTo(name): Promise<void>

Defined in: src/transaction/DbTransaction.ts:106

Rolls back transaction state to a previously created named savepoint.

Parameters​

ParameterTypeDescription
namestringIdentifier of the savepoint to restore.

Returns​

Promise<void>

Usecase​

Revert operations performed after a specific savepoint while keeping earlier operations intact.

Example​

await tx.rollbackTo('step1');

getDriver()​

getDriver<T>(): T

Defined in: src/transaction/DbTransaction.ts:123

Returns the underlying driver transaction instance (e.g. pg.PoolClient, mssql.Transaction).

Type Parameters​

Type ParameterDefault type
TIDbTransactionDriver

Returns​

T

The underlying driver instance.

Usecase​

Access provider-specific raw transaction objects when needed.

Properties​

isolationLevel​

readonly isolationLevel: IsolationLevel = IsolationLevel.ReadCommitted

Defined in: src/transaction/DbTransaction.ts:28

Isolation level for this transaction.