Token导航 LogoToken导航TokenDH.com
开发规范敏感数据github未标认证来源可访问许可证需确认审计通过

drizzle-best-practicesDrizzle 最佳实践

Agent Skill

用于辅助数据库表结构、查询语句、迁移脚本和数据维护任务。它适合让 Agent 分析 schema、编写 SQL、排查查询问题、整理索引或生成迁移建议。使用时需要明确数据库类型、连接环境和目标表,区分只读分析与写入变更;涉及删除、更新、迁移和批量导入时,应优先 dry-run、备份或事务保护,避免误操作。

总安装

212

周安装

9

GitHub Stars

公开资料未说明

下载量

74
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/ofershap/drizzle-best-practices --skill drizzle-best-practices

简介

drizzle-best-practices 提供 Drizzle ORM 最佳实践指南,纠正常见错误如混淆 Prisma 语法与手动 SQL 滥用。

  • 强调 pgTable/mysqlTable 正确用法、关系声明规范与索引策略,提升 schema 可维护性与查询性能。
  • 适用于已有项目改造与新项目初始化,帮助团队统一编码风格与避免技术债务积累。
  • 使用前需确认项目已安装 Drizzle 相关依赖,避免在未配置环境下误用导致编译失败。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

When to use

Use this skill when working with Drizzle ORM code. Agents often confuse Drizzle with Prisma and produce wrong schema syntax, incorrect relation patterns, or manual SQL where the query builder should be used.

Critical Rules

1. Schema definition: use pgTable/mysqlTable/sqliteTable, not Prisma-style

Wrong (agents do this):

model User {
  id    Int    @id @default(autoincrement())
  name  String
  posts Post[]
}

Correct:

import { pgTable, serial, text } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
});

Why: Drizzle uses table-definition functions, not a Prisma-like DSL. Wrong syntax fails at compile time.

2. Relations: use relations(), not foreign key decorators

Wrong:

// Expecting Prisma-style @relation or Sequelize belongsTo

Correct:

import { relations } from 'drizzle-orm';

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

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

Why: Drizzle relations are defined separately from tables via relations(). Foreign keys go on the table; relation metadata goes in relations.

3. Queries: use query builder API, not raw SQL

Wrong:

const users = await db.execute(sql`SELECT * FROM users WHERE id = ${id}`);

Correct:

import { eq } from 'drizzle-orm';

const result = await db.select().from(users).where(eq(users.id, id));

Why: Query builder gives type safety, SQL injection protection, and dialect portability. Raw SQL only when the builder cannot express the query.

4. Type inference: use $inferSelect and $inferInsert

Wrong:

interface User {
  id: number;
  name: string;
}

Correct:

type SelectUser = typeof users.$inferSelect;
type InsertUser = typeof users.$inferInsert;

Why: Inferred types stay in sync with schema. Manual types drift and break on schema changes.

5. Prepared statements for frequent queries

Wrong:

for (const id of ids) {
  await db.select().from(users).where(eq(users.id, id));
}

Correct:

import { sql } from 'drizzle-orm';

const getById = db
  .select()
  .from(users)
  .where(eq(users.id, sql.placeholder('id')))
  .prepare();

for (const id of ids) {
  await getById.execute({ id });
}

Why: Prepared statements reduce parse/plan overhead and improve performance for repeated queries.

6. Transactions: use db.transaction()

Wrong:

await db.execute(sql`BEGIN`);
await db.insert(users).values(u);
await db.execute(sql`COMMIT`);

Correct:

await db.transaction(async (tx) => {
  await tx.insert(users).values(u);
  await tx.insert(posts).values(p);
});

Why: Drizzle transactions handle BEGIN/COMMIT/ROLLBACK and ensure the same connection is used throughout.

7. Migrations: drizzle-kit generate and migrate

Wrong:

// Hand-written 001_create_users.sql

Correct:

drizzle-kit generate
drizzle-kit migrate

Why: Generated migrations stay in sync with schema and support rollback. Manual SQL bypasses Drizzle's migration tracking.

8. Where clauses: use operators from drizzle-orm

Wrong:

db.select().from(users).where(sql`name = ${name}`);

Correct:

import { eq, like, gt, lt } from 'drizzle-orm';

db.select().from(users).where(eq(users.name, name));
db.select().from(users).where(like(users.name, '%foo%'));

Why: Operators are type-safe and parameterized. Raw template strings risk SQL injection.

9. Nested data: use relational queries

Wrong:

const users = await db.select().from(users);
for (const u of users) {
  u.posts = await db.select().from(posts).where(eq(posts.authorId, u.id));
}

Correct:

const usersWithPosts = await db.query.users.findMany({
  with: { posts: true },
});

Why: Relational API fetches nested data in one call with correct joins and typing.

10. Indexes: define in schema

Wrong:

// Separate migration: CREATE INDEX ...

Correct:

import { index } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  email: text('email').notNull(),
}, (t) => [
  index('users_email_idx').on(t.email),
]);

Why: Indexes in schema are versioned and migrated with the rest of the schema.

11. Validation: use drizzle-zod

Wrong:

// Manually maintaining Zod schema that mirrors DB

Correct:

import { createInsertSchema } from 'drizzle-zod';

const insertUserSchema = createInsertSchema(usersTable);

Why: drizzle-zod derives Zod schemas from Drizzle tables, keeping validation in sync.

12. Connection pooling: configure for serverless

Wrong:

const db = drizzle(process.env.DATABASE_URL);

Correct:

import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const client = postgres(url, { max: 10, idle_timeout: 20 });
const db = drizzle(client);

Why: Serverless needs bounded connections. Default pooling can exhaust DB connections.

Patterns

  • Export schema object for drizzle({schema}) when using relational queries
  • Use .returning() after insert/update to get affected rows
  • Prefer findFirst over findMany when expecting a single row
  • Use .$dynamic() for conditional where/orderBy/limit

Anti-Patterns

  • Do not mix Prisma and Drizzle syntax
  • Do not use raw SQL for simple CRUD
  • Do not define manual types that duplicate schema
  • Do not use push in production; use generate + migrate
  • Do not create new db/drizzle instances per request in serverless

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

38.31%
按下载量换算28

Claude

29.65%
按下载量换算22

Cursor

19.49%
按下载量换算14

Gemini CLI

9.25%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

敏感数据

该 Skill 可能接触密钥、Token、环境变量或敏感配置,应进入高风险复核队列,默认不自动发布。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills