scisco v0.1.0 Scisco.Query behaviour View Source
Easy pagination/sorting/filtering.
Use this module to provide the default Scisco query interface:
defmodule MyQuery do
use Scisco.Query
def base_queryable(), do: MySchema
def repo(), do: MyRepo
end
and then call it from outside:
MyQuery.list(%{page: %{number: 3, size: 10}, filter: %{"foo" => "bar"}, sort: "-baz"})
Pagination
The query module supports by default two pagination strategies: the "classic paginator" style
(with :size
and :number
keys - :number
defaulting to 1) and the "raw paginator" style
(with :limit
and :offset
keys - :offset
defaulting to 0).
Sorting
By default any string in the sort key will enforce sorting the queryable by the corresponding
field of the queryable - descending if the string starts with "-"
, ascending otherwise.
One can implement additional clauses that respect the sort_callback
typespec. The clauses
will always receive :asc
or :desc
depending on whether the original sort param starts with "-"
.
defmodule MyQuery do
use Scisco.Query
def base_queryable(), do: MySchema
def repo(), do: MyRepo
def sort(queryable, {:some_key, :asc})
def sort(queryable, {:some_key, :desc})
end
Filtering
By default any key in the filter map will be converted into an exact match of the corresponding field of the queryable.
One can implement additional clauses that respect the filter_callback
typespec.
defmodule MyQuery do
use Scisco.Query
def base_queryable(), do: MySchema
def repo(), do: MyRepo
def filter(queryable, {:some_key, value})
end
Link to this section Summary
Types
The map representing the query params.
Callbacks
Needs to be implemented by the module, returning an initial queryable for pagination etc.
Implements specific filtering behavior. Falls back to match the term with the field
Needs to be implemented by the module, returning the Ecto Repo with which to actually execute the queries.
Implements specific sorting behavior. Falls back to sorting by the given field
Link to this section Types
filter_callback()
View Sourcefilter_callback() :: (Ecto.Queryable.t(), {atom(), term()} -> Ecto.Queryable.t())
page_param()
View Sourcepage_param() :: nil | %{:size => pos_integer(), optional(:number) => pos_integer()} | %{:limit => pos_integer(), optional(:offset) => non_neg_integer()}
params()
View Sourceparams() :: %{ optional(:page) => page_param(), optional(:sort) => sort_param(), optional(:filter) => filter_param() }
The map representing the query params.
sort_callback()
View Sourcesort_callback() :: (Ecto.Queryable.t(), {atom(), :asc | :desc} -> Ecto.Queryable.t())
Link to this section Functions
filter(queryable, filters, cb)
View Sourcefilter( queryable :: Ecto.Queryable.t(), params :: filter_param(), cb :: filter_callback() ) :: Ecto.Queryable.t()
Performs an actual filtering.
paginate(queryable, param)
View Sourcepaginate(queryable :: any(), params :: nil | page_param()) :: any()
Performs an actual pagination.
sort(queryable, key, cb)
View Sourcesort( queryable :: Ecto.Queryable.t(), params :: sort_param(), cb :: sort_callback() ) :: Ecto.Queryable.t()
Performs an actual sorting.
Link to this section Callbacks
Needs to be implemented by the module, returning an initial queryable for pagination etc.
filter(queryable, {})
View Sourcefilter(queryable :: Ecto.Queryable.t(), {field :: atom(), value :: term()}) :: Ecto.Queryable.t()
Implements specific filtering behavior. Falls back to match the term with the field
Needs to be implemented by the module, returning the Ecto Repo with which to actually execute the queries.
sort(queryable, {})
View Sourcesort( queryable :: Ecto.Queryable.t(), {field :: atom(), direction :: :asc | :desc} ) :: Ecto.Queryable.t()
Implements specific sorting behavior. Falls back to sorting by the given field