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
Sienhopist
SienhopistOP•4mo ago
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
ken-kost
ken-kost•4mo ago
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:
aggregates do
list :full_urls, :my_resource, :full_url do
public? true
end
end
aggregates do
list :full_urls, :my_resource, :full_url do
public? true
end
end
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!()
Oliver
Oliver•4mo ago
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
Sienhopist
SienhopistOP•4mo ago
How do I do that?
Oliver
Oliver•4mo ago
Strict:true
Sienhopist
SienhopistOP•4mo ago
Not sure where to put that. I only see a strict option in the Reactor DSL
Oliver
Oliver•4mo ago
Its on load You still need to .list Point was that it will load only the relevant columns
Sienhopist
SienhopistOP•4mo ago
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.
Oliver
Oliver•4mo ago
Because the id on a resource can never be not set if loaded from db Its the baseline

Did you find this page helpful?