docs: finish docs on calculations

This commit is contained in:
Zach Daniel 2020-08-25 01:09:49 -04:00
parent 3b1bde6a39
commit 9d4fc1e2a0
No known key found for this signature in database
GPG key ID: C377365383138D4B
2 changed files with 13 additions and 3 deletions

View file

@ -46,14 +46,14 @@ See the documentation for the calculations section in `Ash.Resource.Dsl.calculat
The calculations declared on a resource allow for declaring a set of named calculations that can be used by extensions.
They can also be loaded in the query using `Ash.Query.load/2`, or after the fact using `c:Ash.Api.load/2`. Calculations declared on the resource will be keys in the resource's struct.
## Custom aggregates in the query
## Custom calculations in the query
Example:
```elixir
User
|> Ash.Query.new()
|> Ash.Query.aggregate(:count_of_posts, :count, :posts, filter: [published: true])
|> Ash.Query.calculate(:full_name, {Concat, keys: [:first_name, :last_name]}, %{separator: ","})
```
See the documentation for `Ash.Query.aggregate/4` for more information.
See the documentation for `Ash.Query.calculate/4` for more information.

View file

@ -364,6 +364,16 @@ defmodule Ash.Query do
end
end
@doc """
Adds a calculation to the query.
Calculations are made available on the `calculations` field of the records returned
The `module_and_opts` argument accepts either a `module` or a `{module, opts}`. For more information
on what that module should look like, see `Ash.Calculation`.
More features for calculations, like passing anonymous functions, will be supported in the future.
"""
def calculate(query, name, module_and_opts, context \\ %{}) do
query = to_query(query)