Repo of the Day
kritikio/prisma-generator-fake-data
Published: Jan 4, 2024
Open repository ↗Generate mock data based on your Prisma models
Summary
prisma-generator-fake-data is a Prisma Generator that produces TypeScript factory methods for each model in a Prisma schema, using faker.js to populate realistic values. It generates a single importable file at build time, so the fake data stays in sync with the schema whenever prisma generate runs. The project is released under the MIT license and is intended as a small build-time utility rather than a runtime service.
What it is useful for
This generator is helpful when you need mock data for unit tests, Storybook stories, local development, or demo seeding, and you do not want to hand-write factory functions for every model. Because it reads the schema directly, a Prisma model like User becomes a fakeUser() call that returns a populated object — for example, { email: '...', name: '...', address: '...' } matching the field types.
A few specific behaviors are worth knowing before adopting it:
- Fields named
name,fullName,firstName,lastName, andageare routed to matching faker.js methods rather than generic ones, producing more realistic values. - Optional Prisma fields are returned as
undefinedby default. TheemptyValueAsgenerator option can change this, for example to"null". - JSON fields are not guessed. You must describe the shape with a triple-slash comment above the field, such as
///FAKE:{notificationsEnabled: faker.datatype.boolean(), preferredColor: faker.color.rgb()}. - Reusable helpers can be pulled into the generated file through the
extraImportandextraExportgenerator options.
How engineers can use it
The README documents a short setup flow inside an existing Prisma project.
-
Install the package as a dev dependency using one of:
npm install -D prisma-generator-fake-datayarn add -D prisma-generator-fake-datapnpm install -D prisma-generator-fake-databun add -D prisma-generator-fake-data
-
Add a generator block to
schema.prisma:generator custom_generator { provider = "prisma-generator-fake-data" output = "../types/fake-data.ts" }If
outputis omitted, the README states the file lands at./prisma/fake-data.ts. -
Run
npx prisma generateto emit the factory file. -
Import the generated factories where needed:
import { fakeUser, fakeProfileComplete } from './prisma/fake-data'; console.log(fakeUser());
Documented limitations: optional fields require an explicit override to deviate from undefined, JSON fields need a ///FAKE: comment or a helper imported via extraImport, and the file must be regenerated after any schema change. The README links a CodeSandbox for trying it without a local setup, and notes the project was inspired by prisma-factory-generator.