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.
Type Parameters
Section titled “Type Parameters”T
The return type of the transaction function.
Parameters
Section titled “Parameters”() => T
The function to execute within the transaction context. Receives a Transaction instance.
Returns
Section titled “Returns”Promise<T>
A promise that resolves with the function’s return value.
Throws
Section titled “Throws”With code “RACING_TRANSACTION” if the transaction fails after retries due to conflicts.
Throws
Section titled “Throws”With code “TXN_LIMIT” if maximum number of transactions is reached.
Throws
Section titled “Throws”With code “LMDB-{code}” for LMDB-specific errors.
Example
Section titled “Example”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 conflictsawait E.transact(() => { const counter = Counter.get("global") || new Counter({id: "global", value: 0}); counter.value++;});