Tx.Macro (tx v0.1.1)
Export macros to allow create complex transactions without boilerplate.
Link to this section Summary
Link to this section Functions
Create a transaction.
import Tx.Macro
tx do
{:ok, a} <- create_a_tx(value)
{:ok, b} <- create_b_tx(a)
{:ok, {a, b}}
end
will expands into:
Tx.new(fn repo ->
with {:ok, a} <- Tx.run(repo, create_a_tx(value)),
{:ok, b} <- Tx.run(repo, create_b_tx(a)) do
{:ok, {a, b}}
end
end)
You can use tx/2
if you need to access to a binding to repo
from
the transaction.
Create a transaction with a binding to repo
.
The repo
binding can be used within the body for raw db operations
(e.g. Repo.insert
, Repo.update
, ...).
Example:
The following code:
import Tx.Macro
tx repo do
{:ok, value} <- repo.insert(foo)
{:ok, a} <- create_a_tx(value)
{:ok, b} <- create_b_tx(a)
{:ok, {a, b}}
end
will expands into:
Tx.new(fn repo ->
with {:ok, value} <- Tx.run(repo, repo.insert(foo)),
{:ok, a} <- Tx.run(repo, create_a_tx(value)),
{:ok, b} <- Tx.run(repo, create_b_tx(a)) do
{:ok, {a, b}}
end
end)