Hi everyone
I recently published a small utility library called ash_req_opt
, and I wanted to share it here in case it might be useful for others building with Ash Framework.
What is it?
ash_req_opt
is a DSL shortcut for declaring attributes and relationships in Ash resources with commonly used allow_nil?
and public?
options.
Ash is powerful, but sometimes writing repetitive attribute
or belongs_to
definitions with the same flags gets a bit tedious. This library simplifies that by providing macros like req
, opt
, req_belongs_to
, etc.
Example
defmodule MyApp.User do
use Ash.Resource, extensions: [AshReqOpt]
attributes do
uuid_primary_key :id
req :email, :string
req_prv :password_hash, :string
opt :name, :string
opt_prv :last_login_at, :utc_datetime
# fallback to native Ash DSL still works
attribute :nickname, :string, allow_nil?: true
end
relationships do
req_belongs_to :company, Company
req_prv_belongs_to :created_by, User
opt_belongs_to :manager, User
opt_prv_belongs_to :updated_by, User
belongs_to :department, Department, allow_nil?: true
end
end
Itβs a small layer, but I hope it helps others too. Feedback, feature suggestions, or improvements are welcome!
Hex: https://hex.pm/packages/ash_req_opt
GitHub: