Skip to content

Commit

Permalink
improvement: add data_layer? option to Ash.calculate/3
Browse files Browse the repository at this point in the history
This option allows you to force us to use the data layer
instead of attempting to resolve expressions in memory.
  • Loading branch information
zachdaniel committed Jan 1, 2025
1 parent 088d030 commit 7fa8dc6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/ash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@ defmodule Ash do
A record to use as the base of the calculation
"""
],
data_layer?: [
type: :boolean,
doc: """
Set to `true` to require that the value be computed within the data layer. Only works for calculations that define an expression.
"""
],
domain: [
type: {:spark, Ash.Domain},
doc: "The domain to use for the action"
Expand Down
14 changes: 9 additions & 5 deletions lib/ash/actions/read/calculations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ defmodule Ash.Actions.Read.Calculations do

evaled =
try do
Ash.Expr.eval(expr,
record: record,
resource: resource,
unknown_on_unknown_refs?: true
)
if opts[:data_layer?] do
:unknown
else
Ash.Expr.eval(expr,
record: record,
resource: resource,
unknown_on_unknown_refs?: true
)
end
rescue
_ ->
:unknown
Expand Down

0 comments on commit 7fa8dc6

Please sign in to comment.