zven21

zven21

TurboEcto: A rich ecto component, including search sort and paginate

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. :smile:

Most Liked

michalmuskala

michalmuskala

I got a quick look and it looks interesting.

One thing I would change and I think it would improve quality-of-life would be returning a struct from the turboq function and implementing Enumerable on it to iterate over the results.

The other thing is why there’s the use API at all - I don’t see it giving the user anything they can’t do just calling Turbo.Ecto functions directly.

tomekowal

tomekowal

We’ve just built something similar ad hoc in our project :slight_smile: with some differences:
We didn’t want to couple params to DB operations and we wanted to filter on joined entities too,
so we decided to allow custom params e.g.

params = %{
  "q" => %{"shipped_before" => "01/01/2018"},
  "s" => "inserted_at+asc",
  "per_page" => 5, "page" => 10
}

And then for each query we need a callback module that implements the query and filter callbacks.

def query do
  Product
  |> join(:inner, [p], sh in assoc(p, :shippings))
end

def maybe_apply_filter_term_to_query(query, "shipped_before", date) do
  query
  |> where([p, sh], sh.date < ^date
end

And then we apply all filters one by one, add ordering and filtering. Saves nice amount of boilerplate, unifies params responsible for sorting and pagination, and allows for arbitrarily complicated queries :slight_smile:

If multiple people arrive at similar ideas, it means that those are good ideas! :slight_smile:

zven21

zven21

@michalmuskala Thank you for your reply, it’s over-design, you are right, I know how to improve it. :slight_smile:

zven21

zven21

Yeah, seed this issue and this one

If you have other needs, please add issue :slight_smile:

cnck1387

cnck1387

Oh I’m not sure what that was. Usually I hit up tests to see a boat load of manual examples because it showcases how to use the library in every intended way.

Where Next?

Popular in Libraries Top

marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: https://github.com/devonestes/assertions ...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
gjaldon
As the title states, EctoEnum has just been updated after some time of hardly any activity in the repo. Here’s the latest release: https:...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Sub Categories:

We're in Beta

About us Mission Statement