Ash FrameworkAF
Ash Framework3y ago
13 replies
Blibs

Add conditions to upsert

Is there any way to add conditionals when doing an upsert in a create action?

For example, I have this resource:

defmodule Pacman.Markets.Blibs do
  use Ash.Resource, data_layer: AshPostgres.DataLayer

  code_interface do
    define_for Pacman.Markets

    define :create
  end

  attributes do
    attribute :id, :uuid do
      default &Ash.UUID.generate/0
      primary_key? true
      allow_nil? false
    end

    attribute :name, :string do
      allow_nil? false

      constraints max_length: 255
    end

    attribute :transaction_time, :integer, allow_nil?: false

    attribute :deleted?, :boolean, allow_nil?: false, default: false

    timestamps(private?: false)
  end

  postgres do
    table "blibs"

    repo Pacman.Repo

    migration_types transaction_time: :bigint, name: {:varchar, 255}
  end

  actions do
    defaults [:read, :destroy]
    
    create :create do
      primary? true

      upsert? true
    end
  end
end
Was this page helpful?