How to list calculation without raw queries
I have a resource with attributes scheme and url, and a calculation
:full_url
. The calculation basically just concats the two with "://"
in between. If I want to get a list of full urls for all my resources, I learned I can do this MyResource |> Ash.Query.load(:full_url) |> Ash.list!(:full_url)
. But I can't figure out how to do this as an aggregate or an action. Is it possible? Is this the right way to do it? I think aggregates always require a relationship.9 Replies
I figured out I can do it as a generic action, but I still feel like I'm missing a proper way to do it as a read action without using Ash.Query directly
if you want the action to return just the list of full url's then I think generic action is the way to go, because a read action always needs to return the resource record itself, nothing else.
not sure for the aggregate approach 🤔 maybe you could use list on the calculation field:
but yea, this would require a parent resource. So generic action I think is the way to go; it doesn't need arguments, you can give it a interface definition and then you could just call it
MyResource.full_urls!()
You also do a sparse load, loading only the id and the full url (plus what is needed to compute it if its not an expr()), should be more or less as optimized as you need vs ROI
How do I do that?
Strict:true
Not sure where to put that. I only see a strict option in the Reactor DSL
Its on load
You still need to .list
Point was that it will load only the relevant columns
Tbf I think it already does. The only question is why does the SQL query also contain
select p0."id"
when this calculation does not use IDs?
Anyways, I think this can be considered done.Because the id on a resource can never be not set if loaded from db
Its the baseline