tellesleandro
How to compose queries?
Ecto allows to compose a query, like this:
def for_patio(query \\ __MODULE__, %Patio{} = patio) do
query
|> join(:inner, [pe], _ in assoc(pe, :patios), as: :patios)
|> where([patios: p], p.id == ^patio.id)
end
def for_parceiro(query \\ __MODULE__, %Parceiro{} = parceiro, voucher) do
query
|> join(:inner, [pe], ape in assoc(pe, :parceiros_precos_estadias),
as: :parceiros_precos_estadias
)
|> where(
[parceiros_precos_estadias: ape],
ape.parceiro_id == ^parceiro.id and
ape.voucher == ^voucher
)
end
I can query the resource in different ways:
- Resource.for_patio(patio) |> Repo.all
- Resource.for_parceiro(parceiro, voucher) |> Repo.all
- Resource.for_patio(patio) |> Resource.for_parceiro(parceiro, voucher) |> Repo.all
How do I do that using Ash?
Marked As Solved
zachdaniel
Honestly it just depends on how you’re using it. For instance, if you’ve got a live view somewhere, you can do this:
assign(socket, :published_posts, Post.published!())
But you can also do
assign(socket, :published_posts, Post.read!(query: Ash.Query.filter(published == true))
If you are considering making a module somewhere with functions like:
def published_posts do
Post.read!(query: Ash.Query.filter(published == true)
end
Then at that point I would suggest making it an action, i.e
read :published do
filter expr(published == true)
end
If you need caller-level composition, then calculations are a good choice.
I know it would be nice if there was “one right answer” but in these case it often comes down to what kind of interface you would need. The general rule of thumb though is: when in doubt, put stuff in an action that does specifically what you need.
Also Liked
zachdaniel
No problem
I appreciate your questions, and I while we definitely want to improve the documentation, having these kinds of questions here in the forum will help other people when they have similar questions. Documentation improvements are a longer term type thing, but this will help people in the short term ![]()
yasoob
Hey @tellesleandro i just wanna say that your questions and the ensuing discussion is very helpful for newbies like me. Thanks for asking these questions! ![]()
zachdaniel
You’d use calculations for this:
calculations do
calculate :for_patio, :boolean, expr(exists(patios, id == ^arg(:id)) do
argument :id, :uuid, allow_nil?: false
end
calculate :for_parceiro, :boolean, expr(exists(parceiros_precos_estadias, parceiro_id == ^arg(:parceiro_id) and voucher == ^arg(:voucher)))
end
And then you can query like
query =
Resource
|> Ash.Query.filter(for_parceiro(parceiro_id: parceiro_id, voucher: voucher))
# or
query =
Resource
|> Ash.Query.filter(for_patio(patio_id: patio_id))
# or combine them
query =
Resource
|> Ash.Query.filter(for_parceiro(parceiro_id: parceiro_id, voucher: voucher) and for_patio(patio_id: patio_id))
# they can also be combined in ways you can't combine your example
query =
Resource
|> Ash.Query.filter(for_parceiro(parceiro_id: parceiro_id, voucher: voucher) or for_patio(patio_id: patio_id))
You cold then call a code interface and provide this query
YourResource.read(..., query: query)
Or use the underlying api for it
YourResource
|> Ash.Query.filter(....)
|> YourApi.read!()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









