一键导入
builderx-api-schemas
Guidelines for creating Ecto Schemas in builderx_api, including composite primary keys, custom json function and Ecto.Changeset.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Guidelines for creating Ecto Schemas in builderx_api, including composite primary keys, custom json function and Ecto.Changeset.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Phoenix Channel patterns in landing_page_backend — real-time collaboration, WebSocket topics, broadcasting, and channel structure.
Guide to Context module patterns in landing_page_backend — Ecto queries, dynamic query building, Ecto.Multi, and Outbox messaging.
Guide to Phoenix Controller structure in landing_page_backend using FallbackController, tuple responses, and permission plugs.
Patterns for creating Oban background workers in landing_page_backend — queue configuration, job arguments, retry strategies, and dynamic query processing.
Custom Plug middleware patterns in landing_page_backend — authentication (JWT/Passport), authorization, permission checking, and rate limiting.
Guidelines for creating Ecto Schemas in landing_page_backend, including binary UUID primary keys, virtual fields, custom json/1 function, and multiple changeset patterns.
| name | builderx_api-schemas |
| description | Guidelines for creating Ecto Schemas in builderx_api, including composite primary keys, custom json function and Ecto.Changeset. |
| metadata | {"author":"Vũ Lưu","version":"2026.03.25","source":"Source code builderx_api"} |
Conventions and structures for defining Ecto Schemas in the builderx_api project.
| Topic | Description | Reference |
|---|---|---|
| Schema Definition | Primary keys, site_id attribute, timestamps | core-schema |
| JSON Serialization | Custom json/1 and json/2 implementation in Ecto Schema | core-schema |
defmodule BuilderxApi.Products.Product do
use Ecto.Schema
import Ecto.Changeset
alias BuilderxApi.Sites.Site
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
@non_required_fields [:id, :inserted_at, :updated_at]
schema "products" do
belongs_to(:site, Site, type: Ecto.UUID, primary_key: true)
field :name, :string
timestamps(type: :utc_datetime_usec)
end
def changeset(%__MODULE__{} = product, attrs \\ %{}) do
fields = __schema__(:fields) -- @non_required_fields
product
|> cast(attrs, fields)
|> validate_required([:site_id, :name])
end
# Custom serializer instead of using Phoenix Views
def json(%__MODULE__{} = product) do
fields = __schema__(:fields)
data = Map.take(product, fields)
data =
case Map.fetch(product, :site) do
{:ok, %Ecto.Association.NotLoaded{}} ->
data
{:ok, value} ->
Map.put(data, :site, Site.json(value))
_ ->
data
end
data
end
def json(products) when is_list(products) do
Enum.map(products, &json(&1))
end
def json(_), do: nil
end