improvement: support lock option on read

improvement: data layer can? might get a dsl, not a resource
This commit is contained in:
Zach Daniel 2023-04-28 13:38:03 -04:00
parent b1205f9a8e
commit b28ccf2e3c
3 changed files with 10 additions and 3 deletions

View file

@ -1502,6 +1502,13 @@ defmodule Ash.Api do
def read(api, query, opts) do
query = Ash.Query.set_api(query, api)
query =
if opts[:lock] do
Ash.Query.lock(query, opts[:lock])
else
query
end
with {:ok, opts} <- Spark.OptionsHelpers.validate(opts, @read_opts_schema),
{:ok, action} <- get_action(query.resource, opts, :read, query.action),
{:ok, action} <- pagination_check(action, query.resource, opts) do

View file

@ -173,7 +173,7 @@ defmodule Ash.DataLayer do
@callback in_transaction?(Ash.Resource.t()) :: boolean
@callback source(Ash.Resource.t()) :: String.t()
@callback rollback(Ash.Resource.t(), term) :: no_return
@callback can?(Ash.Resource.t(), feature()) :: boolean
@callback can?(Ash.Resource.t() | Spark.Dsl.t(), feature()) :: boolean
@callback set_context(Ash.Resource.t(), data_layer_query(), map) ::
{:ok, data_layer_query()} | {:error, term}
@ -479,7 +479,7 @@ defmodule Ash.DataLayer do
end
end
@spec can?(feature, Ash.Resource.t()) :: boolean
@spec can?(feature, Ash.Resource.t() | Spark.Dsl.t()) :: boolean
def can?(feature, resource) do
data_layer = Ash.DataLayer.data_layer(resource)
data_layer.can?(resource, feature)

View file

@ -2063,7 +2063,7 @@ defmodule Ash.Query do
This must be run while in a transaction, and is not supported by all data layers.
"""
@spec lock(t() | Ash.Resource.t(), Ash.DataLayer.lock_type()) :: t()
def lock(query, nil), do: query
def lock(query, nil), do: %{query | lock: nil}
def lock(query, lock_type) do
query = to_query(query)