mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
docs: add more docs for aggregates
This commit is contained in:
parent
2c197c03df
commit
cee0fc990a
1 changed files with 37 additions and 1 deletions
|
@ -14,7 +14,43 @@ aggregates do
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
The available aggregate types are:
|
## Using an aggregate
|
||||||
|
|
||||||
|
Aggregates are loaded and filtered on in the same way that calculations are. Lets look at some examples:
|
||||||
|
|
||||||
|
### Loading aggregates in a query or on records
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
User
|
||||||
|
|> Ash.Query.load(:count_of_posts)
|
||||||
|
|> Map.get(:count_of_posts)
|
||||||
|
# => 10
|
||||||
|
|
||||||
|
users
|
||||||
|
|> Ash.load!(:count_of_posts)
|
||||||
|
|> Enum.map(&(&1.count_of_posts)
|
||||||
|
# => [3, 5, 2]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Filtering on aggregates
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
require Ash.Query
|
||||||
|
|
||||||
|
User
|
||||||
|
|> Ash.Query.filter(count_of_posts > 10)
|
||||||
|
|> Ash.read!()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sorting aggregates
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
User
|
||||||
|
|> Ash.Query.sort(count_of_posts: :asc)
|
||||||
|
|> Ash.read!()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Aggregate types
|
||||||
|
|
||||||
- `count` - counts related items meeting the criteria.
|
- `count` - counts related items meeting the criteria.
|
||||||
- `exists` - checks if any related items meet the criteria.
|
- `exists` - checks if any related items meet the criteria.
|
||||||
|
|
Loading…
Reference in a new issue