Modifying results after reading

Hi, 👋 I need to manipulate the data returned from a database query. I found out how to do my own read based on Ash.Resource.ManualRead, but how can I call the default logic to apply my changes afterwards? Or is there some kind of after_read hook where I could do so? I would appreciate som pointers… 😇
6 Replies
ZachDaniel
ZachDaniel3y ago
Hello! You can use a “preparation” and an after action hook to do what you want to do. Have you used query preparations before?
Jan Ulbrich
Jan UlbrichOP3y ago
No, didn’t use it before, Looking into the docs… 📚
ZachDaniel
ZachDaniel3y ago
You’re looking at something like:
prepare fn query, _ ->
Ash.Query.after_action(fn _query, results ->
{:ok, transform(results)}
end)
end
prepare fn query, _ ->
Ash.Query.after_action(fn _query, results ->
{:ok, transform(results)}
end)
end
That can go in an individual action Or in the global preparations block to run on all reads
Jan Ulbrich
Jan UlbrichOP3y ago
Ah, got it! I want to integrate it as transparent as possible with the AshJsonApi layer. Thanks for the pointer! 👏 Waaaay better than my approach. Thank you so much! Maybe you could change the title to something like "Modify results after reading from database"? My approach using Ash.Resource.ManualRead was a blind end…
ZachDaniel
ZachDaniel3y ago
👍👍 will do! Please add the resolved tag and close the post if it’s been resolved 😀😀
Terris
Terris3y ago
Minor nitpick but could you please fix the title spelling @Zach Daniel since these are more or permanent posts for searching ?

Did you find this page helpful?