New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@quramy/prisma-fabbrica

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@quramy/prisma-fabbrica

Factory helper for Prisma.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
decreased by-23.27%
Maintainers
1
Weekly downloads
 
Created
Source

prisma-fabbrica

Factory helper for Prisma.

Getting started

npm i @quramy/prisma-fabbrica --dev

Then, edit your prisma/schema.prisma and append the prisma-fabbrica generator configuration:

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

generator fabbrica {
  provider = "prisma-fabbrica"
}

And run generate command.

npx prisma generate

The above command generates JavaScript and TypeScript type definition files under src/__generated__/fabbrica directory. You can define your model factory importing them.

For example, if schema.prisma has the following User model, you can import defineUserFactory and define UserFactory using this function.

model User {
  id    String @id
  name  String
  posts Post[]
}
/* src/seed.ts */

import { PrismaClient } from "@prisma/client";

import { initialize, defineUserFactory } from "./__generated__/fabbrica";

const prisma = new PrismaClient();
initialize({ client: prisma });

async function seed() {
  const UserFactory = defineUserFactory();

  await UserFactory.create();
  await UserFactory.create({ name: "Alice" });
  await UserFactory.create({ id: "user002", name: "Bob" });

  console.log(await prisma.user.count()); // 3
}

seed();

Note: The factories uses Prisma client instance passed by initialize function.

Tips

Works with jest-prisma

If you use @quramy/jest-prisma or @quramy/jest-prisma-node, you can pass @quramy/prisma-fabbrica/scripts/jest-prisma to setupFilesAfterEnv in your Jest configuration file.

/* jset.config.mjs */

export default {
  preset: "ts-jest",
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testEnvironment: "@quramy/jest-prisma/environment",
  setupFilesAfterEnv: ["@quramy/prisma-fabbrica/scripts/jest-prisma"],
};

This script calls prisma-fabbrica's initialize function and configures Prisma client used from each factory to integrate to join to transaction managed by jest-prisma.

License

MIT

Keywords

FAQs

Package last updated on 22 Nov 2022

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc