Skip to content

Commit 7ab8f60

Browse files
committed
chore: small potential optimizations in Ash.bulk_create
1 parent 0691129 commit 7ab8f60

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

lib/ash/actions/create/bulk.ex

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ defmodule Ash.Actions.Create.Bulk do
464464

465465
{batch, must_be_simple_results} =
466466
batch
467-
|> Stream.map(fn changeset ->
467+
|> Enum.map(fn changeset ->
468468
Ash.Changeset.require_values(
469469
changeset,
470470
:create
@@ -1468,61 +1468,56 @@ defmodule Ash.Actions.Create.Bulk do
14681468
ref
14691469
)
14701470
|> then(fn records ->
1471-
if opts[:return_records?], do: records, else: []
1472-
end)
1473-
|> then(fn records ->
1474-
select =
1475-
if opts[:select] do
1476-
List.wrap(opts[:select])
1477-
else
1478-
resource |> Ash.Resource.Info.public_attributes() |> Enum.map(& &1.name)
1479-
end
1471+
if opts[:return_records?] do
1472+
select =
1473+
if opts[:select] do
1474+
List.wrap(opts[:select])
1475+
else
1476+
resource |> Ash.Resource.Info.public_attributes() |> Enum.map(& &1.name)
1477+
end
14801478

1481-
case Ash.load(
1482-
records,
1483-
select,
1484-
context: %{private: %{just_created_by_action: action.name}},
1485-
reuse_values?: true,
1486-
domain: domain,
1487-
action: Ash.Resource.Info.primary_action(resource, :read) || action,
1488-
tenant: opts[:tenant],
1489-
actor: opts[:actor],
1490-
authorize?: opts[:authorize?],
1491-
tracer: opts[:tracer]
1492-
) do
1493-
{:ok, records} ->
1494-
Ash.load(
1495-
records,
1496-
List.wrap(opts[:load]),
1497-
context: %{private: %{just_created_by_action: action.name}},
1498-
domain: domain,
1499-
tenant: opts[:tenant],
1500-
action: Ash.Resource.Info.primary_action(resource, :read) || action,
1501-
reuse_values?: true,
1502-
actor: opts[:actor],
1503-
authorize?: opts[:authorize?],
1504-
tracer: opts[:tracer]
1505-
)
1506-
|> case do
1507-
{:ok, records} ->
1508-
{:ok, Enum.reject(records, & &1.__metadata__[:private][:missing_from_data_layer])}
1479+
case Ash.load(
1480+
records,
1481+
select,
1482+
context: %{private: %{just_created_by_action: action.name}},
1483+
reuse_values?: true,
1484+
domain: domain,
1485+
action: Ash.Resource.Info.primary_action(resource, :read) || action,
1486+
tenant: opts[:tenant],
1487+
actor: opts[:actor],
1488+
authorize?: opts[:authorize?],
1489+
tracer: opts[:tracer]
1490+
) do
1491+
{:ok, records} ->
1492+
Ash.load(
1493+
records,
1494+
List.wrap(opts[:load]),
1495+
context: %{private: %{just_created_by_action: action.name}},
1496+
domain: domain,
1497+
tenant: opts[:tenant],
1498+
action: Ash.Resource.Info.primary_action(resource, :read) || action,
1499+
reuse_values?: true,
1500+
actor: opts[:actor],
1501+
authorize?: opts[:authorize?],
1502+
tracer: opts[:tracer]
1503+
)
1504+
|> case do
1505+
{:ok, records} ->
1506+
Enum.reject(records, & &1.__metadata__[:private][:missing_from_data_layer])
15091507

1510-
{:error, error} ->
1511-
{:error, error}
1512-
end
1508+
{:error, error} ->
1509+
store_error(ref, error, opts)
1510+
[]
1511+
end
15131512

1514-
other ->
1515-
other
1513+
{:error, error} ->
1514+
store_error(ref, error, opts)
1515+
[]
1516+
end
1517+
else
1518+
[]
15161519
end
15171520
end)
1518-
|> case do
1519-
{:ok, records} ->
1520-
records
1521-
1522-
{:error, error} ->
1523-
store_error(ref, error, opts)
1524-
[]
1525-
end
15261521
end
15271522

15281523
defp run_bulk_after_changes(

0 commit comments

Comments
 (0)