← Back to archive

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, and age are routed to matching faker.js methods rather than generic ones, producing more realistic values.
  • Optional Prisma fields are returned as undefined by default. The emptyValueAs generator 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 extraImport and extraExport generator options.

How engineers can use it

The README documents a short setup flow inside an existing Prisma project.

  1. Install the package as a dev dependency using one of:

    • npm install -D prisma-generator-fake-data
    • yarn add -D prisma-generator-fake-data
    • pnpm install -D prisma-generator-fake-data
    • bun add -D prisma-generator-fake-data
  2. Add a generator block to schema.prisma:

    generator custom_generator {
        provider = "prisma-generator-fake-data"
        output   = "../types/fake-data.ts"
    }
    

    If output is omitted, the README states the file lands at ./prisma/fake-data.ts.

  3. Run npx prisma generate to emit the factory file.

  4. 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.