Skip to content

4.0: Return a single record from Ash.read when the action has get?: true #1800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ defmodule Ash do
defp do_read_one(query, action, opts) do
query
|> Ash.Actions.Read.unpaginated_read(action, opts)
|> Ash.Helpers.unwrap_one()
|> Ash.Helpers.unwrap_one_if(!action.get?)
|> case do
{:ok, nil} ->
if opts[:not_found_error?] do
Expand Down
17 changes: 15 additions & 2 deletions lib/ash/actions/read/read.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ defmodule Ash.Actions.Read do
end

@spec run(Ash.Query.t(), Ash.Resource.Actions.action(), Keyword.t()) ::
{:ok, Ash.Page.page() | list(Ash.Resource.record())}
| {:ok, Ash.Page.page() | list(Ash.Resource.record()), Ash.Query.t()}
{:ok, Ash.Page.page() | Ash.Resource.record() | list(Ash.Resource.record())}
| {:ok, Ash.Page.page() | Ash.Resource.record() | list(Ash.Resource.record()),
Ash.Query.t()}
| {:error, term}
def run(query, action, opts \\ [])

Expand Down Expand Up @@ -373,6 +374,7 @@ defmodule Ash.Actions.Read do
query,
opts
)
|> unwrap_if_get(query.action)
|> add_query(query, opts)
else
{:error, %Ash.Query{errors: errors} = query} ->
Expand Down Expand Up @@ -1839,6 +1841,17 @@ defmodule Ash.Actions.Read do
end
end

defp unwrap_if_get(result, action) do
if action && action.get? do
case result do
[] -> nil
[record | _] -> record
end
else
result
end
end

@doc false
def add_page(data, action, count, sort, original_query, opts) do
cond do
Expand Down
3 changes: 3 additions & 0 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,9 @@ defmodule Ash.Changeset do
{:ok, nil} ->
changeset

{:ok, []} ->
changeset

{:ok, _} ->
error =
Ash.Error.Changes.InvalidChanges.exception(
Expand Down
12 changes: 12 additions & 0 deletions lib/ash/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ defmodule Ash.Helpers do
end
end

def unwrap_one_if(result, condition) do
if condition do
unwrap_one(result)
else
result
end
end

def unwrap_one({:error, error}) do
{:error, error}
end
Expand Down Expand Up @@ -339,6 +347,10 @@ defmodule Ash.Helpers do
{:error, error}
end

def unwrap_one(struct) when is_struct(struct) do
{:ok, struct}
end

def resource_from_data!(data, query, opts) do
if opts[:resource] do
opts[:resource]
Expand Down
21 changes: 21 additions & 0 deletions test/actions/read_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ defmodule Ash.Test.Actions.ReadTest do
end

read :get_by_id do
get?(true)
get_by(:id)
end

read :get_by_id_and_uuid do
get?(true)
get_by([:id, :uuid])
end
end
Expand Down Expand Up @@ -401,6 +403,25 @@ defmodule Ash.Test.Actions.ReadTest do
|> Ash.Query.offset(1)
|> Ash.read()
end

test "with get?: true actions, it returns the record instead of a list" do
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "test", contents: "yeet"})
|> Ash.create!()

assert {:ok, %Post{}} =
Post
|> Ash.Query.for_read(:get_by_id, %{id: post.id})
|> Ash.read()
end

test "with get?: true actions, it returns nil for missing records" do
assert {:ok, nil} =
Post
|> Ash.Query.for_read(:get_by_id, %{id: Ash.UUID.generate()})
|> Ash.read()
end
end

describe "Ash.read!/2" do
Expand Down