View Source EctoSparkles (bonfire_umbrella v0.9.8-cooperation-beta.31)

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

Link to this macro

join_preload(query, associations)

View Source (macro)

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()
Link to this macro

proload(query, qual \\ :left, associations)

View Source (macro)

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]}
])
Link to this macro

reusable_join(query, qual \\ :left, bindings, expr, opts)

View Source (macro)

reusable_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.