Hey gang, looking for the most idiomatic place to put a function that essentially calls Ash.count/2
. It feels like there is an obvious way to define this as part of the code interface, but I don’t see it.
For example, if the resource is Item
, something like this:
resources do
resource MyApp.SomeDomain.Item do
...
# what I am thinking
define :items_count, action: :read, query: ...
end
end
Or just add a normal function in the domain module?
resources do
resource MyApp.SomeDomain.Item do
...
end
end
# keep it simple
def items_count do
Ash.count(MyApp.SomeDomain.Item)
end
Thanks all
So, there are two things to note here:
Functions are cool
Sometimes it can feel like it’s going against the grain in Ash to define functions. Ultimately it’s not
Your domain is like your context and it’s fine to define functions in it.
So your #keep it simple
comment is on point. If thats all it is then thats all it is 
Generic Actions are an option
There are use cases for generic actions, which are essentially just typed functions that can provide some benefits:
- You can write policies around them
- They emit traces into analytics
- They validate the types of their inputs
So what that looks like is an action on items like this:
action :item_count, :integer do
run fn _input, _context ->
Ash.count(__MODULE__)
end
end
And then in your domain:
resources do
resource MyApp.SomeDomain.Item do
define :item_count
end
end
At the end of the day, if you don’t need any of the benefits of a generic action, go with #1, otherwise #2.
3 Likes
I think that’s exactly what I was looking for. Makes sense, thanks for the clarity.
1 Like