0.0.4 • Published 1 year ago

prisma-mill v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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'