Turbo is a very rich ecto component,including search sort and paginate. Inspiration by ruby ransack and learn from rummage_ecto.
Finshed
iex> params = %{
"q" => %{"title_like" => "hello123", "category_id_eq" => 1},
"s" => "inserted_at+asc",
"per_page" => 5, "page" => 10
}
iex> Turbo.Ecto.turboq(Product, params)
#Ecto.Query<from t in Product, where: t.type == ^1,
where: like(t.title, ^"%hello123%"),
order_by: [asc: t.inserted_at],
limit: ^5, offset: ^45>
iex> Turbo.Ecto.turbo(Product, params)
%{
datas: [Product.t(), ...],
paginate: %{
current_page: 10,
next_page: 11,
per_page: 5,
prev_page: 9,
total_count: 100,
total_pages: 20
}
}
TODO
- Support and && or symbols.
iex> params = %{
"q" => %{"title_or_body_like" => "hello123"}
}
iex> Turbo.Ecto.turboq(Product, params)
#Ecto.Query<from t in Product, where: like(t.body, ^"%hello123%"),
where: like(t.title, ^"%hello123%")
- Support assoc table.
iex> params = %{
"q" => %{"category_name_like" => "elixir"}
}
iex> Turbo.Ecto.turboq(Product, params)
#Ecto.Query<from p in subquery(from p in "products"), join: c in assoc(p, :category), or_where: like(c.name, ^"elixir")>
check it here:
Thank you for reading, have a good day.