AshReqOpt

View Source

Shortcut DSL for attributes and relationships in Ash resources.

For attribute

DSLallow_nil?public?
attribute (Ash)truefalse
reqfalsetrue
req_prvfalsefalse
opttruetrue
opt_prvtruefalse

For belongs_to

DSLallow_nil?public?
belongs_to (Ash)truefalse
req_belongs_tofalsetrue
req_prv_belongs_tofalsefalse
opt_belongs_totruetrue
opt_prv_belongs_totruefalse

Usage Example

defmodule MyApp.User do
  use Ash.Resource, extensions: [AshReqOpt]

  attributes do
    uuid_primary_key :id

    # Required attributes
    req :email, :string    # allow_nil?: false, public?: true
    req_prv :password_hash, :string    # allow_nil?: false, public?: false

    # Optional attributes
    opt :name, :string     # allow_nil?: true, public?: true
    opt_prv :last_login_at, :utc_datetime  # allow_nil?: true, public?: false

    # Original attribute macro still works
    attribute :nickname, :string, allow_nil?: true
  end

  relationships do
    # Required relationships
    req_belongs_to :company, Company    # allow_nil?: false, public?: true
    req_prv_belongs_to :created_by, User    # allow_nil?: false, public?: false

    # Optional relationships
    opt_belongs_to :manager, User       # allow_nil?: true, public?: true
    opt_prv_belongs_to :updated_by, User    # allow_nil?: true, public?: false

    # Original belongs_to macro still works
    belongs_to :department, Department, allow_nil?: true
  end
end

Installation

Add ash_req_opt to your list of dependencies in mix.exs:

def deps do
  [
    {:ash_req_opt, "~> 0.2.0"}
  ]
end

License

MIT