db-seed by jezweb/claude-skills
npx skills add https://github.com/jezweb/claude-skills --skill db-seed生成种子脚本,用真实且符合领域场景的示例数据填充数据库。读取您的数据库模式并生成可直接运行的种子文件。
扫描项目以查找模式定义:
| 来源 | 位置模式 |
|---|---|
| Drizzle 模式 | src/db/schema.ts, src/schema/*.ts, db/schema.ts |
| D1 迁移 | drizzle/*.sql, migrations/*.sql |
| 原始 SQL | schema.sql, |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
db/*.sql| Prisma | prisma/schema.prisma |
读取所有模式文件。构建以下内容的思维模型:
询问用户:
| 参数 | 选项 | 默认值 |
|---|---|---|
| 用途 | dev, demo, testing | dev |
| 数据量 | small (5-10 行/表), medium (20-50), large (100+) | small |
| 领域上下文 | "电子商务商店", "SaaS 应用", "博客" 等 | 从模式推断 |
| 输出格式 | TypeScript (Drizzle), 原始 SQL, 或两者 | 匹配项目的 ORM |
用途影响数据质量:
根据外键构建依赖关系图。先插入父表,再插入子表。
博客模式示例顺序:
1. users (无依赖)
2. categories (无依赖)
3. posts (依赖于 users, categories)
4. comments (依赖于 users, posts)
5. tags (无依赖)
6. post_tags (依赖于 posts, tags)
循环依赖:如果表 A 引用 B 且 B 引用 A,则使用可为空的外键并分两次插入(先插入 NULL,然后 UPDATE)。
请勿使用通用占位符,如 "test123"、"foo@bar.com" 或 "Lorem ipsum"。生成与领域匹配的数据。
姓名:使用硬编码的常见姓名列表。混合性别和文化背景。
const firstNames = ['Sarah', 'James', 'Priya', 'Mohammed', 'Emma', 'Wei', 'Carlos', 'Aisha'];
const lastNames = ['Chen', 'Smith', 'Patel', 'Garcia', 'Kim', 'O\'Brien', 'Nguyen', 'Wilson'];
电子邮件:从姓名派生 —— sarah.chen@example.com。使用 example.com 域(RFC 2606 保留域)。
日期:在真实的时间范围内生成。对于 D1/SQLite 使用 ISO 8601 格式。
const randomDate = (daysBack: number) => {
const d = new Date();
d.setDate(d.getDate() - Math.floor(Math.random() * daysBack));
return d.toISOString();
};
ID:对于 UUID 使用 crypto.randomUUID(),如果模式使用自增,则使用顺序整数。
确定性种子:对于可重现的数据,使用带种子的伪随机数生成器:
function seededRandom(seed: number) {
return () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
}
const rand = seededRandom(42); // 相同的种子 = 每次生成相同的数据
价格/金额:使用真实的范围。(rand() * 900 + 100).toFixed(2) 表示 $1-$10 范围。
描述/内容:为每种内容类型编写 3-5 个真实变体并循环使用。不要生成听起来像 AI 的文本 —— 要像真实的用户数据一样编写。
// scripts/seed.ts
import { drizzle } from 'drizzle-orm/d1';
import * as schema from '../src/db/schema';
export async function seed(db: ReturnType<typeof drizzle>) {
console.log('Seeding database...');
// 清除现有数据(按依赖顺序反向)
await db.delete(schema.comments);
await db.delete(schema.posts);
await db.delete(schema.users);
// 插入用户
const users = [
{ id: crypto.randomUUID(), name: 'Sarah Chen', email: 'sarah@example.com', ... },
// ...
];
// D1 批量限制:每次 INSERT 最多 10 行
for (let i = 0; i < users.length; i += 10) {
await db.insert(schema.users).values(users.slice(i, i + 10));
}
// 插入文章(引用用户)
const posts = [
{ id: crypto.randomUUID(), userId: users[0].id, title: '...', ... },
// ...
];
for (let i = 0; i < posts.length; i += 10) {
await db.insert(schema.posts).values(posts.slice(i, i + 10));
}
console.log(`Seeded: ${users.length} users, ${posts.length} posts`);
}
运行命令:npx tsx scripts/seed.ts
对于 Cloudflare Workers,添加一个种子端点(生产前移除):
app.post('/api/seed', async (c) => {
const db = drizzle(c.env.DB);
await seed(db);
return c.json({ ok: true });
});
-- seed.sql
-- 运行: npx wrangler d1 execute DB_NAME --local --file=./scripts/seed.sql
-- 清除现有数据(反向顺序)
DELETE FROM comments;
DELETE FROM posts;
DELETE FROM users;
-- 用户
INSERT INTO users (id, name, email, created_at) VALUES
('uuid-1', 'Sarah Chen', 'sarah@example.com', '2025-01-15T10:30:00Z'),
('uuid-2', 'James Wilson', 'james@example.com', '2025-02-01T14:22:00Z');
-- 文章(D1 每次 INSERT 最多 10 行)
INSERT INTO posts (id, user_id, title, body, created_at) VALUES
('post-1', 'uuid-1', 'Getting Started', 'Welcome to...', '2025-03-01T09:00:00Z');
种子脚本必须可以安全地重新运行:
// 选项 A: 先删除再插入(简单,但会丢失数据)
await db.delete(schema.users);
await db.insert(schema.users).values(seedUsers);
// 选项 B: Upsert(保留非种子数据)
for (const user of seedUsers) {
await db.insert(schema.users)
.values(user)
.onConflictDoUpdate({ target: schema.users.id, set: user });
}
对于 dev/testing 默认使用选项 A,对于 demo(用户可能已添加自己的数据)使用选项 B。
| 注意事项 | 解决方案 |
|---|---|
| 每次 INSERT 最多约 10 行 | 分块插入,每块 10 行 |
| 无原生 BOOLEAN 类型 | 使用 INTEGER (0/1) |
| 无原生 DATETIME 类型 | 使用 TEXT 并存储 ISO 8601 字符串 |
| JSON 存储为 TEXT | 插入前使用 JSON.stringify() |
| 外键始终强制执行 | 先插入父表 |
| 100 个绑定参数限制 | 保持 批量大小 × 列数 < 100 |
每周安装量
102
代码仓库
GitHub 星标数
643
首次出现
10 天前
安全审计
安装于
opencode99
kimi-cli98
gemini-cli98
amp98
cline98
github-copilot98
Generate seed scripts that populate databases with realistic, domain-appropriate sample data. Reads your schema and produces ready-to-run seed files.
Scan the project for schema definitions:
| Source | Location pattern |
|---|---|
| Drizzle schema | src/db/schema.ts, src/schema/*.ts, db/schema.ts |
| D1 migrations | drizzle/*.sql, migrations/*.sql |
| Raw SQL | schema.sql, db/*.sql |
| Prisma | prisma/schema.prisma |
Read all schema files. Build a mental model of:
Ask the user:
| Parameter | Options | Default |
|---|---|---|
| Purpose | dev, demo, testing | dev |
| Volume | small (5-10 rows/table), medium (20-50), large (100+) | small |
| Domain context | "e-commerce store", "SaaS app", "blog", etc. | Infer from schema |
| Output format | TypeScript (Drizzle), raw SQL, or both | Match project's ORM |
Purpose affects data quality :
Build a dependency graph from foreign keys. Insert parent tables before children.
Example order for a blog schema:
1. users (no dependencies)
2. categories (no dependencies)
3. posts (depends on users, categories)
4. comments (depends on users, posts)
5. tags (no dependencies)
6. post_tags (depends on posts, tags)
Circular dependencies : If table A references B and B references A, use nullable foreign keys and insert in two passes (insert with NULL, then UPDATE).
Do NOT use generic placeholders like "test123", "foo@bar.com", or "Lorem ipsum". Generate data that matches the domain.
Names : Use a hardcoded list of common names. Mix genders and cultural backgrounds.
const firstNames = ['Sarah', 'James', 'Priya', 'Mohammed', 'Emma', 'Wei', 'Carlos', 'Aisha'];
const lastNames = ['Chen', 'Smith', 'Patel', 'Garcia', 'Kim', 'O\'Brien', 'Nguyen', 'Wilson'];
Emails : Derive from names — sarah.chen@example.com. Use example.com domain (RFC 2606 reserved).
Dates : Generate within a realistic range. Use ISO 8601 format for D1/SQLite.
const randomDate = (daysBack: number) => {
const d = new Date();
d.setDate(d.getDate() - Math.floor(Math.random() * daysBack));
return d.toISOString();
};
IDs : Use crypto.randomUUID() for UUIDs, or sequential integers if the schema uses auto-increment.
Deterministic seeding : For reproducible data, use a seeded PRNG:
function seededRandom(seed: number) {
return () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
}
const rand = seededRandom(42); // Same seed = same data every time
Prices/amounts : Use realistic ranges. (rand() * 900 + 100).toFixed(2) for $1-$10 range.
Descriptions/content : Write 3-5 realistic variations per content type and cycle through them. Don't generate AI-sounding prose — write like real user data.
// scripts/seed.ts
import { drizzle } from 'drizzle-orm/d1';
import * as schema from '../src/db/schema';
export async function seed(db: ReturnType<typeof drizzle>) {
console.log('Seeding database...');
// Clear existing data (reverse dependency order)
await db.delete(schema.comments);
await db.delete(schema.posts);
await db.delete(schema.users);
// Insert users
const users = [
{ id: crypto.randomUUID(), name: 'Sarah Chen', email: 'sarah@example.com', ... },
// ...
];
// D1 batch limit: 10 rows per INSERT
for (let i = 0; i < users.length; i += 10) {
await db.insert(schema.users).values(users.slice(i, i + 10));
}
// Insert posts (references users)
const posts = [
{ id: crypto.randomUUID(), userId: users[0].id, title: '...', ... },
// ...
];
for (let i = 0; i < posts.length; i += 10) {
await db.insert(schema.posts).values(posts.slice(i, i + 10));
}
console.log(`Seeded: ${users.length} users, ${posts.length} posts`);
}
Run with: npx tsx scripts/seed.ts
For Cloudflare Workers, add a seed endpoint (remove before production):
app.post('/api/seed', async (c) => {
const db = drizzle(c.env.DB);
await seed(db);
return c.json({ ok: true });
});
-- seed.sql
-- Run: npx wrangler d1 execute DB_NAME --local --file=./scripts/seed.sql
-- Clear existing (reverse order)
DELETE FROM comments;
DELETE FROM posts;
DELETE FROM users;
-- Users
INSERT INTO users (id, name, email, created_at) VALUES
('uuid-1', 'Sarah Chen', 'sarah@example.com', '2025-01-15T10:30:00Z'),
('uuid-2', 'James Wilson', 'james@example.com', '2025-02-01T14:22:00Z');
-- Posts (max 10 rows per INSERT for D1)
INSERT INTO posts (id, user_id, title, body, created_at) VALUES
('post-1', 'uuid-1', 'Getting Started', 'Welcome to...', '2025-03-01T09:00:00Z');
Seed scripts must be safe to re-run:
// Option A: Delete-then-insert (simple, loses data)
await db.delete(schema.users);
await db.insert(schema.users).values(seedUsers);
// Option B: Upsert (preserves non-seed data)
for (const user of seedUsers) {
await db.insert(schema.users)
.values(user)
.onConflictDoUpdate({ target: schema.users.id, set: user });
}
Default to Option A for dev/testing, Option B for demo (where users may have added their own data).
| Gotcha | Solution |
|---|---|
| Max ~10 rows per INSERT | Batch inserts in chunks of 10 |
| No native BOOLEAN | Use INTEGER (0/1) |
| No native DATETIME | Use TEXT with ISO 8601 strings |
| JSON stored as TEXT | JSON.stringify() before insert |
| Foreign keys always enforced | Insert parent tables first |
| 100 bound parameter limit | Keep batch size × columns < 100 |
Weekly Installs
102
Repository
GitHub Stars
643
First Seen
10 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode99
kimi-cli98
gemini-cli98
amp98
cline98
github-copilot98
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
163,300 周安装
Sentry OTel 导出器设置教程:配置OpenTelemetry Collector发送追踪日志
340 周安装
Cloudflare Durable Objects 教程:使用 SQLite 和 WebSocket 构建有状态应用
332 周安装
Codex技能创建器指南:如何构建高效AI代理技能模块与最佳实践
345 周安装
小红书内容转换器:一键将通用文章转为小红书爆款笔记格式 | AI写作助手
338 周安装
Git 智能体合并工具:自动合并优胜分支、归档失败分支、清理工作树
374 周安装
股票行情实时查询工具 - 获取股价、成交量、市值、移动平均线等数据
345 周安装