Is there a way to chain preparations on a query directly without having to go through an action? I went through the online documentation, but I could not find how to do it.
I am trying to do something that looks like this.
MyResource
|> Ash.Query.prepare(MyResource.Preparations.Prepartion1)
|> Ash.Query.prepare(MyResource.Preparations.Prepartion2)
|> Ash.read()
There is not a built in way, no. You could manually call their prepare
functions, but you’d be responsible for setting the opts/context etc.
MyResource
|> MyResource.Preparations.Prepartion1.prepare([], context)
|> ...
|> Ash.read()
1 Like
It would be a nice feature to have. I think it make preparations more reusable.
There are some complexities with that idea. Preparations are meant to be composed by actions, and often we enhance these kinds of behaviour modules with additional callbacks, and then situationally use those new callbacks. If it became standard practice to use preparations outside of actions, it would limit our options for enhancing preparations over time.
1 Like
Actually as I think about it, it’s ok as-it-is. It’s just a bit inconveniencing that one has to pass empty opts and context even when not needed, but I think it is still readable and reusable.
Helpcenter.KnowledgeBase.Category
|> Helpcenter.Preparations.LimitTo5.prepare([], [])
|> Helpcenter.Preparations.MonthToDate.prepare([], [])
|> Helpcenter.Preparations.OrderByMostRecent.prepare([], [])
|> Ash.read!()