VBT.Repo behaviour (vbt v0.1.0) View Source

Wrapper around Ecto.Repo with a few additional helper functions.

Link to this section Summary

Functions

Wrapper around use Ecto.Repo.

Callbacks

Deletes a single database row matching the given query.

Fetches a single struct from the data store where the primary key matches the given id.

Fetches a single result from the given schema of query which matches the given filters.

Fetches a single result from the given query.

Runs the given function inside a transaction.

Link to this section Types

Specs

fetch_opts() :: [{:tag, String.t()} | {:error, String.t()} | {atom(), any()}]

Specs

trans_fun() ::
  (() -> {:ok, any()} | {:error, any()})
  | (module() -> {:ok, any()} | {:error, any()})

Link to this section Functions

Link to this macro

__using__(opts)

View Source (macro)

Wrapper around use Ecto.Repo.

Invoke use VBT.Repo instead of use Ecto.Repo. This macro will internally invoke use Ecto.Repo, passing it the given options.

In addition, the macro will generate the implementation of the VBT.Repo behaviour.

Link to this section Callbacks

Specs

delete_one(Ecto.Queryable.t()) ::
  :ok | {:ok, any()} | {:error, :not_found | :multiple_rows}

Deletes a single database row matching the given query.

This function allows you to delete a single database row, without needing to load it from the database first.

The function can optionally return the deleted row if you provide the :select clause in the input query. In this case, the function will return {:ok, selected_term} on success. If the :select clause is not present, the function will return :ok on success.

The function succeeds only if exactly one row is matched by the given query. If there are multiple rows matching the given query, nothing will be deleted, and an error is returned. Likewise, the function returns an error if there are no rows matching the given query.

Link to this callback

fetch(module, id, fetch_opts)

View Source

Specs

fetch(module(), id :: term(), fetch_opts()) ::
  {:ok, Ecto.Schema.t()} | {:error, String.t()}

Fetches a single struct from the data store where the primary key matches the given id.

This function accepts the same options as fetch_one/2.

Link to this callback

fetch_by(arg1, arg2, fetch_opts)

View Source

Specs

fetch_by(Ecto.Queryable.t(), Keyword.t() | map(), fetch_opts()) ::
  {:ok, any()} | {:error, String.t()}

Fetches a single result from the given schema of query which matches the given filters.

This function accepts the same options as fetch_one/2.

Link to this callback

fetch_one(arg1, fetch_opts)

View Source

Specs

fetch_one(Ecto.Queryable.t(), fetch_opts()) ::
  {:ok, any()} | {:error, String.t()}

Fetches a single result from the given query.

The function returns the result in the form of {:ok, result} | {:error, reason} You can control the error reason with the :tag and the :error options:

iex> Repo.fetch_one(from Account, where: [id: -1])
{:error, "Record not found"}

iex> Repo.fetch_one(from Account, where: [id: -1], tag: "Account)
{:error, "Account not found"}

iex> Repo.fetch_one(from Account, where: [id: -1], error: "Account missing")
{:error, "Account missing"}

In addition, you can pass all the Repo shared options, as well as the :prefix option.

Specs

transact((() -> result) | (module() -> result), Keyword.t()) :: result
when result: {:ok, any()} | {:error, any()}

Runs the given function inside a transaction.

This function is a wrapper around Ecto.Repo.transaction, with the following differences:

  • It accepts only a lambda of arity 0 or 1 (i.e. it doesn't work with multi).
  • If the lambda returns {:ok, result} the transaction is committed, and {:ok, result} is returned.
  • If the lambda returns {:error, reason} the transaction is rolled back, and {:error, reason} is returned.
  • If the lambda returns any other kind of result, an exception is raised, and the transaction is rolled back.