VBT.Ecto (vbt v0.1.0) View Source

Helpers for working with Ecto.

Link to this section Summary

Functions

Maps the result of a multi operation, or returns an error.

Returns the result of a multi operation or the operation error.

Link to this section Types

Specs

changes() :: %{required(Ecto.Multi.name()) => any()}

Specs

multi_result() ::
  {:ok, changes()}
  | {:error, failed_operation :: Ecto.Multi.name(), reason :: any(), changes()}

Link to this section Functions

Link to this function

map_multi_result(multi_result, success_mapper \\ &(&1))

View Source

Specs

map_multi_result(multi_result(), (changes() -> result)) ::
  {:ok, result} | {:error, any()}
when result: var

Maps the result of a multi operation, or returns an error.

iex> result = (
...>   Ecto.Multi.new()
...>   |> Ecto.Multi.run(:foo, fn _, _ -> {:ok, 1} end)
...>   |> Ecto.Multi.run(:bar, fn _, _ -> {:ok, 2} end)
...>   |> Ecto.Multi.run(:baz, fn _, _ -> {:ok, 3} end)
...>   |> VBT.TestRepo.transaction()
...> )
iex> VBT.Ecto.map_multi_result(result)
{:ok, %{foo: 1, bar: 2, baz: 3}}

iex> result = (
...>   Ecto.Multi.new()
...>   |> Ecto.Multi.run(:foo, fn _, _ -> {:ok, 1} end)
...>   |> Ecto.Multi.run(:bar, fn _, _ -> {:ok, 2} end)
...>   |> Ecto.Multi.run(:baz, fn _, _ -> {:ok, 3} end)
...>   |> VBT.TestRepo.transaction()
...> )
iex> VBT.Ecto.map_multi_result(result, &Map.take(&1, [:foo, :bar]))
{:ok, %{foo: 1, bar: 2}}

iex> result = (
...>   Ecto.Multi.new()
...>   |> Ecto.Multi.run(:foo, fn _, _ -> {:ok, 1} end)
...>   |> Ecto.Multi.run(:bar, fn _, _ -> {:error, "bar error"} end)
...>   |> VBT.TestRepo.transaction()
...> )
iex> VBT.Ecto.map_multi_result(result)
{:error, "bar error"}
Link to this function

multi_operation_result(multi_result, operation)

View Source

Specs

multi_operation_result(multi_result(), Ecto.Multi.name()) ::
  {:ok, any()} | {:error, any()}

Returns the result of a multi operation or the operation error.

iex> result = (
...>   Ecto.Multi.new()
...>   |> Ecto.Multi.run(:foo, fn _, _ -> {:ok, 1} end)
...>   |> Ecto.Multi.run(:bar, fn _, _ -> {:ok, 2} end)
...>   |> Ecto.Multi.run(:baz, fn _, _ -> {:ok, 3} end)
...>   |> VBT.TestRepo.transaction()
...> )
iex> VBT.Ecto.multi_operation_result(result, :foo)
{:ok, 1}

iex> result = (
...>   Ecto.Multi.new()
...>   |> Ecto.Multi.run(:foo, fn _, _ -> {:ok, 1} end)
...>   |> Ecto.Multi.run(:bar, fn _, _ -> {:error, "bar error"} end)
...>   |> VBT.TestRepo.transaction()
...> )
iex> VBT.Ecto.multi_operation_result(result, :foo)
{:error, "bar error"}