elixir
Use this skill when building with Elixir — Phoenix framework, OTP, BEAM, LiveView, Ecto, supervision trees. This skill enforces: OTP design principles, Phoenix context boundaries, LiveView state management, Ecto schema conventions, supervision tree structure. Do NOT use for: non-Elixir projects, frontend JavaScript, simple scripts better suited for Bash/Python.
j4flmao · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add j4flmao/agent-skills --skill elixir技能说明
name: elixir description: > Use this skill when building with Elixir — Phoenix framework, OTP, BEAM, LiveView, Ecto, supervision trees. This skill enforces: OTP design principles, Phoenix context boundaries, LiveView state management, Ecto schema conventions, supervision tree structure. Do NOT use for: non-Elixir projects, frontend JavaScript, simple scripts better suited for Bash/Python. version: "1.0.0" author: "j4flmao" license: "MIT" compatibility: claude-code: true cursor: true codex: true windsurf: true tags: [backend, elixir, phase-10]
Elixir
Purpose
Build fault-tolerant, concurrent applications with Elixir/OTP — Phoenix web layer, LiveView interactivity, Ecto persistence, supervision trees, process architecture.
Agent Protocol
Trigger
User request includes: Elixir, Phoenix, Phoenix LiveView, OTP, BEAM, Elixir macros, Ecto, mix, phx.gen, Supervisor, GenServer, Phoenix channels, Phoenix PubSub, Elixir processes, iex.
Input Context
- Framework (Phoenix, Phoenix LiveView, bare Elixir)
- Persistence (Ecto with PostgreSQL, Ecto with SQLite, ETS)
- State management (GenServer, Agent, ETS, Phoenix PubSub)
- Deployment (Elixir releases, Docker, Gigalixir, Fly.io)
Output Artifact
Phoenix project structure, OTP supervision tree, Ecto schema, LiveView module, router config.
Response Format
Produce artifact directly. No preamble, no postamble, no explanations. No filler, no hedging, no transitions. Strip articles a/an/the where unambiguous. Compress output — why use many token when few do trick.
Completion Criteria
- Phoenix project generated with mix phx.new
- Contexts separated by domain boundary
- Ecto schemas with proper associations
- LiveView with state management and event handling
- Supervision tree with appropriate restart strategy
Max Response Length
4096 tokens
Workflow
Step 1: Phoenix Project Setup
# Install Phoenix
mix archive.install hex phx_new
# Create new Phoenix app
mix phx.new my_app --database postgres
mix phx.new my_app --database sqlite3 # Alternative
# Setup database
mix ecto.create
mix ecto.migrate
# Start server
mix phx.server
my_app/
lib/
my_app/
application.ex # OTP application start
repo.ex # Ecto Repo
accounts/ # Context: accounts
user.ex # Ecto schema
user_notifier.ex # Boundary call
user_token.ex
accounts.ex # Context module (public API)
catalog/ # Context: catalog
product.ex
category.ex
catalog.ex
store/ # Context: orders
order.ex
line_item.ex
store.ex
web/
endpoint.ex # Phoenix endpoint
router.ex # Router
controllers/ # Controllers (non-LiveView)
user_session_controller.ex
live/ # LiveViews
product_live/
index.ex
show.ex
cart_live/
index.ex
components/ # Shared components
layout.ex # App layout
navbar.ex
product_card.ex
templates/ # Templates (non-LiveView)
layout/
mailer.ex # Mailer (Swoosh/Bamboo)
my_app.ex # Module aliases
priv/
repo/
migrations/
20250101000000_create_users.exs
config/
config.exs
dev.exs
prod.exs
runtime.exs
mix.exs
Step 2: Ecto Schema and Migration
# priv/repo/migrations/20250101000000_create_users.exs
defmodule MyApp.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users, primary_key: false) do
add :id, :uuid, primary_key: true, default: fragment("gen_random_uuid()")
add :email, :string, null: false
add :username, :string, null: false
add :hashed_password, :string, null: false
add :role, :string, default: "user"
add :confirmed_at, :naive_datetime
add :deleted_at, :naive_datetime
timestamps()
end
create unique_index(:users, [:email])
create unique_index(:users, [:username])
create index(:users, [:deleted_at])
end
end
# lib/my_app/accounts/user.ex
defmodule MyApp.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "users" do
field :email, :string
field :username, :string
field :role, :string, default: "user"
field :confirmed_at, :naive_datetime
field :deleted_at, :naive_datetime
has_many :orders, MyApp.Store.Order
has_one :profile, MyApp.Accounts.Profile
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:email, :username, :role])
|> validate_required([:email, :username])
|> validate_format(:email, ~r/@/)
|> validate_length(:username, min: 3, max: 30)
|> unique_constraint(:email)
|> unique_constraint(:username)
end
end
Step 3: Context Boundary
# lib/my_app/accounts/accounts.ex
defmodule MyApp.Accounts do
@moduledoc """
Accounts context — user registration, authentication, profile management.
"""
import Ecto.Query, warn: false
alias MyApp.Repo
alias MyApp.Accounts.{User, UserToken, UserNotifier}
@doc """
Registers new user.
"""
def register_user(attrs) do
%User{}
|> User.registration_changeset(attrs)
|> Repo.insert()
end
@doc """
Returns user by id.
"""
def get_user!(id), do: Repo.get!(User, id)
@doc """
Authenticates user by email and password.
"""
def authenticate_by_email(email, password) do
user = Repo.get_by(User, email: String.downcase(email))
case check_password(user, password) do
true -> {:ok, user}
false -> {:error, :invalid_credentials}
end
end
defp check_passwo
阅读完整来源 ↗