Database

How to Seed Drizzle ORM with Realistic Test Data

The Problem

Drizzle ORM is lightweight and type-safe, but it doesn't come with a built-in seed command. You are left writing raw db.insert() calls with fabricated data that looks nothing like what your app actually handles. Maintaining foreign key consistency between tables is entirely on you.

Most teams end up with a fragile seed script that breaks every time they add a column. The data itself is often a handful of "Test User 1, Test User 2" records that are useless for UI development or demos.

The Solution: MockHero API

MockHero returns production-quality test data from a single API call. Define your Drizzle tables as a MockHero schema, use ref fields for foreign keys, and get JSON back that maps directly to your Drizzle insert calls.

Quick Setup

curl -X POST https://api.mockhero.dev/api/v1/generate \
  -H "x-api-key: mh_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
  "tables": [
    {
      "name": "teams",
      "count": 5,
      "fields": [
        { "name": "id", "type": "uuid" },
        { "name": "name", "type": "company_name" },
        { "name": "plan", "type": "enum", "params": { "values": ["free","pro","enterprise"] } }
      ]
    },
    {
      "name": "members",
      "count": 20,
      "fields": [
        { "name": "id", "type": "uuid" },
        { "name": "teamId", "type": "ref", "params": { "ref": "teams.id" } },
        { "name": "name", "type": "full_name" },
        { "name": "email", "type": "email" },
        { "name": "role", "type": "enum", "params": { "values": ["owner","admin","member"] } }
      ]
    }
  ],
  "format": "json"
}'

Step-by-Step Guide

1. Install dependencies

npm install drizzle-orm postgres
npm install -D drizzle-kit tsx

2. Get your MockHero API key

Sign up at mockhero.dev/sign-up. The free tier includes 1,000 rows/month.

3. Write the seed script

Create src/db/seed.ts:

import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { teams, members } from "./schema";

const client = postgres(process.env.DATABASE_URL!);
const db = drizzle(client);

async function seed() {
  const res = await fetch("https://api.mockhero.dev/api/v1/generate", {
    method: "POST",
    headers: {
      "x-api-key": process.env.MOCKHERO_API_KEY!,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      tables: [
        {
          name: "teams",
          count: 5,
          fields: [
            { name: "id", type: "uuid" },
            { name: "name", type: "company_name" },
            { name: "plan", type: "enum", params: { values: ["free","pro","enterprise"] } },
          ],
        },
        {
          name: "members",
          count: 20,
          fields: [
            { name: "id", type: "uuid" },
            { name: "teamId", type: "ref", params: { ref: "teams.id" } },
            { name: "name", type: "full_name" },
            { name: "email", type: "email" },
            { name: "role", type: "enum", params: { values: ["owner","admin","member"] } },
          ],
        },
      ],
      format: "json",
    }),
  });

  const { data } = await res.json();

  await db.insert(teams).values(data.teams);
  await db.insert(members).values(data.members);

  console.log("Seeded", data.teams.length, "teams and", data.members.length, "members");
  await client.end();
}

seed();

4. Run the seed

npx tsx src/db/seed.ts

5. Verify with Drizzle Studio

npx drizzle-kit studio

Open Studio and check that every member's teamId references a real team.

Complete Example

The seed script above is the full working example. It fetches data from MockHero's API, then uses Drizzle's db.insert() to write teams and members with correct foreign key relationships.

Why MockHero vs Faker / Manual Seeds

  • Schema as config — change the JSON schema, not TypeScript code, when tables evolve.
  • Batch-friendly — MockHero returns arrays ready for Drizzle's bulk .values() call.
  • Deterministic output — pass a seed param for identical data across runs.

Get Started

Free tier, no credit card. Sign up at mockhero.dev and seed your Drizzle project in under five minutes.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles