EctoUtils.Queryable behaviour (ecto_utils v0.2.1)
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
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
implemented_by?(module)
Specs
Returns true if, and only if, the given module implements the EctoUtils.Queryable behaviour
Link to this section Callbacks
Specs
base_query() :: Ecto.Queryable.t()
query(arg1, arg2)
Specs
query(Ecto.Queryable.t(), Keyword.t()) :: Ecto.Queryable.t()