Qh View Source

Easy Rails-style query helper for iex.

use Qh

Qh.configure(app: :my_app)

q User.where(age > 20).limit(10).all
[%MyApp.User{age: 22, name: "Bob"}, ...]

Installation

Add qh to your list of dependencies in mix.exs:

def deps do
  [
    {:qh, "~> 0.1"}
  ]
end

Add to your .iex.exs:

use Qh

Qh.configure(app: :my_app)

Make sure to change :my_app to your otp app name that holds the schema's and repo's.

Usage

Query 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")

Modifying records

# new
user = q User.new(name: "Alice")
{:ok, user} = Qh.save(user)

# update via save
user = q User.first
{:ok, user} = Qh.save(%{user | name: "Bob"})

# update
user = q User.find(21)
{:ok, user} = Qh.update(user, age: 50)

# delete
user = q User.find(21)
Qh.delete!(user)

Configuration

  • app: App name that is used for infering the schema namespace and repo
  • app_mod: You can also set the app module directly, instead of the app name
  • repo: Set the repo module directly, in case of a non-default repo

Set via config:

config :qh, app: :my_app, repo: MyApp.AlternateRepo

Set via configure:

Qh.configure(app: :my_app, repo: MyApp.AlternateRepo)

Pass in options:

user = q(User.find(21), app: :my_app, repo: MyApp.AlternateRepo)