npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@andyhite/prisma-generator-fake-data

v0.10.2

Published

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

Downloads

4

Readme

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
  • 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
  ///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: '[email protected]',
  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 bootstraped using create-prisma-generator