Tx.Macro (tx v0.4.1)

Export macros to allow create complex transactions without boilerplate.

Link to this section Summary

Functions

Create a transaction.

Create a transaction with a binding to repo.

Link to this section Functions

Link to this macro

tx(list)

(macro)

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
    Tx.run(repo, {:ok, {a, b}})
  end
end)

You can use tx/2 if you need to access to a binding to repo from the transaction.

Link to this macro

tx(repo, list)

(macro)

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
    Tx.run(repo, {:ok, {a, b}})
  end
end)