Trubo Ecto
Trubo is a very rich ecto component,including search sort and paginate. Inspiration by ruby ransack and learn from rummage_ecto
Table of contents
Getting started
-
The package can be installed by adding
trubo_ectoto your list of dependencies inmix.exs:
def deps do
[
{:trubo_ecto, "~> 0.1.2"}
]
end-
Add the Repo of your app and the desired per_page to the
trubo_ectoconfiguration in config.exs:
config :trubo_ecto, Trubo.Ecto,
repo: MyApp.Repo,
per_page: 10-
Or add the
trubo_ectowith elixiruse
use Ttrubo.Ecto, repo: MyApp.Repo, per_page: 10Examples
iex> params = %{
"q" => %{"title_like" => "hello123", "category_id_eq" => 1},
"s" => "inserted_at+asc",
"per_page" => 5, "page" => 10
}
iex> Trubo.Ecto.truboq(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> Trubo.Ecto.trubo(Product, params)
%{
datas: [Product.t()],
paginate: %{
current_page: 10,
next_page: 11,
per_page: 5,
prev_page: 9,
total_count: 100,
total_pages: 20
}
}
Also supports:
-
Use
Trubo.Ecto.searchonly returns searchresultor useTrubo.Ecto.searchqreturns searchqueryable; -
Use
Trubo.Ecto.sortonly returns sortresultor useTrubo.Ecto.sortqreturns sortqueryable; -
Use
Trubo.Ecto.paginatereturns pagianteresultor useTrubo.Ecto.paginateqreturns paginatequeryable.
More example pls move: docs
Search Matchers
List of all possible predicates
| Predicate | Description | Finish | Note
| ------------- | ------------- |-------- |-------- |
| *_eq | equal | Y | |
| *_not_eq | not equal | N | |
| *_lt | less than | N | |
| *_lteq | less than or equal | Y | |
| *_gt | greater than | N | |
| *_gteq | greater than or equal | N | |
| *_present | not null and not empty | N | Only compatible with string columns. Example: q[name_present]=1 (SQL: col is not null AND col != '') |
| *_blank | is null or empty. | N | (SQL: col is null OR col = '') |
| *_null | is null | N | |
| *_not_null | is not null | N | |
| *_in | match any values in array | N | e.g. q[name_in][]=Alice&q[name_in][]=Bob |
| *_start_with | Starts with | N | SQL: col LIKE 'value%' |
| *_not_start_with | Does not start with | N | |
| *_end_with | Ends with | N | |
| *_not_end_with | Does not end with | N | |
| *_like | Contains value | Y | uses LIKE |
| *_ilike | Contains any of | N | |
| *_true | is true | N | |
| *_false | is false | N | |
| *_between| begin < between < end | N | |
Features
- Example website.
- Add the necessary code test.
-
Support
andandorsymbol e.g.q[title_or_body_like]=hello123 - Support multi table assoc search.
- Support multi table assoc sort.
Credits
- ecto - Very great API.
- ransack - Initial inspiration of this project.
- rummage_ecto - Similar implementation.