Token导航 LogoToken导航

Agent Skill · 前端设计

ecto-schema-patterns(外图式模式)

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

ecto-schema-patterns 是前端设计类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为只读;许可证为 MIT,第三方扫描已通过,按安装量在前端设计类的 10,216 个 Skill 里排第 7,174。

总安装 412周安装 17GitHub Stars 142下载 135核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
MIT

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:ecto-schema-patterns(外图式模式)
来源仓库:https://github.com/thebushidocollective/han
仓库路径:skills/ecto-schema-patterns
安装命令:
npx skills add https://github.com/thebushidocollective/han --skill ecto-schema-patterns
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/thebushidocollective/han --skill ecto-schema-patterns

这个 Skill 是做什么的

前端设计类 Skill 关注页面结构、视觉规范和组件写法,输出通常是可运行的界面代码。

用于辅助前端页面、组件和样式开发,支持 React、Vue 等框架的代码生成与审查。适合整理组件结构、优化布局或定位性能问题,需结合项目路由和构建方式使用。避免生成孤立代码片段,涉及页面改动时应配合本地预览确认效果。安装方式:通过 npx skills add 命令从指定 GitHub 仓库安装。

建议确认权限范围和维护状态,避免触发不必要的文件操作或网络请求。

来源仓库内路径:skills/ecto-schema-patterns

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add https://github.com/thebushidocollective/han --skill ecto-schema-p…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    它标注为只读,先用只读任务试一次,确认行为符合预期再扩大使用范围。

常见问题

6 条

ecto-schema-patterns 怎么安装?

在终端执行 npx skills add https://github.com/thebushidocollective/han --skill ecto-schema-patterns。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

ecto-schema-patterns 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

ecto-schema-patterns 需要什么权限?

权限级别是只读。该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

ecto-schema-patterns 是免费的吗?

来源标注的许可证是 MIT。本站不代收任何费用,也不托管安装包;能否商用、要不要署名,以许可证原文为准。

ecto-schema-patterns 用的人多吗?

公开数据是累计安装 412 次,近一周安装 17 次,下载 135 次,GitHub 142 星。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

ecto-schema-patterns 安全吗?

第三方扫描结果为通过,但这只覆盖已知的静态检查项,不能替代自己读一遍源码和安装命令。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。
  • 3 个目录同时收录,说明这个 Skill 流传比较广。
  • 许可证明确为 MIT,商用和分发边界好判断。
  • 第三方扫描已通过,常见的高风险行为没有命中。

先想清楚的地方

  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理前端设计类任务、又不想每次都手写提示词的人。
  • 需要许可证清楚、并且有第三方扫描结果的团队项目。

不太适合

  • 需要厂商级技术支持和 SLA 的生产系统。

前端设计类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
frontend-design

前端设计 · Codex、Claude

8,823,360126,072只读MIT通过
vercel-composition-patterns

Vercel 组合模式 · Codex、Claude

3,858,81625,893需要联网MIT通过
vercel-react-native-skills

Vercel React native skills 前端 · Codex、Claude

2,468,66425,878只读MIT通过
adapt

adapt 搜索 · Codex、Claude

2,027,04023,125操作浏览器未明确通过
bolder

bolder 分析 · Codex、Claude

2,004,28823,130只读未明确通过
ecto-schema-patterns

外图式模式 · 本页

412142只读MIT通过

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

Ecto Schema Patterns

Master Ecto schemas to define robust data structures for your Elixir applications. This skill covers schema definitions, field types, associations, embedded schemas, and advanced patterns for modeling complex domain data.

Basic Schema Definition

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :name, :string
    field :email, :string
    field :age, :integer
    field :confirmed_at, :naive_datetime

    timestamps()
  end
end

Schemas map to database tables and define the structure of your data. Each schema has a source name (table name) and a list of fields with their types. The timestamps() macro automatically adds inserted_at and updated_at fields.

