Changelog - 2026-09-26
runInTransaction runs commit and rollback for you
FeatureIn one line. You no longer hand-write begin, try, commit, catch and rollback in every repository method that needs a transaction.
const order = await orderRepository.runInTransaction({
execute: async ({ transaction }) => {
const { data: created } = await orderRepository.create({
data: orderData,
options: { transaction },
});
await orderItemRepository.create({
data: { orderId: created.id, ...itemData },
options: { transaction },
});
return created;
},
});Pass a transaction and runInTransaction joins it: it never commits or rolls back, and transactionOptions is ignored. Pass none and it owns one: it begins with beginTransaction(transactionOptions), commits on success, and rolls back on failure, rethrowing the original error even when the rollback fails. A service method that must work stand-alone and nested is one forward:
async function createOrder(opts: { data: TOrderCreate; transaction?: IDatabaseTransaction }) {
return orderRepository.runInTransaction({
transaction: opts.transaction,
execute: async ({ transaction }) =>
orderRepository.create({ data: opts.data, options: { transaction } }),
});
}If execute ends an owned transaction itself:
- Calling
commit()insideexecute: the result is returned, with no second commit. - Calling
rollback()insideexecute, or leaving it failed: a dedicated error names thatexecuteended the transaction before it could be committed, with no rollback attempt. executerejecting after ending the transaction: the original rejection, with no further rollback.- Always
awaitcommit()androllback()insideexecute. An unawaited commit is read as committed before it settles, sorunInTransactioncan return while that commit goes on to fail. - A transaction from an entirely different
ITransactionimplementation is unreadable and reports the "ended before it could be committed" error even ifexecutecommitted it.
See Transactions.
TransactionDouble stubs a transaction with no database
FeatureIn one line. Unit-test runInTransaction without standing up Postgres or SQLite.
TransactionDouble comes from the new @venizia/ignis/testing entry (@venizia/ignis-connectors/testing in the connectors package):
import { spyOn } from 'bun:test';
import { PostgresTransactionDouble } from '@venizia/ignis/testing';
const double = new PostgresTransactionDouble();
spyOn(orderRepository, 'beginTransaction').mockResolvedValue(double);
await orderRepository.runInTransaction({ execute: async () => 'ok' });
expect(double.commitCount).toBe(1);Reading double.connector throws, so a repository method that reaches the database must be stubbed too. Inject commitError or rollbackError to test a failed end. A double proves control flow, not SQL atomicity. See Transaction Doubles.
Who is affected
Additive. Nothing existing changes behavior, and adopting either API is optional.
One small difference: isActive on the transaction handle beginTransaction() returns now lives on a shared prototype, not as an own property. A spread copy ({ ...transaction }) no longer carries it, so passing a spread copy to a repository now throws "Transaction is no longer active". Reading isActive through a Proxy or an Object.create(transaction) wrapper now throws a TypeError. Nothing in IGNIS does either.