Flop v0.4.0 Flop View Source
Flop is a helper library for filtering, ordering and pagination with Ecto.
Usage
Derive Flop.Schema
in your Ecto schemas.
defmodule Pet do
use Ecto.Schema
@derive {Flop.Schema,
filterable: [:name, :species], sortable: [:name, :age]}
schema "pets" do
field :name, :string
field :age, :integer
field :species, :string
field :social_security_number, :string
end
end
Validate a parameter map to get a Flop.t/0
struct with Flop.validate/1
.
Add the Flop.t/0
to a Ecto.Queryable.t/0
with Flop.query/2
.
iex> params = %{"order_by" => ["name", "age"], "limit" => 5}
iex> {:ok, flop} = Flop.validate(params, for: Flop.Pet)
{:ok,
%Flop{
filters: [],
limit: 5,
offset: nil,
order_by: [:name, :age],
order_directions: nil,
page: nil,
page_size: nil
}}
iex> Flop.Pet |> Flop.query(flop)
#Ecto.Query<from p0 in Flop.Pet, order_by: [asc: p0.name, asc: p0.age], limit: ^5>
Link to this section Summary
Types
Represents the supported order direction values.
Represents the query parameters for filtering, ordering and pagination.
Functions
Applies the filter
parameter of a Flop.t/0
to an Ecto.Queryable.t/0
.
Applies the order_by
and order_directions
parameters of a Flop.t/0
to an Ecto.Queryable.t/0
.
Applies the pagination parameters of a Flop.t/0
to an
Ecto.Queryable.t/0
.
Adds clauses for filtering, ordering and pagination to a
Ecto.Queryable.t/0
.
Validates a Flop.t/0
.
Link to this section Types
Specs
order_direction() :: :asc | :asc_nulls_first | :asc_nulls_last | :desc | :desc_nulls_first | :desc_nulls_last
Represents the supported order direction values.
Specs
t() :: %Flop{ filters: [Flop.Filter.t() | nil], limit: pos_integer() | nil, offset: non_neg_integer() | nil, order_by: [atom() | String.t()] | nil, order_directions: [order_direction()] | nil, page: pos_integer() | nil, page_size: pos_integer() | nil }
Represents the query parameters for filtering, ordering and pagination.
Fields
limit
,offset
: Used for pagination. May not be used together withpage
andpage_size
.page
,page_size
: Used for pagination. May not be used together withlimit
andoffset
.order_by
: List of fields to order by. Fields can be restricted by derivingFlop.Schema
in your Ecto schema.order_directions
: List of order directions applied to the fields defined inorder_by
. If empty or the list is shorter than theorder_by
list,:asc
will be used as a default for each missing order direction.filters
: List of filters, seeFlop.Filter.t/0
.
Link to this section Functions
Specs
filter(Ecto.Queryable.t(), t()) :: Ecto.Queryable.t()
Applies the filter
parameter of a Flop.t/0
to an Ecto.Queryable.t/0
.
Used by Flop.query/2
.
Specs
order_by(Ecto.Queryable.t(), t()) :: Ecto.Queryable.t()
Applies the order_by
and order_directions
parameters of a Flop.t/0
to an Ecto.Queryable.t/0
.
Used by Flop.query/2
.
Specs
paginate(Ecto.Queryable.t(), t()) :: Ecto.Queryable.t()
Applies the pagination parameters of a Flop.t/0
to an
Ecto.Queryable.t/0
.
The function supports both offset
/limit
based pagination and
page
/page_size
based pagination.
If you validated the Flop.t/0
with Flop.validate/1
before, you can be
sure that the given Flop.t/0
only has pagination parameters set for one
pagination method. If you pass an unvalidated Flop.t/0
that has
pagination parameters set for multiple pagination methods, this function
will arbitrarily only apply one of the pagination methods.
Used by Flop.query/2
.
Specs
query(Ecto.Queryable.t(), t()) :: Ecto.Queryable.t()
Adds clauses for filtering, ordering and pagination to a
Ecto.Queryable.t/0
.
The parameters are represented by the Flop.t/0
type. Any nil
values
will be ignored.
Examples
iex> flop = %Flop{limit: 10, offset: 19}
iex> Flop.query(Flop.Pet, flop)
#Ecto.Query<from p0 in Flop.Pet, limit: ^10, offset: ^19>
Or enhance an already defined query:
iex> require Ecto.Query
iex> flop = %Flop{limit: 10}
iex> Flop.Pet |> Ecto.Query.where(species: "dog") |> Flop.query(flop)
#Ecto.Query<from p0 in Flop.Pet, where: p0.species == "dog", limit: ^10>
Specs
validate(t() | map(), keyword()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
Validates a Flop.t/0
.
Examples
iex> params = %{"limit" => 10, "offset" => 0, "texture" => "fluffy"}
iex> Flop.validate(params)
{:ok,
%Flop{
filters: [],
limit: 10,
offset: 0,
order_by: nil,
order_directions: nil,
page: nil,
page_size: nil
}}
iex> flop = %Flop{offset: -1}
iex> {:error, changeset} = Flop.validate(flop)
iex> changeset.valid?
false
iex> changeset.errors
[
offset: {"must be greater than or equal to %{number}",
[validation: :number, kind: :greater_than_or_equal_to, number: 0]}
]
It also makes sure that only one pagination method is used.
iex> params = %{limit: 10, offset: 0, page: 5, page_size: 10}
iex> {:error, changeset} = Flop.validate(params)
iex> changeset.valid?
false
iex> changeset.errors
[limit: {"cannot combine multiple pagination types", []}]
If you derived Flop.Schema
in your Ecto schema to define the filterable
and sortable fields, you can pass the module name to the function to validate
that only allowed fields are used.
iex> params = %{"order_by" => ["social_security_number"]}
iex> {:error, changeset} = Flop.validate(params, for: Flop.Pet)
iex> changeset.valid?
false
iex> [order_by: {msg, [_, {_, enum}]}] = changeset.errors
iex> msg
"has an invalid entry"
iex> enum
[:name, :age, :species]
Note that currently, trying to use an existing field that is not allowed as
seen above will result in the error message has an invalid entry
, while
trying to use a field name that does not exist in the schema (or more
precisely: a field name that doesn't exist as an atom) will result in
the error message is invalid
. This might change in the future.