Field Types and Options

defmodule MyApp.Product do
  use Ecto.Schema

  schema "products" do
    # Standard field types
    field :title, :string
    field :description, :string
    field :price, :decimal
    field :quantity, :integer
    field :is_active, :boolean, default: true
    field :published_at, :utc_datetime

    # Enum type
    field :status, Ecto.Enum, values: [:draft, :published, :archived]

    # Map type for unstructured data
    field :metadata, :map

    # Array type
    field :tags, {:array, :string}

    # Binary type for binary data
    field :image_data, :binary

    # Virtual field (not persisted to database)
    field :display_price, :string, virtual: true

    timestamps()
  end
end

Ecto supports a wide range of field types including strings, integers, decimals, booleans, datetime types, enums, maps, arrays, and binary data. Virtual fields exist only in memory and are useful for computed values.

Using Map Type for Flexible Data

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :name, :string
    field :email, :string
    field :data, :map

    timestamps()
  end
end

# Usage
user = %MyApp.User{
  name: "John Doe",
  email: "john@example.com",
  data: %{
    preferences: %{
      theme: "dark",
      notifications: true
    },
    settings: %{
      language: "en"
    }
  }
}

The :map type allows storing arbitrary Elixir maps in the database, providing flexibility for unstructured or semi-structured data without requiring schema changes.

Embedded Schemas

defmodule MyApp.Address do
  use Ecto.Schema

  embedded_schema do
    field :street, :string
    field :city, :string
    field :state, :string
    field :zip_code, :string
    field :country, :string, default: "US"
  end
end

Embedded schemas define data structures that are not tied to a database table. They can be embedded within other schemas or used independently in memory for data validation and casting.

Embedding One Association

defmodule MyApp.Order do
  use Ecto.Schema

  schema "orders" do
    field :total, :decimal
    field :status, :string

    embeds_one :item, Item

    timestamps()
  end
end

defmodule MyApp.Item do
  use Ecto.Schema

  embedded_schema do
    field :title, :string
    field :price, :decimal
    field :quantity, :integer
  end
end

The embeds_one macro defines a one-to-one relationship with an embedded schema. The embedded data is stored as a JSON or map column in the parent table, not in a separate table.

Inline Embedded Schema Definition

defmodule MyApp.Parent do
  use Ecto.Schema

  schema "parents" do
    field :name, :string

    embeds_one :child, Child do
      field :name, :string
      field :age, :integer
    end

    timestamps()
  end
end

Schemas can be embedded inline using a do block, which creates a nested module (e.g., MyApp.Parent.Child). This is useful for simpler embedded structures that don't need to be defined separately.

Embedding Many Association

defmodule MyApp.Order do
  use Ecto.Schema

  schema "orders" do
    field :customer_name, :string
    field :total, :decimal

    embeds_many :items, OrderItem do
      field :product_name, :string
      field :quantity, :integer
      field :price, :decimal
    end

    timestamps()
  end
end

The embeds_many macro defines a one-to-many relationship with embedded schemas. Multiple embedded records are stored as a JSON array in the parent table.

Complex Embedded Schema with Custom Changeset

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :full_name, :string
    field :email, :string
    field :avatar_url, :string
    field :confirmed_at, :naive_datetime

    embeds_one :profile, Profile do
      field :online, :boolean
      field :dark_mode, :boolean
      field :visibility, Ecto.Enum, values: [:public, :private, :friends_only]
    end

    timestamps()
  end

  def changeset(%__MODULE__{} = user, attrs \\ %{}) do
    user
    |> Ecto.Changeset.cast(attrs, [:full_name, :email])
    |> Ecto.Changeset.cast_embed(:profile, required: true, with: &profile_changeset/2)
  end

  def profile_changeset(profile, attrs \\ %{}) do
    profile
    |> Ecto.Changeset.cast(attrs, [:online, :dark_mode, :visibility])
    |> Ecto.Changeset.validate_required([:online, :visibility])
  end
