Socket
Book a DemoInstallSign in
Socket

prisma-factory-generator

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prisma-factory-generator

Factory generator for Prisma Schema

0.1.7
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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

  • Install this package using:
npm install prisma-factory-generator

or

yarn add prisma-factory-generator
  • Add the generator to the schema
generator factory {
  provider = "prisma-factory-generator"
}
  • 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.

// 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()))
  • 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 )

FAQs

Package last updated on 25 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.