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.