db-seed-generator
npx machina-cli add skill Nembie/claude-code-skills/db-seed-generator --openclawDB Seed Generator
Before generating any output, read config/defaults.md and adapt all patterns, imports, and code examples to the user's configured stack.
Process
- Read
schema.prismaand extract all models, fields, types, constraints, relations, and enums. - Determine dependency order: models with no required relations first, then models that depend on them.
- Generate a
prisma/seed.tsfile with realistic fake data that respects all constraints. - Output a ready-to-run seed file.
Dependency Ordering
Build a directed graph of model dependencies from @relation fields. Topologically sort so parent records are created before children.
User (no deps) → create first
Organization (no deps) → create first
Post (depends on User) → create second
Comment (depends on User, Post) → create third
If circular dependencies exist, use a two-pass approach: create records with required fields first, then update with optional relation fields.
Data Generation Rules
Field Name Conventions
Generate realistic values based on field names:
| Pattern | Generated value |
|---|---|
firstName | "Elena", "Marcus", "Priya" |
lastName | "Rodriguez", "Chen", "Okafor" |
name (on User) | "Elena Rodriguez" |
email | "elena.rodriguez@example.com" |
phone | "555-0101", "555-0102" |
avatarUrl, imageUrl | "https://example.com/avatars/1.jpg" |
url, website | "https://example.com" |
title (on Post/Article) | Realistic short sentences |
content, body, description | 1-3 realistic sentences |
slug | Derived from title: "realistic-short-sentence" |
price, amount | Realistic numbers like 29.99, 149.00 |
quantity, count | Small integers: 1-100 |
createdAt, updatedAt | Dates spread over the last 90 days |
publishedAt | Either null or a date after createdAt |
isActive, isPublished | Mix of true/false |
role (enum) | Distribute across valid enum values |
password | Pre-hashed placeholder: "$2b$10$..." — never plaintext |
Constraints
@unique: Ensure every value is unique. Use index suffixes:"user1@example.com","user2@example.com".@id @default(cuid()): Omit from create calls — let the database generate them.@default(now()): Omit or set explicit dates for predictable ordering.@default(value): Omit to use the default, or set explicitly for variety.- Enums: Only use values defined in the enum.
Int,Float: Respect@db.SmallInt,@db.Decimal(10,2)etc.
Relations
- One-to-many: Create the "one" side first, reference its ID on the "many" side.
- Many-to-many (implicit): Use
connectsyntax after both sides exist. - Self-referencing: Create root records first (no parent), then children pointing to them.
- Required relations: Always satisfy — never leave a required FK null.
- Optional relations: Mix of connected and null.
Output Template
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
// Clear existing data (reverse dependency order)
await prisma.comment.deleteMany();
await prisma.post.deleteMany();
await prisma.user.deleteMany();
// Seed in dependency order
const users = await Promise.all([
prisma.user.upsert({
where: { email: "elena.rodriguez@example.com" },
update: {},
create: {
email: "elena.rodriguez@example.com",
name: "Elena Rodriguez",
// ... fields
},
}),
// ... more users
]);
// Use createMany for bulk inserts where relations allow
await prisma.post.createMany({
data: [
{ title: "...", authorId: users[0].id },
// ...
],
});
console.log("Seeded: X users, Y posts, Z comments");
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(() => prisma.$disconnect());
Scale Parameter
When the user specifies a count (e.g., "seed 50 users"), generate that many records. Default to 5-10 records per model. Use createMany for bulk inserts where possible. Wrap large seeds in a prisma.$transaction().
Upsert Pattern
Use upsert on a unique field to make the seed idempotent — safe to run multiple times.
Reference
See references/seed-patterns.md for dependency ordering details, realistic data patterns, and performance tips.
Source
git clone https://github.com/Nembie/claude-code-skills/blob/main/skills/db-seed-generator/SKILL.mdView on GitHub Overview
DB Seed Generator creates a realistic seed.ts from your Prisma schema. It reads schema.prisma, builds a dependency order, and outputs a ready-to-run seed script that respects all constraints, including relations and enums. It adapts imports and code samples to your configured stack by consulting config/defaults.md.
How This Skill Works
The tool parses schema.prisma to enumerate models, fields, relations, and enums, then builds a dependency graph using @relation fields. It topologically sorts models to create parent records before children, and emits a PrismaClient-based seed.ts that uses create, connect, and createMany with realistic data while honoring constraints and optional defaults. If circular dependencies exist, it applies a two-pass approach to first create required fields and then update optional relation fields.
When to Use It
- When asked to generate seed data for dev, test, or fixtures
- When you need to populate a new environment after migrations
- When you require realistic, constraint-respecting sample data across related models
- When creating CI-friendly fixtures with deterministic data
- When you want to seed or reload test databases during development
Quick Start
- Step 1: Ensure schema.prisma is in your project and inspect model relations
- Step 2: Run the generator to produce prisma/seed.ts configured for your stack (per config/defaults.md)
- Step 3: Run your seed script (e.g., npx prisma db seed) to populate the database
Best Practices
- Let the database generate IDs for @id @default(cuid()) fields by omitting them in seed calls
- Respect @unique constraints by producing unique values (e.g., emails, slugs)
- Handle circular dependencies with a two-pass create-then-update strategy
- Use createMany where possible for bulk inserts, when relations allow
- Hash passwords using the pre-hashed placeholder format (e.g., "$2b$10$...") and avoid plaintext; validate enums exhaustively
Example Use Cases
- Seed a social app: User, Organization, Post, Comment with realistic names and emails
- Seed e-commerce: User, Product, Category, Order, OrderItem with non-null foreign keys
- Seed a blog: Author, Blog, Post, Tag, PostTag with SEO-friendly titles and slugs
- Seed project management: User, Project, Task, Comment with nested relations
- Seed a media library: User, Album, Photo with relation-based access control