Qh (qh v0.1.0) View Source

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

assign(schema, params, opts \\ [])

View Source

Update fields in a struct

Examples:

user = %MyApp.User{name: nil, age: nil}
user = Qh.assign(user, name: "Bob", age: 21)

# or initialize a new record
user = Qh.assign(:user, name: "Alice", age: 22)
Link to this function

default_primary_order(schema)

View Source
Link to this function

delete!(struct, opts \\ [])

View Source

Delete a struct in the database

Will raise if the record doesn't exists

Examples:

user = %MyApp.User{id: 2, name: "Bob", age: 21}
Qh.delete!(user)
Link to this function

lookup_schema(schema, opts \\ [])

View Source
Link to this macro

q(q, opts \\ [])

View Source (macro)

Run a query

Examples:

use Qh

Qh.configure(app: :my_app)

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

# Initialize new
user = q User.new(name: "Bob")
Link to this function

save(struct, opts \\ [])

View Source

Save a struct to the database

Inserts a new record if no primary key is set or no record exists with the current primary key.

Performs a get and an update if the record already exists.

Returns {:ok, struct} on success or {:error, validation_errors} on failure.

Examples:

user = %MyApp.User{id: nil, name: "Bob", age: 21}
{:ok, user} = Qh.save(user)
Link to this function

save!(struct, opts \\ [])

View Source

Save a struct to the database

Similar to save/1 but will raise on failures and return only the struct on success.

Examples:

user = %MyApp.User{id: nil, name: "Bob", age: 21}
user = Qh.save!(user)
Link to this function

update(struct, params, opts \\ [])

View Source

Performs a database update for a struct and params

Returns {:ok, struct} on success or {:error, validation_errors} on failure.

Examples:

user = %MyApp.User{id: 2, name: "Bob", age: 21}
{:ok, user} = Qh.update(user, age: 22)
Link to this function

update!(struct, params, opts \\ [])

View Source

Save a struct to the database

Similar to update/1 but will raise on failures and return only the struct on success.

Examples:

user = %MyApp.User{id: 2, name: "Bob", age: 21}
user = Qh.update!(user, age: 22)