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
Functions
Update fields in a struct
Delete a struct in the database
Run a query
Save a struct to the database
Save a struct to the database
Performs a database update for a struct and params
Save a struct to the database
Link to this section Functions
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)
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)
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")
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)
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)
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)
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)