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.

Most Liked

fidr

fidr

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

fidr

fidr

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.

Where Next?

Popular in Announcing Top

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
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. https://github.com/bryanjos/elixirscript/blob/master/CHANGELOG.md Most of the chan...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 13014 120
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 18934 194
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement