The easiest way to generate mock data based on your Prisma models!
npm install prisma-generator-fake-data_Generated with Bing Image Creator. Prompt: create a logo with a cute panda smiling and holding a triangular prisma, digital art_
!npm
!GitHub Workflow Status (with branch)
It's a Prisma Generator that uses faker.js to create realistic-looking fake data for your Prisma models. With this generator, you can quickly and easily create fake data for your Prisma models, without having to write barely any code.
- Setup your Prisma project as usual (Get Started With Prisma)
- Install this package
- 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
- Modify your Prisma model file
``prisma`
generator custom_generator {
provider = "prisma-generator-fake-data"
output = "../types/fake-data.ts"
}
npx prisma generate
- Run
You're all done!
> If you prefer, you can easily get started with this CodeSandbox.
Once the file is generated, you can import it in your project.
`prisma
generator client {
provider = "prisma-client-js"
}
generator custom_generator {
provider = "prisma-generator-fake-data"
/// by default, the file will be generated at ./prisma/fake-data.ts
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
name String
/// Use comments to specify the faker method. For example:
///FAKE:faker.location.streetAddress({ useFullAddress: true })
address String
///FAKE:{notificationsEnabled: faker.datatype.boolean(), preferredColor: faker.color.rgb()}
settings Json
status UserStatus
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
someConfiguration Boolean
userId String @unique
user User @relation(fields: [userId], references: [id])
}
enum UserStatus {
active
inactive
}
`
`ts
import { fakeUser, fakeProfileComplete } from './prisma/fake-data'; //or your custom output path
console.log(fakeUser());
console.log(fakeProfileComplete());
/*
{
email: 'Ella.Mayer@gmail.com',
name: 'Lana Gulgowski',
age: 81,
settings: { notificationsEnabled: true, preferredColor: '#ee5344' },
maybeString: undefined,
status: 'active'
}
{
id: 96601,
someConfiguration: true,
userId: '821cf67a-dd86-49d7-b0e4-9ad451ad173d'
}
*/
`
| Option | Required | Default | Example | Description |
| ------------ | -------- | ------------------ | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| output | no | "./fake-data.ts" | "../types/fake-data.ts" | Path where the file will be exported to. Base folder is your prisma folder. |""
| extraImport | no | | "import {myCustomFunction} from '../utils/fakeImports'" | This import will be added to your generated file. Useful when you want to use a custom function to generate fake JSON data. You can use your TypeScript aliases. Base folder is your prisma folder. |""
| extraExport | no | | "export * from '../utils/fakeImports'" | This export will be added to your generated file. Useful when you want to export all of the fake methods created by this generator from the same file. Base folder is your prisma folder. |"undefined"
| emptyValueAs | no | | "null" | By default, optional fields will be generated with undefined. You can change this behavior by overriding this parameter. You can use a function imported in extraImport or just pass a hardcoded value like "null". |
| | | |
- name, fullName, firstName, lastName, and age will use specific faker-js methods to appear more realistic.message String?
- If you have an optional Prisma field (e.g., ), the fake data generator will always return undefined for that property (customizable through the emptyValueAs option).JSON
- If you have a Prisma field, you can add your own data shape by adding a special comment above your field.///FAKE:{test: faker.lorem.word()}
- Example: FAKE:
- Keep in mind that the generator will simply relay whatever is after the string to your generated code, so it needs to be valid TypeScript.extraImport` generator option, you can create your own helper methods to generate fake data for your JSON fields
- By using the
Inspired by https://github.com/toyamarinyon/prisma-factory-generator
This generator was bootstrapped using create-prisma-generator