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-mill

v0.0.4

Published

A Prisma generator to create factories for your models

Downloads

73

Readme

Prisma Mill - Factory Code Generation

This is a Prisma generator which creates factories based on your schema to aid in test

Mill is a fixtures tool for Prisma which does the following:

  • Generates helpers, based off of your schema, to aid in the creation of your factories.
  • Multiple strategies:
    • Build: Unsaved instances
    • Create: Saved instances

Table of Contents

Getting Started

Install

Run:

npm add -D prisma-mill
# or
pnpm add -D prisma-mill
# or
yarn add -D prisma-mill

Setup

Add the following generator block to your schema.prisma file and set the output to the same directory where your Prisma client files are generate. That's node_modules/.prisma/factories, by default. This will happen automatically in the future.

generator factories {
  provider = "prisma-mill"
  output   = "./node_modules/.prisma/factories"
}

Usage

Assuming the following models:

model User {
  id        Int       @id @default(autoincrement())
  name      String
  posts     Post[]
  createdAt DateTime  @default(now()) @db.Timestamp(6)
  updatedAt DateTime? @updatedAt
}

model Post {
  id       Int       @id @default(autoincrement())
  title    String
  content  String
  author   User      @relation(fields: [authorId], references: [id])
  authorId Int
  createdAt DateTime  @default(now()) @db.Timestamp(6)
  updatedAt DateTime? @updatedAt
}

The generator will create a function, createUserFactory, and you can use this in the following fashion:

Create a Factory

import { faker } from '@faker-js/faker';
import { createUserFactory } from '.prisma/factories';

const UserFactory = createUserFactory({
  name: faker.name.fullName(),
});

Build from a Factory

This builds a factory but does not save it to the database. As such, it won't include database-generated fields like id, createdAt, or updatedAt, for example.

const basic = UserFactory.build();
console.log(basic.name); // The name generated by Faker

const overridden = UserFactory.build({ name: 'Tom' });
console.log(overridden.name); // => 'Tom'

Create from a Factory

This builds a factory and saves it to the database. Unlike .build(), the result will include all expected fields from the database.

const basic = UserFactory.create();

console.log(basic.id); // The id generated by the DB
console.log(basic.name); // The name generated by Faker

const overridden = UserFactory.build({ name: 'Tom' });
console.log(overridden.name); // => 'Tom'

Managing Relationships

import { faker } from '@faker-js/faker';
import { createUserFactory } from '.prisma/factories';

const UserFactory = createUserFactory({
  name: faker.name.fullName(),
});

const PostFactory = createUserFactory({
  title: faker.lorem.sentence(),
  content: faker.lorem.paragraph(),
});

// Via `create`
const post1 = PostFactory.create({
  title: 'Foo',
  content: 'Bar',
  author: {
    create: UserFactory.build({ name: 'Tom' });
  }
})

console.log(post1.id); // => The id generated by the DB
console.log(post1.title); // => 'Foo'
console.log(post1.content); // => 'Bar'
console.log(post1.author.id); // => The id generated by the DB
console.log(post1.author.name); // => 'Tom'

// Via `connect`
const user = UserFactory.create({ name: 'Tom' });
const post2 = PostFactory.create({
  author: {
    create: { id: user.id }
  }
})

console.log(post2.id); // => The id generated by the DB
console.log(post2.title); // => The title generated by Faker
console.log(post2.content); // => The content generated by Faker
console.log(post2.author.id); // => The id generated by the DB
console.log(post2.author.name); // => 'Tom'