View Source Ash.Calculation behaviour (ash v2.18.2)

The behaviour for a calculation module

Use select/2 to apply a select statement when the calculation is loaded. This does not apply in the case that you are loading on existing resources using MyApi.load. It also doesn't apply when the calculation is used in a filter or sort, because it is not necessary to select fields to power filters done in the data layer.

Summary

Types

@type context() :: %{
  :actor => term() | nil,
  :tenant => String.t() | nil,
  :authorize? => boolean() | nil,
  :tracer => module() | nil,
  optional(atom()) => any()
}
@type opts() :: Keyword.t()

Callbacks

Link to this callback

calculate(records, opts, context)

View Source (optional)
@callback calculate(
  records :: [Ash.Resource.record()],
  opts :: opts(),
  context :: context()
) ::
  {:ok, [term()]} | [term()] | {:error, term()} | :unknown
@callback describe(opts :: opts()) :: String.t()
Link to this callback

expression(otps, context)

View Source (optional)
@callback expression(otps :: opts(), context :: context()) :: any()
@callback init(opts :: opts()) :: {:ok, opts()} | {:error, term()}
Link to this callback

load(query, opts, context)

View Source
@callback load(query :: Ash.Query.t(), opts :: opts(), context :: context()) ::
  atom() | [atom()] | Keyword.t()
Link to this callback

select(query, opts, context)

View Source
@callback select(query :: Ash.Query.t(), opts :: opts(), context :: context()) :: [atom()]