What is the equivalent of Ecto's Repo.exists? in Ash?

Let's say I have a Post resource, and I want to create a read action that instead of returning a specific Post, I want it just to check if the post exists and return only true/false just like Ecto's Repo.exists? does. Is that possible with Ash?
5 Replies
ZachDaniel
ZachDaniel2y ago
Currently we do not have that, but it will be added at some point. You can write a generic action in the meantime to simulate it Or you could use Ash.Query.datalayer_query and pass that to ecto.
Blibs
BlibsOP2y ago
Here is my solution:
actions do
require Ash.Query

action :exists, :boolean do
argument :id, :uuid, allow_nil?: false

run(fn input, context ->
id = input.arguments.id
repo = AshPostgres.DataLayer.Info.repo(__MODULE__)

{:ok, ecto_query} =
__MODULE__ |> Ash.Query.filter(id == ^id) |> Ash.Query.data_layer_query()

{:ok, repo.exists?(ecto_query)}
end)
end
end
actions do
require Ash.Query

action :exists, :boolean do
argument :id, :uuid, allow_nil?: false

run(fn input, context ->
id = input.arguments.id
repo = AshPostgres.DataLayer.Info.repo(__MODULE__)

{:ok, ecto_query} =
__MODULE__ |> Ash.Query.filter(id == ^id) |> Ash.Query.data_layer_query()

{:ok, repo.exists?(ecto_query)}
end)
end
end
Are you aware of some way to to the same without having to use data_layer_query? I tried simulating the exists? feature with Ash.Query.select(1) |> Ash.Query.limit(1) but using 1 for the select function call is not supported.
ZachDaniel
ZachDaniel2y ago
doing select(:id) would be roughly equivalent but currently its either selecting a field or using the repo like you did
Robert Graff
Robert Graff2y ago
@Zach Daniel does this solution respect multitenancy and policies? Not sure if those are apply at the query or action level
ZachDaniel
ZachDaniel2y ago
It does not. Those happen at the action level. Actually I’d have to double check on multitenancy But that’s the reason I’d suggest opting for the select id approach.

Did you find this page helpful?