fidr

fidr

Qh - Ecto query helper for iex (Rails style)

I created a little helper q to easily query your database in iex.

Github: qh

It helps by namespacing the schema’s under the specified app name and integrating the Repo funtionality by chaining it to the query.

So instead of doing:

(from u in MyApp.User, order: u.name, limit: 1) |> MyApp.Repo.one()
(from u in MyApp.User, where: u.name == "Bob") |> MyApp.Repo.aggregate(:count)

You could do:

q User.order(name).first
q User.where(name == "Bob").count

Other examples:

q User.first
q User.order(name).last(3)
q User.order(name: :asc, age: :desc).last
q User.order("lower(?)", name).last
q User.where(age > 20 and age <= 30).count
q User.where(age > 20 and age <= 30).limit(10).all
q User.where(age > 20 or name == "Bob").all
q User.where(age > 20 and (name == "Bob" or name == "Anna")).all
q User.where(age: 20, name: "Bob").count
q User.where("nicknames && ?", ["Bobby", "Bobi"]).count
q User.where("? = ANY(?)", age, [20, 30, 40]).count
q User.find(21)
q User.find_by(name: "Bob Foo")
q User.find_by(name == "Bob" or name == "Anna")

q returns a query if you don’t call a repo function at the end, so you could even chain it:

q(User.where(age > 20 and age <= 30)) |> MyApp.Repo.all()

Not all Ecto functions are supported, but I’m looking to expand the library with the most common cases.

First 3 of 3 Posts Switch mode

fidr

fidr OP

I just published v0.2.0:

  • Support most Ecto.Query functions:
    distinct,where,select,select_merge,group_by,having,lock,or_having,or_where,order_by,preload,windows,limit,except,except_all,exclude,intersect,intersect_all,union,union_all,reverse_order,join,inner_join,left_join,right_join,cross_join,full_join,inner_lateral_join,left_lateral_join

  • Support extra Ecto.Repo functions:
    all, one, stream, exists?

  • Optional binding supported for when needed:
    q User.where([u], u.name == "Bob").first

  • Added an aggr function and helpers to easily do an aggragation on a grouped_by query like:
    q User.group_by(name).aggr(count(), avg(age))
    and some helpers like:
    q User.group_by(name).count

  • Easy joins (defaults to assoc):
    q User.join(:messages).select([u, m], {u.id, m.message}).all

brightball

brightball

Is there a way to convert this syntax to a full Ecto syntax quickly? Working things out in iex and then copying the code into the application is a common workflow and with the shorthand syntax I couldn’t do that as easily.

I like the approach a lot though.

fidr

fidr OP

That would be hard to do currently, because it uses some internal convenience functions in the generated code. But I’ll keep it in mind for a next update. I might be able to make it work

— All posts loaded —

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement