Ash FrameworkAF
Ash Framework8mo ago
7 replies
Marco Dell'Olio

before_action in bulk_create

I'm implementing some logic that would bulk_destroy existing records and bulk_create in a transaction


  create :replace_signatures do
      accept :*

      change relate_actor(:user)

      change fn changeset, context ->
        changeset
        |> Ash.Changeset.before_action(fn changeset ->
          Emails.EmailSignature.list_signatures!(
            actor: context.actor,
            tenant: context.tenant
          )
          |> Ash.bulk_destroy!(:destroy, %{}, tenant: context.tenant)

          changeset
        end)
      end
    end
  end


and then

  Ash.bulk_create(new_signatures, EmailSignature, :replace_signatures,
          actor: %{id: user.id},
          tenant: org.id,
          return_records?: true,
          assume_casted?: true
        )


but the before_action gets called before every batch, what's the idiomatic way to achieve this behavior and having the before_action running only once in transaction?

Thanks
Was this page helpful?