end

Custom changeset functions can be defined for embedded schemas using the :with option in cast_embed/3. This allows for specific validation logic on nested data.

Extracted Embedded Schema Module

# user/user.ex
defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :full_name, :string
    field :email, :string
    field :avatar_url, :string
    field :confirmed_at, :naive_datetime

    embeds_one :profile, MyApp.UserProfile

    timestamps()
  end
end

# user/user_profile.ex
defmodule MyApp.UserProfile do
  use Ecto.Schema

  embedded_schema do
    field :online, :boolean
    field :dark_mode, :boolean
    field :visibility, Ecto.Enum, values: [:public, :private, :friends_only]
  end

  def changeset(%__MODULE__{} = profile, attrs \\ %{}) do
    profile
    |> Ecto.Changeset.cast(attrs, [:online, :dark_mode, :visibility])
    |> Ecto.Changeset.validate_required([:online, :visibility])
  end
end

Extracting embedded schemas into dedicated modules improves organization and allows the embedded schema to have its own changeset functions, validations, and behavior.

Belongs To Association

defmodule MyApp.Comment do
  use Ecto.Schema

  schema "comments" do
    field :body, :string
    field :author, :string

    belongs_to :post, MyApp.Post

    timestamps()
  end
end

defmodule MyApp.Post do
  use Ecto.Schema

  schema "posts" do
    field :title, :string
    field :body, :string

    has_many :comments, MyApp.Comment

    timestamps()
  end
end

The belongs_to macro defines a foreign key relationship. By default, it creates a post_id field in the comments table. The parent schema typically defines the inverse relationship with has_many.

Custom Belongs To Field

defmodule MyApp.Comment do
  use Ecto.Schema

  schema "comments" do
    field :post_id, :integer
    belongs_to :post, MyApp.Post, define_field: false
  end
end

You can customize the foreign key field definition by setting define_field: false and manually defining the field. This is useful when you need special options on the foreign key field.

Has One Association

defmodule MyApp.Account do
  use Ecto.Schema

  schema "accounts" do
    field :email, :string

    has_one :profile, MyApp.Profile

    timestamps()
  end
end

defmodule MyApp.Profile do
  use Ecto.Schema

  schema "profiles" do
    field :name, :string
    field :age, :integer

    belongs_to :account, MyApp.Account

    timestamps()
  end
end

The has_one macro defines a one-to-one relationship where the foreign key is stored in the associated schema. The associated schema must have a corresponding belongs_to relationship.

Has Many Association

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :name, :string
    field :email, :string

    has_many :posts, MyApp.Post, foreign_key: :author_id

    timestamps()
  end
end

The has_many macro defines a one-to-many relationship. You can customize the foreign key name using the foreign_key option if it differs from the default convention.

Many to Many Association with Join Table

defmodule MyApp.Post do
  use Ecto.Schema

  schema "posts" do
    field :title, :string
    field :body, :string

    many_to_many :tags, MyApp.Tag,
      join_through: "posts_tags",
      on_replace: :delete

    timestamps()
  end
end

defmodule MyApp.Tag do
  use Ecto.Schema

  schema "tags" do
    field :name, :string

    many_to_many :posts, MyApp.Post,
      join_through: "posts_tags"

    timestamps()
  end
end

The many_to_many macro defines a many-to-many relationship through a join table. The join_through option specifies the table name, and on_replace::delete controls how the association is updated.

Many to Many with Join Schema

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :name, :string

    many_to_many :organizations, MyApp.Organization,
      join_through: MyApp.UserOrganization

    timestamps()
  end
end

defmodule MyApp.Organization do
  use Ecto.Schema

  schema "organizations" do
    field :name, :string

    many_to_many :users, MyApp.User,
      join_through: MyApp.UserOrganization

    timestamps()
  end
end

