We’ve all been there: you’re building a beautiful Express API, but your frontend looks like a ghost town because your database is empty. You spend 20 minutes manually typing test@test.com into TablePlus just to see if your CSS breaks.
Stop doing that.
By combining Faker.js with Prisma, we can create a "Reset Button" for our local environment that wipes the junk and seeds 100+ realistic records in seconds.
The Stack
- Database: PostgreSQL
- ORM: Prisma
- Mocking: @faker-js/faker
- Server: Express + TypeScript
1. Install the Secret Sauce
You don’t want Faker in your production bundle. Keep it in devDependencies.
npm install @faker-js/faker --save-dev
2. The "Nuke & Pave" Script
Create a file at prisma/seed.ts. This script does two things: it cleans the database (to prevent unique constraint errors) and populates it.
import { PrismaClient } from '@prisma/client';
import { faker } from '@faker-js/faker';
const prisma = new PrismaClient();
async function main() {
console.log("⚡ Starting database refresh...");
// --- THE REMOVER ---
// Note: Delete 'many' in order of dependency (Posts first, then Users)
await prisma.post.deleteMany();
await prisma.user.deleteMany();
console.log("🧹 Database wiped.");
// --- THE SEEDER ---
const userCount = 10;
for (let i = 0; i < userCount; i++) {
await prisma.user.create({
data: {
email: faker.internet.email(),
name: faker.person.fullName(),
avatar: faker.image.avatar(),
posts: {
create: [
{ title: faker.lorem.sentence(), content: faker.lorem.paragraphs(2) },
{ title: faker.lorem.sentence(), content: faker.lorem.paragraphs(2) },
],
},
},
});
}
console.log(`✅ Success! Seeded ${userCount} users with posts.`);
}
main()
.catch((e) => { console.error(e); process.exit(1); })
.finally(async () => { await prisma.$disconnect(); });
3. Wire it into Package.json
Prisma has built-in support for seeding. Add this to your package.json so you can trigger the refresh with one command.
{
"name": "my-express-api",
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"scripts": {
"db:seed": "prisma db seed"
}
}
4. Why This Wins
-
Scalability Testing: Want to see how your Express middleware handles 5,000 users? Just change
userCountto5000and run the script. - Zero Manual Entry: No more "asdfghjkl" names in your UI. You get real names, real emails, and real paragraphs.
-
Sanitizing Dev: Every time you run
npm run db:seed, you start with a predictable, clean state.
The Result in Express
Now, when you hit your GET /users endpoint, your UI will actually look like a real product:
app.get('/users', async (req, res) => {
const users = await prisma.user.findMany({ include: { posts: true } });
res.json(users); // Now returns a rich, realistic dataset!
});
Wrap Up
Setting up a seeder takes 5 minutes, but it saves hours of manual data entry over the life of a project. It’s the difference between guessing if your UI works and knowing it does.
Happy hacking! 🚀
Top comments (0)