Turbo.Ecto (turbo_ecto v0.6.1)

A rich ecto component, including search sort and paginate. https://hexdocs.pm/turbo_ecto

Example

Category Table Structure

FieldTypeComment
namestring

Product Table Structure

FieldTypeComment
namestring
bodytext
pricefloat
category_idinteger
availableboolean

Variant Table Structure

FieldTypeComment
namestring
pricefloat
product_idinteger
  • Input Search

    url_query = http://localhost:4000/varinats?q[product_name_or_name_like]=elixir
  • Expect output:

    iex> params = %{"q" => %{"product_name_or_name_like" => "elixir"}}
    iex> Turbo.Ecto.turboq(Turbo.Ecto.Variant, params)
    #Ecto.Query<from v0 in Turbo.Ecto.Variant, join: p1 in assoc(v0, :product), where: like(p1.name, "%elixir%") or like(v0.name, "%elixir%"), limit: 10, offset: 0>

Link to this section Summary

Functions

Returns a result and pageinate info.

Returns processed queryable.

Link to this section Functions

Link to this function

turbo(queryable, params, opts \\ [])

Specs

turbo(Ecto.Query.t(), map(), keyword()) :: map()

Returns a result and pageinate info.

Example

iex> params = %{"q" => %{"name_or_product_name_like" => "elixir", "price_eq" => "1"}, "s" => "updated_at+asc", "per_page" => 5, "page" => 1}
iex> Turbo.Ecto.turbo(Turbo.Ecto.Variant, params)
%{
  paginate: %{current_page: 1, per_page: 5, next_page: nil, prev_page: nil, total_count: 0, total_pages: 0},
  datas: []
}

iex> import Ecto.Query
iex> params = %{"q" => %{"name_or_product_name_like" => "elixir", "price_eq" => "1"}, "s" => "updated_at+asc", "per_page" => 5, "page" => 1}
iex> callback = fn queryable -> queryable |> select([:name]) end
iex> Turbo.Ecto.turbo(Turbo.Ecto.Variant, params, callback: callback)
%{
  paginate: %{current_page: 1, per_page: 5, next_page: nil, prev_page: nil, total_count: 0, total_pages: 0},
  datas: []
}
Link to this function

turboq(queryable, params)

Specs

turboq(Ecto.Query.t(), map()) :: Ecto.Query.t()

Returns processed queryable.

Example

iex> params = %{"q" => %{"name_or_body_like" => "elixir", "a_eq" => ""}, "s" => "updated_at+asc", "per_page" => 5, "page" => 1}
iex> Turbo.Ecto.turboq(Turbo.Ecto.Product, params)
#Ecto.Query<from p0 in Turbo.Ecto.Product, where: like(p0.name, "%elixir%") or like(p0.body, "%elixir%"), order_by: [asc: p0.updated_at], limit: 5, offset: 0>