Communitygithub.com

YepAPI/skills

Drizzle 結構定義、push 與 migrate 的差異、型別安全查詢、關聯、交易以及預備陳述式。

skills 是什麼?

這項技能旨在協助開發者有效率地使用 Drizzle ORM 進行資料庫操作,適用於 Drizzle 支援的所有資料庫(如 PostgreSQL、MySQL、SQLite)。透過清晰的規則與模式,技能涵蓋從結構定義(`pgTable`/`mysqlTable`/`sqliteTable`)、開發與生產環境的遷移策略(`drizzle-kit push` vs. `generate` + `migrate`)、型別安全的鏈式查詢、關聯查詢(`relations()` 搭配 `one()`/`many()`)、交易處理(`db.transaction`),到預備陳述式(`.prepare()`)等核心功能。開發者能藉此快速迭代結構、避免常見錯誤(如 N+1 查詢、遺漏索引),並善用 Drizzle Studio 進行視覺化資料庫瀏覽。

相容平台~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.

相關技能