Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"stream": [
"dist/stream.d.ts"
],
"transactions": [
"dist/transactions.d.ts"
],
"valueproxy": [
"dist/valueproxy.d.ts"
]
Expand Down Expand Up @@ -73,6 +76,10 @@
"types": "./dist/stream.d.ts",
"default": "./dist/stream.js"
},
"./transactions": {
"types": "./dist/transactions.d.ts",
"default": "./dist/transactions.js"
},
"./valueproxy": {
"types": "./dist/valueproxy.d.ts",
"default": "./dist/valueproxy.js"
Expand Down
28 changes: 28 additions & 0 deletions src/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { InterfaceFunction } from "@antelopejs/interface-core";

type TransactionRunner = <T>(callback: () => Promise<T>) => Promise<T>;

/**
* Runs a callback once inside an atomic database transaction.
*
* The callback is invoked exactly once. Database operations started through the
* interface during the callback share the transaction. The returned promise
* resolves only after commit, while callback rejections abort the transaction.
*
* Do not perform externally visible side effects in the callback because a
* database rollback cannot undo them. Nested transactions and database work
* started in the callback but awaited after it returns are rejected. Providers
* reject this operation when their configured topology does not support
* transactions.
*
* A rejection while committing can have an ambiguous outcome: the provider may
* be unable to determine whether the database committed the transaction. The
* callback is never replayed to resolve uncertain commit acknowledgement.
*
* Transaction duration and commit acknowledgement bounds are provider-specific.
*
* @param callback Asynchronous database work to execute atomically
* @returns The callback result after the transaction is committed
*/
export const RunInTransaction =
InterfaceFunction<TransactionRunner>() as TransactionRunner;
Loading