View Source EctoSparkles (bonfire_umbrella v0.9.8-cooperation-beta.15)
query_filter
brings convenience and shortens the boilterplate of ecto queries
Common filters available include:
preload
- Preloads fields onto the query resultsstart_date
- Query for items inserted after this dateend_date
- Query for items inserted before this datebefore
- Get items with IDs before this valueafter
- Get items with IDs after this valueids
- Get items with a list of idsfirst
- Gets the first n itemslast
- Gets the last n itemslimit
- Gets the first n itemsoffset
- Offsets limit by n itemssearch
- Warning: This requires schemas using this to have a&by_search(query, val)
function
You are also able to filter on any natural field of a model, as well as use
- gte/gt
- lte/lt
- like/ilike
- is_nil/not(is_nil)
query_filter(User, %{name: %{ilike: "steve"}})
query_filter(User, %{name: %{ilike: "steve"}}, :last_name, :asc)
query_filter(User, %{name: %{age: %{gte: 18, lte: 30}}})
query_filter(User, %{name: %{is_banned: %{!=: nil}}})
query_filter(User, %{name: %{is_banned: %{==: nil}}})
my_query = query_filter(User, %{name: "Billy"})
query_filter(my_query, %{last_name: "Joe"})
Summary
Functions
join_preload
is a helper for preloading associations using joins.
AKA join_preload++
. It's more powerful, but it does it with more (and different!) syntax.
reusable_join
is similar to Ecto.Query.join/{4,5}
, but can be called multiple times with the same alias.
Functions
join_preload
is a helper for preloading associations using joins.
By default, Ecto preloads associations using a separate query for each association, which can degrade performance. You could make it run faster by using a combination of join/preload, but that requires a bit of boilerplate (see example below).
With EctoSparkles
, you can accomplish this with just one line of code.
Example using just Ecto
import Ecto.Query
Invoice
|> join(:left, [i], assoc(i, :customer), as: :customer)
|> join(:left, [i, c], assoc(c, :account), as: :account)
|> join(:left, [i], assoc(i, :lines), as: :lines)
|> preload([lines: v, customers: c, account: a], lines: v, customer: {c, [a: account]})
|> Repo.all()
Example using join_preload
import EctoSparkles
Invoice
|> join_preload([:customer, :account])
|> join_preload([:lines])
|> Repo.all()
AKA join_preload++
. It's more powerful, but it does it with more (and different!) syntax.
e.g.
proload(query, activity: [
:verb, :boost_count, :like_count, :replied,
# relations under object will have their aliases prefixed with object_, i.e.
# :object_message, :object_post, :object_post_content
# the original names will still be used for the associations.
object: {"object_", [:message, :post, :post_content]}
])
query_filter(module_or_query, filters, order_by_prop \\ :id, order_direction \\ :desc)
View Sourcereusable_join
is similar to Ecto.Query.join/{4,5}
, but can be called multiple times with the same alias.
Note that only the first join operation is performed, the subsequent ones that use the same alias are just ignored. Also note that because of this behaviour, it is mandatory to specify an alias when using this function.
This is helpful when you need to perform a join while building queries one filter at a time,
because the same filter could be used multiple times or you could have multiple filters that
require the same join, which poses a problem with how the filter/3
callback work, as you
need to return a dynamic with the filtering, which means that the join must have an alias,
and by default Ecto raises an error when you add multiple joins with the same alias.
To solve this, it is recommended to use this macro instead of the default Ecto.Query.join/{4,5}
,
in which case there will be only one join in the query that can be reused by multiple filters.