Trash.Query (trash v0.1.0)
Provides query methods for working with records that implement Trash
.
Schemas should first include Trash.Schema
and/or manually add the necessary
fields for these to work.
Link to this section Summary
Functions
Adds trashable fields to select.
Adds a where clause for returning discarded records.
Adds a where clause for returning kept records.
Link to this section Functions
select_trashable(queryable)
Specs
select_trashable(queryable :: Ecto.Queryable.t()) :: Ecto.Queryable.t()
Adds trashable fields to select.
This ensures that both trashable fields are included in the select statement
by using Ecto.Query.select_merge/3
to merge in the fields.
For a list of the current trashable fields, see
Trash.Schema.trashable_fields/0
.
This loads discarded_at
from the database and computes the boolean for
discarded?
from the SQL expression discarded_at IS NOT NULL
.
Note: Since discarded?
is a virtual field, without using this function,
it'll be nil
by default.
Examples
iex> Trash.Query.select_trashable(Post) |> Repo.all()
[%Post{title: "Hello World", discarded_at: %DateTime{}, discarded?: true}]
iex> Trash.Query.select_trashable(Post) |> Repo.all()
[%Post{title: "Hello World", discarded_at: nil, discarded?: false}]
where_discarded(queryable)
Specs
where_discarded(queryable :: Ecto.Queryable.t()) :: Ecto.Queryable.t()
Adds a where clause for returning discarded records.
This adds a where clause equivalent to the SQL expression discarded_at IS NOT NULL
which denotes a record that has been discarded.
Examples
iex> Trash.Query.where_discarded(Post) |> Repo.all()
[%Post{title: "Hello World", discarded_at: %DateTime{}, discarded?: nil}]
where_kept(queryable)
Specs
where_kept(queryable :: Ecto.Queryable.t()) :: Ecto.Queryable.t()
Adds a where clause for returning kept records.
This adds a where clause equivalent to the SQL expression discarded_at IS NULL
which denotes a record that has been kept.
Examples
iex> Trash.Query.where_kept(Post) |> Repo.all()
[%Post{title: "Hello World", discarded_at: nil, discarded?: nil}]