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
| Parameter | Type | Default value | Description |
|---|---|---|---|
driver | IDbTransactionDriver | undefined | Low-level transaction driver (e.g. SQLite, PostgreSQL client, MSSQL transaction). |
isolationLevel | IsolationLevel | IsolationLevel.ReadCommitted | Isolation 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
| Parameter | Type | Description |
|---|---|---|
name | string | Identifier 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
| Parameter | Type | Description |
|---|---|---|
name | string | Identifier 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 Parameter | Default type |
|---|---|
T | IDbTransactionDriver |
Returns
T
The underlying driver instance.
Usecase
Access provider-specific raw transaction objects when needed.
Properties
isolationLevel
readonlyisolationLevel:IsolationLevel=IsolationLevel.ReadCommitted
Defined in: src/transaction/DbTransaction.ts:28
Isolation level for this transaction.