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

prisma-factory-generator

v0.1.7

Published

Factory generator for Prisma Schema

Downloads

6

Readme

Automatically generate a factory from your Prisma Schema. This package contains a prisma generator so reference will automatically update everytime you will run prisma generate

There is a schema that looks like this

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

generator client {
  provider = "prisma-client-js"
}

generator factory {
  provider = "node ./dist/index.js"
}

model User {
  id       Int       @id @default(autoincrement())
  email    String    @unique
  userName String    @unique
  posts    Post[]
  comments Comment[]

  @@map(name: "users")
}

model Post {
  id        Int       @id @default(autoincrement())
  user      User      @relation(fields: [userId], references: [id])
  userId    Int       @unique @map(name: "user_id")
  title     String
  body      String
  createdAt DateTime  @default(now()) @map(name: "created_at")
  updatedAt DateTime  @default(now()) @map(name: "updated_at")
  comments  Comment[]

  @@map("posts")
}

model Comment {
  id        Int      @id @default(autoincrement())
  post      Post     @relation(fields: [postId], references: [id])
  postId    Int      @unique @map(name: "post_id")
  user      User     @relation(fields: [userId], references: [id])
  userId    Int      @unique @map(name: "user_id")
  body      String
  createdAt DateTime @default(now()) @map(name: "created_at")
  approved  Boolean  @default(false)

  @@map("comments")
}

After running npx prisma generate, the following functions will be available.

  • createUser
  • createPost
  • createComment

The scalar fields are set to random values by faker. Currently, the relation field needs to be set manually (we would like to create an option for it to be created automatically).


// Create a user (email and userName are will be created by faker)
const user = await createUser()
console.log(user) 
/**
 * {
 *   id: 1,
 *   email: 'National Directives Orchestrator',
 *   userName: 'Future Accountability Consultant'
 * }
 */

// Create a post (the title and body will be created by faker, and the user will be connected to the one created above).
const postConnectUser = await createPost({
  user: {
    connect: {
      id: user.id,
    },
  },
})
console.log(postConnectUser)
/**
 * {
 *  id: 1,
 *  userId: 1,
 *  title: 'Dynamic Research Architect',
 *  body: 'Corporate Tactics Associate',
 *  createdAt: 2021-12-02T09:10:06.640Z,
 *  updatedAt: 2021-12-02T09:10:06.640Z
 *  }
 */

// As an alternative, you can create users at the same time. In this case, you can use the function `inputsForUser` to set the parameters needed to create a user.
const postCreateUser = await createPost({
  user: {
    create: inputsForUser(),
  },
})
console.log(postCreateUser)
/**
 * {
 *  id: 6,
 *  userId: 6,
 *  title: 'Future Accountability Officer',
 *  body: 'Lead Usability Specialist',
 *  createdAt: 2021-12-02T13:28:21.541Z,
 *  updatedAt: 2021-12-02T13:28:21.541Z
 *  }
 */

// Create a comment (the body will be created by faker, the approved will be set to the DB default, and the user and post will be connected to the one created above).
const comment = await createComment({
  user: {
    connect: {
      id: user.id,
    },
  },
  post: {
    connect: {
      id: post.id,
    },
  },
})
console.log(comment)
/**
 * {
 *  id: 1,
 *  postId: 1,
 *  userId: 1,
 *  body: 'Human Security Consultant',
 *  createdAt: 2021-12-02T09:10:06.643Z,
 *  approved: false
 *  }
 */

Getting Started

  1. Install this package using:
npm install prisma-factory-generator

or

yarn add prisma-factory-generator
  1. Add the generator to the schema
generator factory {
  provider = "prisma-factory-generator"
}
  1. Run npx prisma generate to trigger the generator. This will create a factory folder in node_modules/.prisma/factory

Seeding data using a factory

This factory can be used when creating data with the seed function provided in Prisma.

  1. Prepare the seed script by referring to the Prisma documentation. Seeding your database / Prisma documentation
  2. Rewrite the main of the seed script to use the factory. Below is an example.
// prisma/seed.ts
import {
  createUser,
  createPost,
  createComment,
  inputsForUser,
} from '.prisma/factory'
async function main() {
  const user = await createUser()
  console.log(user)

  const newUser = await createUser({
    data: { userName: 'hello!' },
  })
  console.log(newUser)

  const postConnectUser = await createPost({
    data: {
      user: {
        connect: {
          id: user.id,
        },
      },
    },
  })
  console.log(postConnectUser)

  const postCreateUser = await createPost({
    data: {
      user: {
        create: inputsForUser(),
      },
    },
  })
  console.log(postCreateUser)

  const comment = await createComment({
    data: {
      user: {
        connect: {
          id: user.id,
        },
      },
      post: {
        connect: {
          id: postConnectUser.id,
        },
      },
    },
  })
  console.log(comment)
}

main().catch((e) => console.error(e.toString()))
  1. Run npx prisma db seed to create a seed using the factory

To Do

  • [ ] attributesForUser
  • [ ] createUserCollection

License

MIT

(This is not an official Prisma project. It is personally maintained by me )