Communitygithub.com

Drizzle ORM スキル

Drizzle のスキーマ定義、プッシュとマイグレーションの使い分け、型安全なクエリ、リレーション、トランザクション、プリペアドステートメントをカバーします。

Drizzle ORM スキル とは?

このスキルは、Drizzle ORM を使用したデータベース操作のベストプラクティスを提供します。開発時には `drizzle-kit push` で迅速なイテレーションを行い、本番環境では `drizzle-kit generate` と `drizzle-kit migrate` で追跡可能なマイグレーション履歴を管理します。`relations()` ヘルパーによるリレーション定義、`db.query` を使用した型安全な結合クエリ、プリペアドステートメントによるパフォーマンス最適化、`db.transaction` によるトランザクション処理など、Drizzle の主要機能を網羅しています。また、外部キーやフィルタリングカラムへのインデックス追加、Drizzle Studio によるビジュアルデータベースブラウジングについても説明しています。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/drizzle-orm

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Drizzle ORM

ルール

  • スキーマ定義:pgTable() / mysqlTable() / sqliteTable() を src/db/schema.ts で使用
  • 開発時: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 を使用してリレーションを1つのクエリで結合すること
  • 外部キーやフィルタリングカラムへのインデックス不足 — 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.

関連スキル