ash/documentation/topics/aggregates.md

31 lines
965 B
Markdown
Raw Normal View History

2020-08-10 19:51:28 +12:00
# Aggregates
Aggregates in Ash allow for retrieving summary information over groups of related data. A simple example might be to show the "count of published posts for a user".
2020-08-10 19:51:28 +12:00
## Declaring aggregates on a resource
Example:
2020-08-10 19:51:28 +12:00
```elixir
aggreates do
2020-08-10 19:51:28 +12:00
count :count_of_posts, :posts, filter: [published: true]
end
2020-08-10 19:51:28 +12:00
```
See the documentation for the aggregates section in `Ash.Resource.Dsl.aggregates/1` for more information.
The aggregates declared on a resource allow for declaring a set of named aggregates that can be used by extensions.
2020-09-25 16:36:50 +12:00
They can also be loaded in the query using `Ash.Query.load/2`, or after the fact using `c:Ash.Api.load/3`. Aggregates declared on the resource will be keys in the resource's struct.
2020-08-10 19:51:28 +12:00
## Custom aggregates in the query
Example:
2020-08-10 19:51:28 +12:00
```elixir
User
|> Ash.Query.new()
|> Ash.Query.aggregate(:count_of_posts, :count, :posts, filter: [published: true])
```
See the documentation for `Ash.Query.aggregate/4` for more information.