Prisma Seeding: Quickly Populate Your Database for Development
When developing applications, having realistic test data is crucial for debugging and testing. Instead of manually inserting data, you can automate this process with Prisma ORM and Faker.js. In this guide, we’ll walk through how to set up and run a database seeder in a Node.js Express application using Prisma and Faker.js.
Why Use Prisma for Seeding?
Prisma ORM makes interacting with databases effortless, and its seeding capabilities allow you to generate structured test data efficiently. Pairing it with Faker.js, a powerful data generation library, ensures your mock data looks natural and realistic.
Step 1: Install Faker.js
First, install Faker.js as a dev dependency:
npm install @faker-js/faker --save-dev
Faker.js provides an extensive API for generating fake names, descriptions, dates, and more. You can explore its features at fakerjs.dev.
Step 2: Create the Seeder File
Inside your Prisma directory, create a new file named seed.ts
. This file will contain the logic to populate your database.
import { PrismaClient } from "@prisma/client"; import { faker } from "@faker-js/faker"; const prisma = new PrismaClient(); const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1); const userIds = Array.from({ length: 3 }, () => faker.string.uuid()); async function main() { for (const userId of userIds) { const project = await prisma.project.create({ data: { user_id: userId, name: capitalize(faker.company.name()), }, }); for (let i = 0; i < 2; i++) { await prisma.task.create({ data: { user_id: userId, project_id: i % 2 === 0 ? project.id : null, name: capitalize(faker.word.words(2)), description: faker.lorem.sentence(), due_date: faker.date.future(), }, }); } } } main() .then(() => prisma.$disconnect()) .catch((error) => { console.error(error); prisma.$disconnect(); process.exit(1); });
This script:
- Generates three user IDs.
- Creates one project per user.
- Assigns two tasks per user, with some linked to projects and others standalone.
🚀Boost your career with 👉 IBM Full Stack Software Developer Certificate
Step 3: Configure TypeScript for Prisma
Since seed.ts
is outside your main src
folder, create a tsconfig.json
inside the Prisma directory:
{ "extends": "@tsconfig/node20/tsconfig.json" }
This ensures TypeScript compiles and runs the seed script correctly.
Step 4: Add the Seeder Script in package.json
To easily run the seed script, add the following entry to the scripts
section of package.json
:
"scripts": { "db:seed": "prisma db seed" }
Next, specify the seed file inside your Prisma configuration:
"prisma": { "seed": "ts-node prisma/seed.ts" }
Step 5: Run the Seeder
Execute the following command to populate your database:
npm run db:seed
You should see a confirmation message indicating the successful insertion of projects and tasks. To verify, open your database tool (e.g., TablePlus) and check the projects
and tasks
tables.
Wrapping Up
You’ve successfully automated database seeding using Prisma and Faker.js! With realistic test data, you can now focus on building and testing your application more efficiently.