AbacusSql (abacus_sql v2.1.1) View Source
Use AbacusSql to introduce powerful customizable query features to your application.
By leveraging the safe to execute expression syntax of Abacus, Ecto queries can be modified with filters (via where/3), additional custom selects, custom order_by clauses, and more.
Examples
from(u in User, group_by: u.id)
|> AbacuSql.where(~S[name == "Peter"]))
|> AbacusSql.select("name", "name")
# blog_posts assoc is automatically joined
|> AbacusSql.select("post_count", "count(blog_posts.id)")
|> AbacusSql.order_by("name", true)
# take a filter from plug params
from(u in User)
|> where(Map.get(params, "filter", "true"))
Link to this section Summary
Functions
Adds a group_by expression to the query
Adds the given filter to the havings list.
Adds an order_by expression to the query.
Adds scoped data to the query. That data is accessible by simply accessing the given key.
Adds scoped data to the query. That data is accessible by simply accessing the given key.
Adds or merges a selection to the query.
Adds an and_where clause to the query.
Link to this section Types
Specs
option() :: root_option()
Specs
options() :: [option()]
Specs
Default: 0
Specifies which source in the query should be used as root. It can either be given as a table id (0 is the actual root, 1-n is the first-nth join), or as an alias.
The expressions will then have the given source as their root, meaning their fields and assocs are directly accessible.
Example:
query = from u in User, join bp in assoc(u, :blog_posts), as: :blog_posts
AbacusSql.where(query, "title != null", root: :blog_posts)
otherwise we'd have to write the where expression like this:
query = from u in User, join bp in assoc(u, :blog_posts)
AbacusSql.where(query, "blog_posts.title != null")
Specs
Link to this section Functions
Specs
group_by(Ecto.Query.t(), t(), options()) :: Ecto.Query.t()
Adds a group_by expression to the query
Specs
having(Ecto.Query.t(), t(), options()) :: Ecto.Query.t()
Adds the given filter to the havings list.
Make sure that the query has at least one group_by clause.
The advantage is that it can filter inside has_many
assocs
using aggregation such as count
, min
or max
.
For example: having(BlogPost, "count(author.posts.id) > 10")
would
filter for blog posts whose authors have at least 10 posts.
Specs
order_by(Ecto.Query.t(), t(), boolean(), options()) :: Ecto.Query.t()
Adds an order_by expression to the query.
Specs
scope(Ecto.Query.t(), [ {atom(), term() | %{__struct__: module(), __meta__: Ecto.Schema.Metadata.t()}} ]) :: Ecto.Query.t()
Adds scoped data to the query. That data is accessible by simply accessing the given key.
Example:
from(u in User)
|> AbacusSql.scope(some_number: 13)
|> AbacusSql.where("count(blog_posts.id) < some_number")
Specs
scope( Ecto.Query.t(), atom(), term() | %{__struct__: module(), __meta__: Ecto.Schema.Metadata.t()} ) :: Ecto.Query.t()
Adds scoped data to the query. That data is accessible by simply accessing the given key.
Example:
from(u in User)
|> AbacusSql.scope(:some_number, 13)
|> AbacusSql.where("count(blog_posts.id) < some_number")
Specs
select(Ecto.Query.t(), atom() | String.t(), t(), options()) :: Ecto.Query.t()
Adds or merges a selection to the query.
Example:
query = from u in User
query = select(query, "name", ~S[concat(name, " (", count(blog_posts.id), " posts)")])
Repo.all(query) == [%{"name" => "Peter (31 posts)"}, %{"name" => "Mark (13 posts)"}]
Specs
where(Ecto.Query.t(), t(), options()) :: Ecto.Query.t()
Adds an and_where clause to the query.