turbo_ecto v0.1.5 Turbo.Ecto
A rich ecto component, including search sort and paginate. https://hexdocs.pm/turbo_ecto
Example
Category Table Structure
Field | Type | Comment |
---|---|---|
name | string |
Product Table Structure
Field | Type | Comment |
---|---|---|
title | string | |
body | text | |
price | float | |
category_id | integer | |
available | boolean |
Variant Table Structure
Field | Type | Comment |
---|---|---|
title | string | |
price | float | |
product_id | integer |
- Input Search
url_query = http://localhost:4000/varinats?q[product_category_name_and_product_name_or_name_like]=elixir
- Expect output:
iex> params = %{"q" => %{"product_category_name_and_product_name_or_name_like" => "elixir"}}
iex> Turbo.Ecto.turboq(Turbo.Ecto.Variant, params)
#Ecto.Query<from v in subquery(from v in subquery(from v in subquery(from v in Turbo.Ecto.Variant),
or_where: like(v.name, ^"%elixir%")),
join: p in assoc(v, :product),
join: c in assoc(p, :category),
where: like(c.name, ^"%elixir%")), join: p in assoc(v, :product), where: like(p.name, ^"%elixir%"), limit: ^10, offset: ^0>
Link to this section Summary
Functions
Gets paginate info
Returns Paginate result
Returns Paginate queryable
Returns searching result
Returns searching queryable
Returns sorting result
Returns sorting queryable
Returns a result and pageinate info
Returns processed queryable
Link to this section Functions
Link to this function
get_paginate(queryable, params, opts \\ [])
Gets paginate info.
Example
iex> params = %{"per_page" => 5, "page" => 2}
iex> Turbo.Ecto.get_paginate(Turbo.Ecto.Product, params)
%{per_page: 5, current_page: 2, next_page: nil, prev_page: nil, total_count: 0, total_pages: 0}
Link to this function
paginate(queryable, params, opts \\ [])
paginate(Ecto.Query.t(), Map.t(), Keyword.t()) :: Map.t()
Returns Paginate result.
Example
iex> params = %{"per_page" => 5, "page" => 2}
iex> Turbo.Ecto.paginate(Turbo.Ecto.Product, params)
%{dates: [], paginate: %{current_page: 2, next_page: nil, per_page: 5, prev_page: nil, total_count: 0, total_pages: 0}}
Link to this function
paginateq(queryable, params)
paginateq(Ecto.Query.t(), Map.t()) :: Ecto.Query.t()
Returns Paginate queryable.
Example
iex> params = %{"per_page" => 5, "page" => 2}
iex> Turbo.Ecto.paginateq(Turbo.Ecto.Product, params)
#Ecto.Query<from p in Turbo.Ecto.Product, limit: ^5, offset: ^5>
Link to this function
search(queryable, params, opts \\ [])
search(Ecto.Query.t(), Map.t(), Keyword.t()) :: any()
Returns searching result.
Example
iex> params = %{"q" => %{"name_like" => "q"}}
iex> Turbo.Ecto.search(Turbo.Ecto.Product, params)
[]
Link to this function
searchq(queryable, params)
Returns searching queryable.
Example
iex> Turbo.Ecto.searchq(Turbo.Ecto.Product, %{"q" => %{"name_like" => "elixir"}})
#Ecto.Query<from p in subquery(from p in Turbo.Ecto.Product), where: like(p.name, ^"%elixir%")>
Link to this function
sort(queryable, params, opts \\ [])
sort(Ecto.Query.t(), Map.t(), Keyword.t()) :: any()
Returns sorting result.
Example
iex> params = %{"s" => "updated_at+desc"}
iex> Turbo.Ecto.sort(Turbo.Ecto.Product, params)
[]
Returns sorting queryable.
Example
iex> params = %{"s" => "updated_at+desc"}
iex> Turbo.Ecto.sortq(Turbo.Ecto.Product, params)
#Ecto.Query<from p in Turbo.Ecto.Product, order_by: [desc: p.updated_at]>
Link to this function
turbo(queryable, params, opts \\ [])
turbo(Ecto.Query.t(), Map.t(), Keyword.t()) :: Map.t()
Returns a result and pageinate info.
Example
iex> params = %{"q" => %{"name_like" => "q"}, "s" => "updated_at+asc", "per_page" => 5, "page" => 1}
iex> Turbo.Ecto.turbo(Turbo.Ecto.Product, params)
%{
paginate: %{current_page: 1, per_page: 5, next_page: nil, prev_page: nil, total_count: 0, total_pages: 0},
datas: []
}
Returns processed queryable.
Example
iex> params = %{"q" => %{"name_or_body_like" => "elixir"}, "s" => "updated_at+asc", "per_page" => 5, "page" => 1}
iex> Turbo.Ecto.turboq(Turbo.Ecto.Product, params)
#Ecto.Query<from p in subquery(from p in subquery(from p in Turbo.Ecto.Product),
or_where: like(p.body, ^"%elixir%")), where: like(p.name, ^"%elixir%"), order_by: [asc: p.updated_at], limit: ^5, offset: ^0>