ash/documentation/topics/calculations.md
2020-08-25 01:10:17 -04:00

1.9 KiB

Calculations

Calculations in Ash allow for displaying complex values as a top level value of a resource. They are relatively limited in their current form, supporting only functional calculations, where you provide a module that takes a list of records and returns a list of values for that calculation. Eventually, there will be support for calculations that can be embedded into the data layer(for things like postgres) that will allow for sorting and filtering on calculated data.

Declaring calculations on a resource

Example:

defmodule Concat do
  # An example concatenation calculation, that accepts the delimeter as an argument, and the fields to concatenate as options
  use Ash.Calculation, type: :string

  def init(opts) do
    if opts[:keys] && is_list(opts[:keys]) && Enum.all?(opts[:keys], &is_atom/1) do
      {:ok, opts}
    else
      {:error, "Expected a `keys` option for which keys to concat"}
    end
  end

  def calculate(records, opts, %{separator: separator}) do
    Enum.map(records, fn record ->
      Enum.map_join(opts[:keys], separator, fn key ->
        to_string(Map.get(record, key))
      end)
    end)
  end
end

# Usage in a resource
calculations do
  calculate :full_name, {Concat, keys: [:first_name, :last_name]} do
    argument :separator, :string
  end
end

See the documentation for the calculations section in Ash.Resource.Dsl.calculations/1 for more information.

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 calculations in the query

Example:

User
|> Ash.Query.new()
|> Ash.Query.calculate(:full_name, {Concat, keys: [:first_name, :last_name]}, %{separator: ","})

See the documentation for Ash.Query.calculate/4 for more information.