{@thread.name}

Jan Ulbrich
2023-01-23

Jan Ulbrich:

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… 😇

ZachDaniel:

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:

No, didn’t use it before, Looking into the docs… 📚

ZachDaniel:

You’re looking at something like:

prepare fn query, _ ->
  Ash.Query.after_action(fn _query, results ->
      {:ok, transform(results)}
  end)
end

ZachDaniel:

That can go in an individual action

ZachDaniel:

Or in the global preparations block to run on all reads

Jan Ulbrich:

Ah, got it! I want to integrate it as transparent as possible with the AshJsonApi layer. Thanks for the pointer! 👏

Jan Ulbrich:

Waaaay better than my approach. Thank you so much!

Jan Ulbrich:

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:

👍👍 will do!

ZachDaniel:

Modyfing results after reading

ZachDaniel:

Please add the resolved tag and close the post if it’s been resolved 😀😀

Terris:

Minor nitpick but could you please fix the title spelling <@197905764424089601> since these are more or permanent posts for searching ?

ZachDaniel:

Modifying results after reading