Phoenix API Toolkit v0.6.0 PhoenixApiToolkit.Ecto.GenericQueries View Source
Generic queries are applicable to any named binding in a query. By using generic queries, it is not necessary to implement standard queries for every Ecto model.
For example, instead of implementing in a User model:
def by_username(query, username) do
from [user: user] in query, where: user.username == ^username
end
User.by_username(query, "some username")
...you can use generic query equals/4
instead:
GenericQueries.equals(query, :user, :username, "some username")
Such generic queries can be combined together in complex ways:
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> base_query()
...> |> equals(:user, :name, "Peter")
...> |> smaller_than(:user, :balance, 50.00)
#Ecto.Query<from u0 in "users", as: :user, where: u0.name == ^"Peter", where: u0.balance < ^50.0>
Most of these generic queries rely on named bindings to do their work. That's why it's probably a good idea to always name all bindings in your queries, and not rely on positional bindings to separate models in your queries.
Link to this section Summary
Types
The directions supported by order_by/4
Functions
Narrow down the query to results in which the value of binding.field
is
equal to value
. If value
is a list, results that are equal to any list
element are returned.
Narrow down the query to results in which the value contained in
binding.field
is greater than or equal to value
.
Limit the query result set size to value
.
Narrow down the query to results in which value
is a member of the set of
values contained in field.binding
. Use with array-type Ecto fields.
Offset the query results by value
.
Order the query by binding.field
in direction
.
Narrow down the query to results in which the value contained in
binding.field
is smaller than value
.
Link to this section Types
order_directions()
View Sourceorder_directions() :: :asc | :asc_nulls_first | :asc_nulls_last | :desc | :desc_nulls_first | :desc_nulls_last
The directions supported by order_by/4
Link to this section Functions
equals(query, binding, field, value)
View Sourceequals(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()
Narrow down the query to results in which the value of binding.field
is
equal to value
. If value
is a list, results that are equal to any list
element are returned.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> equals(base_query(), :user, :name, "Peter")
#Ecto.Query<from u0 in "users", as: :user, where: u0.name == ^"Peter">
iex> equals(base_query(), :user, :name, ["Peter", "Patrick"])
#Ecto.Query<from u0 in "users", as: :user, where: u0.name in ^["Peter", "Patrick"]>
greater_than_or_equals(query, binding, field, value)
View Sourcegreater_than_or_equals(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()
Narrow down the query to results in which the value contained in
binding.field
is greater than or equal to value
.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> greater_than_or_equals(base_query(), :user, :balance, 50.00)
#Ecto.Query<from u0 in "users", as: :user, where: u0.balance >= ^50.0>
limit(query, value)
View Sourcelimit(Ecto.Query.t(), integer()) :: Ecto.Query.t()
Limit the query result set size to value
.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> limit(base_query(), 10)
#Ecto.Query<from u0 in "users", as: :user, limit: ^10>
member_of(query, binding, field, value)
View Sourcemember_of(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()
Narrow down the query to results in which value
is a member of the set of
values contained in field.binding
. Use with array-type Ecto fields.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> member_of(base_query(), :user, :roles, "admin")
#Ecto.Query<from u0 in "users", as: :user, where: ^"admin" in u0.roles>
offset(query, value)
View Sourceoffset(Ecto.Query.t(), integer()) :: Ecto.Query.t()
Offset the query results by value
.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> offset(base_query(), 10)
#Ecto.Query<from u0 in "users", as: :user, offset: ^10>
order_by(query, binding, field, direction)
View Sourceorder_by(Ecto.Query.t(), atom(), atom(), order_directions()) :: Ecto.Query.t()
Order the query by binding.field
in direction
.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> order_by(base_query(), :user, :name, :asc_nulls_first)
#Ecto.Query<from u0 in "users", as: :user, order_by: [asc_nulls_first: u0.name]>
smaller_than(query, binding, field, value)
View Sourcesmaller_than(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()
Narrow down the query to results in which the value contained in
binding.field
is smaller than value
.
iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>
iex> smaller_than(base_query(), :user, :balance, 50.00)
#Ecto.Query<from u0 in "users", as: :user, where: u0.balance < ^50.0>