View Source Qh (qh v0.2.0)

Query helper for iex

.iex.exs:

use Qh

Qh.configure(app: :my_app)

Example:

iex>q User.where(age > 20 and age < 50).order(name).first
%MyApp.User%{id: 123, age: 21, name: "Anna"}

Link to this section Summary

Link to this section Functions

Link to this function

default_primary_order(schema)

View Source
Link to this function

group_by_and_aggregate(query, target)

View Source
Link to this function

lookup_schema(schema, opts \\ [])

View Source
Link to this macro

q(query, opts \\ [])

View Source (macro)
Link to this function

query(q, opts \\ [], caller \\ __ENV__)

View Source

Run a query

Examples:

# First/last
q User.first
q User.first(10)
q User.last
q User.last(1)

# Custom order
q User.order(name).last(3)
q User.order(name: :asc, age: :desc).last
q User.order("lower(?)", name).last

# Conditions
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

# Opional binding
q User.where([u], u.age > 20 and u.age <= 30).count

# Find
q User.get!(21)
# or
q User.find(21)

# Alias for where(...).first
q User.find_by(name: "Bob Foo")
q User.find_by(name == "Bob" or name == "Anna")

# Aggregations
q User.group_by("length(name)").count
q User.group_by(name).avg(age)

# Select stats
q User.select(count(), avg(age), min(age), max(age)).all

# Aggregate stats grouped by column
q User.group_by(name).aggr(%{count: count(), avg: avg(age), min: min(age), max: max(age)})

# Count number of messages per user
q User.left_join(:messages).group_by(id).count([u, m], m.id)

# Grab only users that have messages
q User.distinct(id).join(:messages).all

# Custom join logic
q User.join([u], u in MyApp.Messages, on: u.id == m.sent_by_id, as: :m)
Link to this function

split_optional_binding(params)

View Source
Link to this function

transform_aggr(original_params, caller, opts)

View Source
Link to this function

transform_join(fun, params)

View Source
Link to this function

transform_with_binding(fun, params)

View Source