Documentation / @andrew_l/mongo-transaction / withTransactionControlled
Function: withTransactionControlled()
withTransactionControlled<
T,K,Args>(fn):TransactionControlled<Awaited<T>,K,Args>
Wraps a function and returns a TransactionControlled interface, allowing manual control over transaction commit and rollback operations.
This provides finer-grained control over the transaction lifecycle, enabling users to explicitly commit or rollback a transaction based on custom logic. It's especially useful in scenarios where transactional state or conditions need to be externally determined.
Type Parameters
T
T
K
K = any
Args
Args extends any[] = any[]
Parameters
fn
(this, ...args) => T
Returns
TransactionControlled<Awaited<T>, K, Args>
Example
ts
const t = withTransactionControlled(async (userId) => {
await useTransactionEffect(async () => {
await db.users.updateById(userId, { premium: true });
return () => db.users.updateById(userId, { premium: false })
});
const user = await db.users.findById(userId);
return user;
});
await t.run();
// Remove premium when no subscriptions
if (t.result.activeSubscriptions > 0) {
await t.commit();
} else {
await t.rollback();
}