EctoUtils.Queryable behaviour (ecto_utils v0.2.0)

Exposes the queryable behaviour and utility macro for making Ecto Query composition for Ecto Schemas easier to define, isolate, and be consistent.

Link to this section Summary

Functions

Generic fallback filters for the EctoUtils.Queryable behaviour. Schemas which implement the query/2 callback are able to add a catch-all fallback clause pointing to this function in order to allow EctoUtils to automatically derive common queries.

Returns true if, and only if, the given module implements the EctoUtils.Queryable behaviour

Link to this section Functions

Link to this function

apply_filter(query, field, value)

Specs

apply_filter(module() | Ecto.Queryable.t(), field :: atom(), value :: term()) ::
  Ecto.Queryable.t()

Generic fallback filters for the EctoUtils.Queryable behaviour. Schemas which implement the query/2 callback are able to add a catch-all fallback clause pointing to this function in order to allow EctoUtils to automatically derive common queries.

Usage:

@impl EctoUtils.Queryable
def query(base_query \ base_query(), filters) do
  Enum.reduce(filters, query, fn
    {:inserted_at_start, datetime}, query ->
      from(x in query, where: x.inserted_at >= ^datetime)

    {:inserted_at_end, datetime}, query ->
      from(x in query, where: x.inserted_at <= ^datetime)

    {:updated_at_start, datetime}, query ->
      from(x in query, where: x.updated_at >= ^datetime)

    {:updated_at_end, datetime}, query ->
      from(x in query, where: x.updated_at <= ^datetime)

    {field, value}, query ->
      apply_filter(query, field, value)
  end)
end
Link to this function

implemented_by?(module)

Specs

implemented_by?(module()) :: boolean()

Returns true if, and only if, the given module implements the EctoUtils.Queryable behaviour

Link to this section Callbacks

Link to this callback

base_query()

(optional)

Specs

base_query() :: Ecto.Queryable.t()
Link to this callback

query(arg1, arg2)

Specs