EctoUtils.Repo (ecto_utils v0.1.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.

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

Given a struct derived from a module that use-es Ecto.Schema, returns the list of currently preloaded associations for said 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.is_schema(Date.utc_today())
> false
Link to this function

is_schema(module)

Specs

is_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

Link to this function

list_preloads(record)

Specs

list_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.list_preloads(user)

user
|> MyApp.Repo.reload()
|> MyApp.Repo.preload(preloads)