Communitygithub.com

YepAPI/skills

Drizzle 架构定义、push 与 migrate 对比、类型安全查询、关联、事务和预编译语句。

skills 是什么?

此技能提供了一套完整的 Drizzle ORM 使用指南,涵盖了从数据库架构定义到生产环境迁移的整个工作流。它强调了在开发阶段使用 `drizzle-kit push` 进行快速迭代,而在生产环境中则必须通过 `drizzle-kit generate` 和 `drizzle-kit migrate` 来生成可追踪的迁移历史。该技能深入介绍了如何利用 Drizzle 的类型安全查询 API(如 `db.select().from(users).where(...)`)和 TypeScript 的类型推断功能,确保代码的健壮性。此外,它还涵盖了如何使用 `relations()` 辅助函数定义关联关系,并进行高效的关联查询;使用 `db.transaction` 实现事务;以及通过 `prepare()` 方法创建预编译语句以优化热点路径的性能。该技能适用于使用 Node.js 和 Drizzle ORM 的开发者,帮助他们构建类型安全的数据库应用。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/drizzle-orm

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Drizzle ORM

规则

  • 架构定义:在 src/db/schema.ts 中使用 pgTable() / mysqlTable() / sqliteTable()
  • 开发阶段:使用 drizzle-kit push 进行快速迭代 — 无需生成迁移文件
  • 生产环境:使用 drizzle-kit generate + drizzle-kit migrate 获得可追踪的迁移历史
  • 类型安全查询:db.select().from(users).where(eq(users.email, email)) — 完整的 TypeScript 类型推断
  • 关联:使用 relations() 辅助函数定义 — one() 和 many() 用于通过 db.query 进行关联查询
  • 预编译语句:db.select().from(users).where(eq(users.id, sql.placeholder("id"))).prepare() — 在热点路径中复用
  • 事务:db.transaction(async (tx) => { ... }) — 在回调内部使用 tx 而不是 db
  • 索引:使用 .index() 内联定义或在表配置中定义 — 在 where 和 orderBy 使用的列上添加
  • Drizzle Studio:npx drizzle-kit studio — 开发期间可视化数据库浏览器

模式

// schema.ts
import { pgTable, text, timestamp, uuid, integer } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

export const users = pgTable("users", {
  id: uuid("id").primaryKey().defaultRandom(),
  name: text("name").notNull(),
  email: text("email").notNull().unique(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const posts = pgTable("posts", {
  id: uuid("id").primaryKey().defaultRandom(),
  title: text("title").notNull(),
  authorId: uuid("author_id").references(() => users.id).notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export const postsRelations = relations(posts, ({ one }) => ({
  author: one(users, { fields: [posts.authorId], references: [users.id] }),
}));

// 带关联的查询
const result = await db.query.users.findFirst({
  where: eq(users.id, userId),
  with: { posts: true },
});

避免

  • 在生产环境中使用 drizzle-kit push — 应使用 generate + migrate 进行受追踪的迁移
  • 当 Drizzle API 已涵盖时使用原始 SQL — 仅在复杂表达式中使用 sql 模板标签
  • 忘记定义 relations() — 没有它们,db.query 关联查询将无法工作
  • N+1 查询问题 — 在 db.query 中使用 with 在单个查询中连接关联
  • 在外键和筛选列上缺少索引 — Drizzle 不会自动创建它们

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Tenant isolation (row-level vs schema), subdomain routing, per-tenant config, and data partitioning.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能