Moebius v3.0.1 Moebius.Query
The main query interface for Moebius. Import this module into your code and query like a champ
Summary
Functions
Insert multiple rows at once, within a single transaction, returning the inserted records. Pass in a composite list, containing the rows to be inserted. Note, the columns to be inserted are defined based on the first record in the list. All records to be inserted must adhere to the same schema
Executes a COUNT query based on the assembled pipeline. Analagous to map/reduce(:count)
. Returns an integer
Specifies the table or view you want to query and returns a QueryCommand struct
Creates a DELETE command
Executes a function with the given name, passed as an atom
Executes a function with the given name, passed as an atom
Creates a function command
Specifies a GROUP BY for a map/reduce
(aggregate) query that is a string
Creates an insert command based on the assembled pipeline
Build a table join for your query. There are a number of options to handle various joins. Joins can also be piped for multiple joins
Executes a given pipeline and returns the last matching result. You should specify a sort
to be sure first works as intended.
cols - Any columns (specified as a string) that you want to have aliased or restricted in your return.
For example `now() as current_time, name, description`. Defaults to "*"
Example:
cheap_skate = db(:users)
|> sort(:money_spent, :desc)
|> last("first, last, email")
Sets the limit of the return
An alias for filter
, specifies a range to rollup on for an aggregate query using a WHERE statement
Offsets the limit and is an alias for skip/1
”
A rollup operation that aggregates the mapped result set by the specified operation
Full text search using Postgres’ built in indexing, ranked using tsrank
. This query will result in a full table scan and is not optimized for large result
sets. For better results, create a tsvector
field and populate it with a trigger on insert/update. This will cause some side
effects, one of them being that Postgrex, the Elixir driver we use, doesn’t know how to resolve the tsvector type, and will throw
Creates a SELECT command based on the assembled pipeline. Uses the QueryCommand as its core structure
Offsets the limit and is an alias for offset/1
”
Sets the order by. Ascending using :asc
is the default, you can send in :desc
if you like
Executes the SQL in a given SQL file without parameters. Specify the scripts directory by setting the scripts
directive in the config.
Pass the file name as an atom, without extension
Executes the SQL in a given SQL file with the specified parameters. Specify the scripts
directory by setting the scripts
directive in the config. Pass the file name as an atom,
without extension
Creates a SQL File command
Creates an update command based on the assembled pipeline
Functions
Insert multiple rows at once, within a single transaction, returning the inserted records. Pass in a composite list, containing the rows to be inserted. Note, the columns to be inserted are defined based on the first record in the list. All records to be inserted must adhere to the same schema.
Example:
data = [
[first_name: "John", last_name: "Lennon", address: "123 Main St.", city: "Portland", state: "OR", zip: "98204"],
[first_name: "Paul", last_name: "McCartney", address: "456 Main St.", city: "Portland", state: "OR", zip: "98204"],
[first_name: "George", last_name: "Harrison", address: "789 Main St.", city: "Portland", state: "OR", zip: "98204"],
[first_name: "Paul", last_name: "Starkey", address: "012 Main St.", city: "Portland", state: "OR", zip: "98204"],
]
result = db(:people) |> insert(data)
Executes a COUNT query based on the assembled pipeline. Analagous to map/reduce(:count)
. Returns an integer.
Example:
count = db(:users)
|> limit(20)
|> count
|> single
#count == 20
Specifies the table or view you want to query and returns a QueryCommand struct.
“table” - the name of the table you want to query, such as membership.users
:table - the name of the table you want to query, such as :users
Example
result = db(:users)
|> to_list
result = db("membership.users")
|> to_list
Executes a function with the given name, passed as an atom.
Example:
result = db(:users)
|> function(:all_users)
Executes a function with the given name, passed as an atom.
params: - An array of values to be passed to the function.
Example:
result = db(:users)
|> function(:friends, ["mike","jane"])
Specifies a GROUP BY for a map/reduce
(aggregate) query that is a string.
cols - A string specifying the column to GROUP BY. Will also be part of the SELECT list.
Example:
result = db(:users)
|> map("money_spent > 100")
|> group("company, state")
|> reduce(:sum, :money_spent)
Build a table join for your query. There are a number of options to handle various joins. Joins can also be piped for multiple joins.
:join - set the type of join. LEFT, RIGHT, FULL, etc. defaults to INNER :on - specify the table to join on :foreign_key - specify the tables foreign key column :primary_key - specify the joining tables primary key column :using - used to specify a USING queries list of columns to join on
Example of simple join:
cmd = db(:customers)
|> join(:orders)
|> select
Example of multiple table joins:
cmd = db(:customers)
|> join(:orders, on: :customers)
|> join(:items, on: :orders)
|> select
Executes a given pipeline and returns the last matching result. You should specify a sort
to be sure first works as intended.
cols - Any columns (specified as a string) that you want to have aliased or restricted in your return.
For example `now() as current_time, name, description`. Defaults to "*"
Example:
cheap_skate = db(:users)
|> sort(:money_spent, :desc)
|> last("first, last, email")
Sets the limit of the return.
bound - And integer limiter
Example:
result = db(:users)
|> limit(20)
|> to_list
An alias for filter
, specifies a range to rollup on for an aggregate query using a WHERE statement.
criteria - A string, atom or list (see filter
)
Example:
result = db(:users)
|> map("money_spent > 100")
|> reduce(:sum, :money_spent)
Offsets the limit and is an alias for skip/1
”
Example:
result = db(:users)
|> limit(20)
|> offset(2)
|> to_list
A rollup operation that aggregates the mapped result set by the specified operation.
op - An atom indicating what you want to have happen, such as :sum
, :avg
, :min
, :max
.
Corresponds directly to a PostgreSQL rollup function.
Example:
result = db(:users)
|> map("money_spent > 100")
|> reduce(:sum, :money_spent)
Full text search using Postgres’ built in indexing, ranked using tsrank
. This query will result in a full table scan and is not optimized for large result
sets. For better results, create a tsvector
field and populate it with a trigger on insert/update. This will cause some side
effects, one of them being that Postgrex, the Elixir driver we use, doesn’t know how to resolve the tsvector type, and will throw.
You will need to be sure that you exclude that search column from your query.
for: - The string term you want to query against. in: - An atomized list of columns to search againts.
Example:
result = db(:users)
|> search(for: "Mike", in: [:first, :last, :email])
|> run
Creates a SELECT command based on the assembled pipeline. Uses the QueryCommand as its core structure.
cols - Any columns (specified as a string) that you want to have aliased or restricted in your return.
For example `now() as current_time, name, description`
Example:
command = db(:users)
|> limit(20)
|> offset(2)
|> select("now() as current_time, name, description")
#command is a QueryCommand object with all of the pipelined settings applied
Offsets the limit and is an alias for offset/1
”
Example:
result = db(:users)
|> limit(20)
|> skip(2)
|> to_list
Sets the order by. Ascending using :asc
is the default, you can send in :desc
if you like.
cols - The atomized name of the columns, such as :company
direction - :asc
(default) or :desc
Example:
result = db(:users)
|> sort(:name, :desc)
|> to_list
Executes the SQL in a given SQL file without parameters. Specify the scripts directory by setting the scripts
directive in the config.
Pass the file name as an atom, without extension.
result = sql_file(:simple)
Executes the SQL in a given SQL file with the specified parameters. Specify the scripts
directory by setting the scripts
directive in the config. Pass the file name as an atom,
without extension.
result = sql_file(:save_user, [1])