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 Libraries Top

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...
574 16576 179
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
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
mathieuprog
Hello :wave: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First and fore...
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
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement