EctoUtils.Repo (ecto_utils v0.2.0)

Utility module containing functions which aim to augment modules that use Ecto.Repo

Link to this section Summary

Functions

Allows you to execute any EctoUtils.Repo function in the module that use-es this macro.

Executes the given closure in a transaction; returning its returns, but ensuring that the transaction was rolled back in the process.

Given a struct derived from a module that use-es Ecto.Schema, returns the list of currently preloaded associations for said schema.

Returns true if the given paramter is an Elixir module that use-es Ecto.Schema or is a struct derived from a module that use-es Ecto.Schema

Link to this section Functions

Link to this macro

__using__(opts)

(macro)

Allows you to execute any EctoUtils.Repo function in the module that use-es this macro.

This is useful for centralizing Repo functions in your app's own repo module, rather than you needing to manually call EctoUtils.Repo functions yourself.

Usage:

defmodule MyApp.Repo do
  use Ecto.Repo, ...
  use EctoUtils.Repo
end

MyApp.Repo.schema?(Date.utc_today())
> false
Link to this function

dry_run(repo, closure)

Specs

dry_run(repo :: module(), closure :: (() -> term())) ::
  {:ok, term()} | {:error, term()}

Executes the given closure in a transaction; returning its returns, but ensuring that the transaction was rolled back in the process.

Link to this function

preloads(record)

Specs

preloads(struct()) :: [atom()]

Given a struct derived from a module that use-es Ecto.Schema, returns the list of currently preloaded associations for said schema.

This can be useful when used in conjunction with Repo.reload/2 to refetch a given struct from the database while also reloading all preloads:

user = %MyApp.User{orgs: [...]}
preloads = MyApp.Repo.preloads(user)

user
|> MyApp.Repo.reload()
|> MyApp.Repo.preload(preloads)
Link to this function

schema?(module)

Specs

schema?(module() | struct()) :: boolean()

Returns true if the given paramter is an Elixir module that use-es Ecto.Schema or is a struct derived from a module that use-es Ecto.Schema