Detalhes do pacote

prisma-generator-fake-data

luisrudge14kMIT0.15.0

The easiest way to generate mock data based on your Prisma models!

prisma, prisma2, generator, fake data

readme (leia-me)

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

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)

The easiest way to generate mock data based on your Prisma models!

What is prisma-generator-fake-data?

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.

Get started

  • 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

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

You're all done!

If you prefer, you can easily get started with this CodeSandbox.

Usage

Once the file is generated, you can import it in your project.

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
}
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'
}
*/

Generator options

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.
emptyValueAs no "undefined" "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".

Fields with special treatment:

  • name, fullName, firstName, lastName, and age will use specific faker-js methods to appear more realistic.
  • If you have an optional Prisma field (e.g., message String?), the fake data generator will always return undefined for that property (customizable through the emptyValueAs option).
  • If you have a JSON Prisma field, you can add your own data shape by adding a special comment above your field.
    • Example: ///FAKE:{test: faker.lorem.word()}
    • Keep in mind that the generator will simply relay whatever is after the FAKE: string to your generated code, so it needs to be valid TypeScript.
    • By using the extraImport generator option, you can create your own helper methods to generate fake data for your JSON fields

Inspired by https://github.com/toyamarinyon/prisma-factory-generator

This generator was bootstrapped using create-prisma-generator

changelog (log de mudanças)

0.14.3 (2024-11-01)

Bug Fixes

  • Integer data type fields should not be returning bigint's (8c15f32)

0.14.2 (2024-03-22)

Bug Fixes

  • Correctly generate import path on Windows (56ec5e0)

0.14.1 (2024-01-25)

Bug Fixes

  • Correctly generate import path (0ff1424)

0.14.0 (2024-01-25)

Features

Bug Fixes

  • fix BigInt generation, fix example folder (82e0d4e)

0.13.0 (2023-11-23)

Features

  • Use optional client output location when it exists (130bed7)

0.12.0 (2023-11-15)

Features

  • add support for enum arrays with default value (eab337c)

0.11.1 (2023-11-08)

Bug Fixes

  • Update createMethods.ts wrong function call (efa2928)

0.11.0 (2023-09-27)

Features

  • update to use faker 7.x -> 9.x (261e7a2)

Bug Fixes

0.10.0 (2023-07-21)

Features

0.9.0 (2023-07-14)

Features

  • add option to default to null instead of undefined fix #11 (1dbf9d2)

0.8.0 (2023-05-29)

Bug Fixes

  • render fake values for JSON fields with default values (55fefba)

0.7.0 (2023-04-06)

Features

  • render real enums; render optional fields as undefined; render default values when available (70b21fc)

0.6.0 (2023-04-05)

Features

  • remove overrides and add fake*Complete methods with foreign keys (e370ab4)

Bug Fixes

  • render enums with as const (1e1946d)

0.5.1 (2023-03-31)

Bug Fixes

  • render enums with as const (1e1946d)

0.5.0 (2023-03-30)

Features

0.4.1 (2023-03-28)

Bug Fixes

  • remove prettier as dependency (ed2ae92)

0.4.0 (2023-03-27)

Features

0.3.5 (2023-03-26)

Bug Fixes

  • return undefined for optional fields (0d92c64)

0.3.4 (2023-03-26)

Bug Fixes

0.3.3 (2023-03-26)

Bug Fixes

  • omit relation fields from overrides types (a0936d5)

0.3.2 (2023-03-26)

Bug Fixes

  • parse json field during creation (9dcbf15)

0.3.1 (2023-03-26)

0.3.0 (2023-03-26)

Features

  • handle non-required fields (4d60be7)

0.2.0 (2023-03-26)

New Features
  • custom data shapes for JSON fields (a2432738)
  • add name, fullName, firstName faker methods (5ff33fa4)

0.1.2 (2023-03-26)

Chores
Documentation Changes
New Features