db-schema
Skill193 starsChanged 4 months agoMITView on GitHub
创建或修改数据库 Schema。当需要创建新表、修改表结构、定义字段、设置索引约束、或涉及 Drizzle ORM / drizzle-zod 操作时使用
5 other projects use a version of this.
Adopted by
Most recently taken up by
- Markssuave/Markssuave-Agent-skillstheir own version, first seen Nov 8, 2026
- adarshaccreteinfo/Claude-Skillstheir own version, first seen Aug 27, 2026
- tstephx/book-mcp-servertheir own version, first seen Aug 19, 2026
- Jinbae10/skillstheir own version, first seen Jul 7, 2026
- Pascal-Neubauer/Claude_Skillstheir own version, first seen Apr 20, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 5
- adarshaccreteinfo/Claude-Skills★ 1same name
- Markssuave/Markssuave-Agent-skills★ 1same name
- Pascal-Neubauer/Claude_Skills★ 1same name
- tstephx/book-mcp-server★ 1same name
- Jinbae10/skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository29 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- bullmqSkill
- create-tierSkill
- crudSkill
- db-schemaSkill
- drizzle-v1Skill
- effect-v4Skill
- openspec-apply-changeSkill
- openspec-archive-changeSkill
- openspec-exploreSkill
- openspec-proposeSkill
- source-command-opsx-applySkill
- source-command-opsx-archiveSkill
- source-command-opsx-exploreSkill
- source-command-opsx-proposeSkill
- bullmqSkill
- create-tierSkill
- crudSkill
- drizzle-v1Skill
- effect-v4Skill
- openspec-apply-changeSkill
- openspec-archive-changeSkill
- openspec-exploreSkill
- openspec-proposeSkill
- openspec-apply-changeSkill
- openspec-archive-changeSkill
- openspec-exploreSkill
- openspec-proposeSkill
The file
Files
数据库 Schema 开发指南
技术栈
- ORM: Drizzle ORM (postgres.js driver)
- Schema 验证: drizzle-zod + Zod v4
- 数据库: PostgreSQL
文件结构
src/db/schema/
├── _shard/ # 共享定义
│ ├── base-columns.ts # 基础列(id/createdAt/updatedAt/createdBy/updatedBy)
│ ├── enums.ts # 数据库枚举定义
│ └── types/ # 共享类型
├── _infra/ # 基础设施表(非业务表)
│ └── queue/ # 队列任务
├── admin/ # 管理端表
│ ├── system/ # 系统管理
│ └── auth/ # 认证相关
├── client/ # 客户端表
└── index.ts # 统一导出注意:
_infra目录使用下划线前缀,存放基础设施相关的表(如审计日志、队列任务记录等),与业务表分开管理。
核心规则
导入方式
import db from "@/db"; // 数据库实例(default export)
import { users } from "@/db/schema"; // Schema 定义命名约定
- TS 属性名:camelCase(自动转换为 snake_case)
- 表名:
{tier}_{feature}s(如system_users) - 索引名:
{表名}_{字段名}_idx - 主键:统一命名为
id
批量插入
// 正确:批量插入
db.insert(table).values([...items])
// 错误:循环单条插入
for (const item of items) {
db.insert(table).values(item) // 不要这样做
}模板参考
数据库 Schema
参考 db-schema.md
包含:
- 标准表定义
- 字段类型参考
- 约束定义语法
- 主键设计规则
- 索引设计原则
- 枚举定义流程
- Relations 定义
- JSONB 使用规范
- Drizzle-kit 工作流程
Zod Schema
参考 zod-schema.md
包含:
- 标准 Schema 文件结构
- Schema 派生规则
- 最佳实践
- Zod v4 注意事项
PostgreSQL 表设计最佳实践
参考 postgresql-design.md
包含:
- 数据类型选择(避免使用的类型、推荐类型)
- 约束设计(PK、FK、UNIQUE、CHECK、EXCLUDE)
- 索引类型(B-tree、GIN、GiST、BRIN)
- 分区策略(RANGE、LIST、HASH)
- 特殊场景优化(更新密集、插入密集、Upsert)
- 安全 Schema 演进
- JSONB 使用指南
- 常用扩展(pgcrypto、pg_trgm、timescaledb、postgis、pgvector)
数据库迁移指南
参考 migration.md
包含:
- Drizzle 工作流程(push vs generate vs migrate)
- 迁移最佳实践(小单位迁移、安全添加/删除列)
- 生产检查清单(迁移前/中/后)
- 回滚策略
- 常见问题解答
开发流程
创建新表
- 在
src/db/schema/{tier}/{category}/下创建文件 - 定义表结构(继承 baseColumns)
- 创建 selectSchema 和 insertSchema
- 在
src/db/schema/index.ts中导出 - 运行
pnpm push(开发环境)
修改现有表
- 修改 schema 文件中的表定义
- 运行
pnpm push(开发环境) - 如果需要迁移数据,编写迁移脚本
生产部署
pnpm generate # 生成迁移文件
pnpm migrate # 执行迁移重要提醒
- 不要修改已执行的迁移文件和
meta/文件夹;未执行的迁移文件可以修改或删除后重新生成 - 使用枚举时确保 TS 枚举和 DB 枚举保持同步
- 外键选择:物理外键(需级联)vs 逻辑外键(应用层维护)
- 索引设计:不确定时先询问,避免过度设计
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

