Is there a convention for the case where you want to ensure that a given Ecto query always returns no results, but still supports piping?
For example, in Ruby land ActiveRecord provides the none
method that returns a null query object.
Naively I can just use a limit with something like…
defp none(query) do
from query, limit: 0
end
Is there a more idiomatic route or a way to return some sort of null query object that Repo.all
then knows not to even run the query?
I thought perhaps returning nil
or []
might serve that purpose, but those are not implemented by Ecto.Query. Probably better that they don’t.
This is handy in cases where I’m filtering a collection of records based upon a user’s permissions prior to piping to other filters.
defp scope(query, user) do
cond do
user.role == "admin" -> query
user.role == "manager" -> from r in query, where: r.user_id == ^user.id
true -> none(query)
end
end