Search code examples
elixirphoenix-frameworkecto

Ecto dynamic query params in phoenixframework


I'm new to Elixir and Phoenix framework. How can I dynamically add parameter to ecto query ? For example:

def index(conn, _params) do
  departments = Repo.all(Department)
  render(conn, "index.json", data: departments)
end

Is it possible to use the _params in Ecto query ? Something like:

Repo.get_by(Department, _params)

Solution

  • You can use "bindingless queries" for this, but there's a problem. Ecto expects the column names to be atoms and the argument to be a keyword list. The latter is easy, but converting arbitrary user input into atoms may result in the Erlang VM crashing. Here's a safe way to do this using a whitelist of column names:

    # user input
    params = %{"first_name" => "John", "last_name" => "Smith", "age" => 28}
    filtered_params =
      params
      |> Map.take(~w(first_name last_name age))
      |> Enum.map(fn {k, v} -> {String.to_atom(k), v} end)
    
    from(MyApp.Person, where: ^filtered_params)
    |> MyApp.Repo.all
    |> IO.inspect
    

    Output:

    [debug] SELECT p0."id", p0."first_name", p0."last_name", p0."age", p0."inserted_at", p0."updated_at" FROM "people" AS p0 WHERE (((p0."age" = ?) AND (p0."first_name" = ?)) AND (p0."last_name" = ?)) [28, "John", "Smith"] OK query=6.5ms queue=14.8ms
    [%MyApp.Person{__meta__: #Ecto.Schema.Metadata<:loaded>, age: 28,
      first_name: "John", id: 1, inserted_at: #Ecto.DateTime<2016-06-11T16:01:49Z>,
      last_name: "Smith", updated_at: #Ecto.DateTime<2016-06-11T16:01:49Z>}]