How to Seed Prisma with Realistic Test Data
The Problem
Prisma makes database access elegant, but its seed file (prisma/seed.ts) is your responsibility. Most developers end up writing hundreds of lines of prisma.user.create() calls with hardcoded data that looks nothing like production. Worse, you have to manually chain creates to maintain foreign key relationships.
Faker.js helps with realistic values, but it does not understand your Prisma schema. You still wire up every relation by hand, and the seed file grows into an unmaintainable monster.
The Solution: MockHero API
MockHero generates relationally-consistent data from a simple JSON schema. Use ref fields to describe foreign keys and get back perfectly linked records. Feed the JSON directly into your Prisma client.
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": "User",
"count": 10,
"fields": [
{ "name": "id", "type": "cuid" },
{ "name": "name", "type": "full_name" },
{ "name": "email", "type": "email" },
{ "name": "role", "type": "enum", "params": { "values": ["USER","ADMIN"] } }
]
},
{
"name": "Post",
"count": 30,
"fields": [
{ "name": "id", "type": "cuid" },
{ "name": "authorId", "type": "ref", "params": { "ref": "User.id" } },
{ "name": "title", "type": "sentence" },
{ "name": "content", "type": "paragraphs" },
{ "name": "published", "type": "boolean" }
]
}
],
"format": "json"
}'
Step-by-Step Guide
1. Install dependencies
npm install -D tsx
npm install @prisma/client
2. Get your MockHero API key
Sign up at mockhero.dev/sign-up and copy your API key.
3. Write the seed script
Create prisma/seed.ts:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
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: "User",
count: 10,
fields: [
{ name: "id", type: "cuid" },
{ name: "name", type: "full_name" },
{ name: "email", type: "email" },
{ name: "role", type: "enum", params: { values: ["USER", "ADMIN"] } },
],
},
{
name: "Post",
count: 30,
fields: [
{ name: "id", type: "cuid" },
{ name: "authorId", type: "ref", params: { ref: "User.id" } },
{ name: "title", type: "sentence" },
{ name: "content", type: "paragraphs" },
{ name: "published", type: "boolean" },
],
},
],
format: "json",
}),
});
const { data } = await res.json();
for (const user of data.User) {
await prisma.user.create({ data: user });
}
for (const post of data.Post) {
await prisma.post.create({ data: post });
}
console.log("Seeded", data.User.length, "users and", data.Post.length, "posts");
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect());
4. Configure Prisma to use the seed
Add to your package.json:
"prisma": {
"seed": "tsx prisma/seed.ts"
}
5. Run the seed
npx prisma db seed
6. Verify
npx prisma studio
Open Prisma Studio and browse the User and Post tables. Every post's authorId points to a valid user.
Complete Example
The prisma/seed.ts above is the complete example. It fetches data from MockHero and inserts it through the Prisma client, preserving all relationships defined by ref fields.
Why MockHero vs Faker / Manual Seeds
- No seed file maintenance — your schema is a JSON payload, not 300 lines of TypeScript.
- Automatic relations —
reffields guarantee foreign keys resolve correctly. - Works with any Prisma provider — PostgreSQL, MySQL, SQLite, MongoDB — data is just JSON.
Get Started
Free tier, 1,000 rows/month, no credit card. Sign up at mockhero.dev and run npx prisma db seed with real data in minutes.
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