defmodule MyApp.UserOrganization do
  use Ecto.Schema

  @primary_key false
  schema "users_organizations" do
    belongs_to :user, MyApp.User
    belongs_to :organization, MyApp.Organization
    field :role, :string
    field :joined_at, :utc_datetime

    timestamps()
  end
end

Using a dedicated join schema (instead of just a table name) allows you to add additional fields to the join table, such as role or timestamp information.

Schema with Composite Primary Key

defmodule MyApp.UserOrganization do
  use Ecto.Schema

  @primary_key false
  schema "users_organizations" do
    belongs_to :user, MyApp.User, primary_key: true
    belongs_to :organization, MyApp.Organization, primary_key: true
    field :role, :string

    timestamps()
  end
end

Composite primary keys can be created by setting @primary_key false and marking the relevant belongs_to associations with primary_key: true.

Custom Primary Key

defmodule MyApp.Product do
  use Ecto.Schema

  @primary_key {:sku, :string, autogenerate: false}
  schema "products" do
    field :name, :string
    field :price, :decimal

    timestamps()
  end
end

You can customize the primary key field name, type, and autogeneration behavior using the @primary_key module attribute.

Schema Metadata Access

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :name, :string
    field :email, :string

    has_many :posts, MyApp.Post
    belongs_to :organization, MyApp.Organization

    timestamps()
  end
end

# Access schema metadata
MyApp.User.__schema__(:source)        # "users"
MyApp.User.__schema__(:fields)        # [:id, :name, :email, :organization_id, :inserted_at, :updated_at]
MyApp.User.__schema__(:primary_key)   # [:id]
MyApp.User.__schema__(:associations)  # [:posts, :organization]
MyApp.User.__schema__(:type, :name)   # :string

The __schema__/1 and __schema__/2 functions provide access to schema metadata at runtime, useful for metaprogramming and dynamic query building.

Self-Referencing Association

defmodule MyApp.Person do
  use Ecto.Schema

  schema "people" do
    field :name, :string

    many_to_many :relations, MyApp.Person,
      join_through: MyApp.Relationship,
      join_keys: [person_id: :id, relation_id: :id]

    timestamps()
  end
end

defmodule MyApp.Relationship do
  use Ecto.Schema

  @primary_key false
  schema "relationships" do
    belongs_to :person, MyApp.Person
    belongs_to :relation, MyApp.Person
    field :relationship_type, :string

    timestamps()
  end
end

Self-referencing associations allow a schema to reference itself, useful for hierarchical data or relationships between entities of the same type.

Polymorphic Association Pattern

defmodule MyApp.TodoItem do
  use Ecto.Schema

  schema "todo_items" do
    field :description, :string

    timestamps()
  end
end

defmodule MyApp.Comment do
  use Ecto.Schema

  schema "comments" do
    field :body, :string
    field :commentable_id, :integer
    field :commentable_type, :string

    timestamps()
  end
end

# Create a comment for a TodoItem
comment = %MyApp.Comment{
  body: "This needs to be done ASAP",
  commentable_id: todo_item.id,
  commentable_type: "todo_item"
}

Polymorphic associations can be implemented using a combination of ID and type fields. While Ecto doesn't have built-in polymorphic associations like some ORMs, this pattern provides similar functionality.

Schema with Virtual Fields

defmodule MyApp.User do
  use Ecto.Schema

  schema "users" do
    field :first_name, :string
    field :last_name, :string
    field :email, :string

    # Virtual fields
    field :full_name, :string, virtual: true
    field :password, :string, virtual: true
    field :password_hash, :string

    timestamps()
  end

  def build_full_name(%__MODULE__{} = user) do
    %{user | full_name: "#{user.first_name} #{user.last_name}"}
  end
end

Virtual fields are not persisted to the database but can be useful for temporary data like unhashed passwords or computed values like full names.

Schema with Custom Source

defmodule MyApp.LegacyUser do
  use Ecto.Schema

  @schema_prefix "legacy"
  schema "tbl_users" do
    field :user_name, :string, source: :username
    field :user_email, :string, source: :email
    field :create_date, :utc_datetime, source: :created_at

    timestamps()
  end
end

You can map schema fields to different column names using the :source option, useful when working with legacy databases or following different naming conventions.

Schema Prefix for Multi-Tenant Applications

defmodule MyApp.Organization do
  use Ecto.Schema

  @schema_prefix "public"
  schema "organizations" do
    field :name, :string
    field :slug, :string

    timestamps()
  end
end

defmodule MyApp.Tenant.User do
  use Ecto.Schema

  schema "users" do
    field :name, :string
    field :email, :string

    timestamps()
  end
end

# Query with dynamic prefix
MyApp.Repo.all(MyApp.Tenant.User, prefix: "tenant_#{tenant_id}")

The @schema_prefix attribute and the prefix option enable multi-tenant applications where each tenant has its own database schema or prefix.

When to Use This Skill

Use ecto-schema-patterns when you need to:

  • Define database-backed schemas for your application's domain models
  • Create embedded schemas for nested or complex data structures
  • Set up associations between different schemas (belongs_to, has_many, many_to_many)
  • Model hierarchical or self-referential data
  • Work with legacy databases using custom field mappings
  • Implement multi-tenant applications with schema prefixes
  • Define virtual fields for computed or temporary data
  • Use custom primary keys or composite keys
  • Store unstructured data using map or array fields
  • Create polymorphic association patterns

Best Practices

  • Use descriptive schema and field names that reflect your domain
  • Always define timestamps() for audit trails unless there's a specific reason not to
  • Prefer embedded schemas over JSON columns when the structure is known
  • Use Ecto.Enum for fields with a fixed set of values
  • Keep schemas focused on data structure, put business logic elsewhere
  • Use virtual fields for data that shouldn't be persisted
  • Define proper associations to leverage Ecto's preloading capabilities
  • Use on_replace::delete for many_to_many associations that should cascade
  • Extract complex embedded schemas into separate modules for reusability
  • Use belongs_to with define_field: false when you need custom foreign key options
  • Leverage schema metadata functions for metaprogramming sparingly
  • Use :source option to bridge schema and database naming differences
  • Set appropriate @primary_key for non-standard primary keys
  • Use join schemas instead of join tables when you need extra attributes
  • Document schema relationships and constraints in module documentation

Common Pitfalls

  • Forgetting to add timestamps() and losing audit information
  • Not setting on_replace option for associations, causing unexpected behavior
  • Confusing embeds_one/embeds_many with has_one/has_many
  • Using :map type when an embedded schema would provide better structure
  • Defining associations without corresponding database constraints
  • Not marking virtual fields with virtual: true, causing database errors
  • Mixing business logic with schema definitions
  • Creating circular dependencies between schema modules
  • Forgetting to set @primary_key false for join schemas
  • Not using proper foreign key options in belongs_to
  • Accessing associations without preloading them first
  • Over-nesting embedded schemas, making them hard to maintain
  • Not considering the difference between has_one and belongs_to
  • Using polymorphic patterns without proper indexing
  • Forgetting that embedded data is stored as JSON/map in a single column
  • Not handling nil values properly in custom source mappings
  • Creating too many associations, leading to N+1 query problems
  • Not using join schemas when additional join table attributes are needed
  • Hardcoding schema prefixes instead of making them configurable
  • Not validating embedded schemas separately from parent schemas

Resources

Official Ecto Documentation

Guides and Patterns

Community Resources

平台分布

OpenCode

29.21%
按下载量换算39

Codex

22.73%
按下载量换算31

Claude Code

17.41%
按下载量换算24

windsurf

13.77%
按下载量换算19

Antigravity

8.24%
按下载量换算11

Gemini CLI

3.55%
按下载量换算5

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

继续浏览同类 Skills