Db Seed

Db Seed

Generate database seed scripts with realistic sample data. Reads Drizzle schemas or SQL migrations, respects foreign key ordering, produces idempotent

Category: development Source: jezweb/claude-skills

What Is Db Seed? Db Seed is a Claude Code skill designed to generate database seed scripts with realistic, domain-appropriate sample data. It intelligently reads your database schema from Drizzle ORM TypeScript files, raw SQL migrations, or other schema formats, and produces ready-to-run seed files in either TypeScript or SQL. With built-in understanding of table relationships, data types, and constraints, Db Seed ensures that your development, demo, or testing environments are populated with consistent and plausible sample data, making it easier to develop, demo, and test your applications. ## Why Use Db Seed? Populating a database with meaningful data is essential for any modern software project, whether for local development, automated testing, or product demonstrations. Manually writing seed scripts is tedious and error-prone, especially when dealing with complex schemas, foreign key relationships, and unique constraints. Db Seed automates this entire process, saving time and reducing the risk of inconsistencies. Key motivations for using Db Seed include: - Accelerated Development: Developers can quickly spin up databases pre-populated with representative data, enabling faster iteration. - Reliable Testing: Automated tests run against realistic datasets, uncovering issues that might not surface with trivial test data. - Impressive Demos: Demo environments reflect realistic user scenarios, helping stakeholders visualize the product’s capabilities. - Consistent Environments: Idempotent seed scripts ensure that every environment starts with the same data, reducing “works on my machine” issues. ## How to Get Started Db Seed is designed for use with Claude Code and integrates seamlessly into projects that use Drizzle, D1, raw SQL, or Prisma for their schemas. Here’s a step-by-step guide to get started: 1. Ensure Your Schema Is Available: Place your schema files in standard locations. Db Seed will look for: - Drizzle schemas in src/db/schema.ts, src/schema/*.ts, or db/schema.ts - SQL migrations in drizzle/*.sql or migrations/*.sql - Raw SQL in schema.sql or db/*.sql - Prisma schema in prisma/schema.prisma 2. Invoke the Skill: Use trigger phrases such as “seed database,” “sample data,” or “generate fixtures” within your Claude Code environment. 3. Configure Parameters: Db Seed will prompt you for intent and scale, such as: - Purpose: (dev, demo, test) - Volume: Number of rows per table or total dataset size 4. Review and Run Generated Scripts: Db Seed produces idempotent TypeScript or SQL seed files. Review the output and run the scripts against your database. ### Example: Seeding a Simple User and Posts Schema Suppose you have the following Drizzle schema: typescript // src/db/schema.ts import { integer, text, pgTable } from "drizzle-orm/pg-core"; export const users = pgTable("users", { id: integer("id").primaryKey(), email: text("email").unique().notNull(), name: text("name"), }); export const posts = pgTable("posts", { id: integer("id").primaryKey(), userId: integer("user_id").references(() => users.id), title: text("title").notNull(), content: text("content"), }); Db Seed can generate a TypeScript seed file similar to: typescript await db.insert(users).values([ { id: 1, email: "alice@example.com", name: "Alice" }, { id: 2, email: "bob@example.com", name: "Bob" }, ]); await db.insert(posts).values([ { id: 1, userId: 1, title: "Hello World", content: "This is Alice's first post." }, { id: 2, userId: 2, title: "Greetings", content: "Bob says hello." }, ]); ## Key Features Db Seed offers several advanced capabilities tailored for modern development workflows: - Schema Awareness: Reads Drizzle TypeScript schemas, raw SQL files, and Prisma definitions to infer table structures, data types, relationships, and constraints. - Foreign Key Ordering: Automatically determines the correct order for inserting records, ensuring that parent records (e.g., users) are created before dependent records (e.g., posts). - Idempotent Scripts: Generates seed files that can be safely re-run without duplicating or corrupting data. - Constraint Handling: Respects UNIQUE, NOT NULL, DEFAULT, and foreign key constraints, avoiding common pitfalls in sample data generation. - Realistic Data: Fills tables with domain-appropriate fake data, rather than generic placeholders, enhancing the realism of dev and demo environments. - Batch and Limit Awareness: Handles D1 batch limits and other platform-specific constraints to avoid runtime errors during seeding. - Flexible Output: Produces TypeScript or SQL files, supporting a range of tech stacks and workflows. ## Best Practices To get the most out of Db Seed, consider the following best practices: - Keep Your Schema Up-to-Date: Ensure your schema files accurately reflect your production database. Outdated schemas can lead to invalid seed data. - Use for Dev, Demo, and Test Only: Seed data is intended for non-production use. Never seed real production databases with generated data. - Customize When Needed: While Db Seed generates realistic defaults, review and tweak the generated seed data for scenarios requiring specific edge cases. - Version Your Seed Scripts: Store generated scripts in version control to ensure consistent environments across teams. - Automate Seeding in CI: Integrate seed script execution into your CI/CD pipeline to set up test databases automatically. ## Important Notes