Skip to content

transact

transact<T>(fn): Promise<T>

Defined in: .work/repos/edinburgh/src/edinburgh.ts:137

Executes a function within a database transaction context.

Loading models (also through links in other models) and changing models can only be done from within a transaction.

Transactions have a consistent view of the database, and changes made within a transaction are isolated from other transactions until they are committed. In case a commit clashes with changes made by another transaction, the transaction function will automatically be re-executed up to 6 times.

T

The return type of the transaction function.

() => T

The function to execute within the transaction context. Receives a Transaction instance.

Promise<T>

A promise that resolves with the function’s return value.

With code “RACING_TRANSACTION” if the transaction fails after retries due to conflicts.

With code “TXN_LIMIT” if maximum number of transactions is reached.

With code “LMDB-{code}” for LMDB-specific errors.

const paid = await E.transact(() => {
const user = User.get("john_doe");
if (user.credits > 0) {
user.credits--;
return true;
}
return false;
});
// Transaction with automatic retry on conflicts
await E.transact(() => {
const counter = Counter.get("global") || new Counter({id: "global", value: 0});
counter.value++;
});