Is this query possible in Ecto?

Good news!

Ecto master now allows developers to build dynamic queries. Here is an example from the documentation:

dynamic = false

dynamic =
  if params["is_public"] do
    dynamic([p], p.is_public or ^dynamic)
  else
    dynamic
  end

dynamic =
  if params["allow_reviewers"] do
    dynamic([p, a], a.reviewer == true or ^dynamic)
  else
    dynamic
  end

from query, where: ^dynamic

This means you should now be able to write the query above by building each dynamic piece bit by bit and then later adding to a given query field.

Currently we allow dynamic expressions only in where but we will add support for other expressions throughout the week. More information can be found here: https://github.com/elixir-ecto/ecto/blob/2f5190dac96da71da62b8c03ec2ac72463b6fd47/lib/ecto/query.ex#L333

Notice you will need Ecto master for that. If you have any questions, please let me know.

7 Likes