UNPKG

@prisma-extensions/factory

Version:

Factory enables you to easily create and build database records for testing or seeding purposes.

85 lines (65 loc) 2.09 kB
# @prisma-extensions/factory - `@prisma-extensions/factory` - Create factories - `@prisma-extensions/msoft-delete` - Enable a `deletedAt` field. - `@prisma-extensions/truncate` - Truncate ## Challenges - When using `model.$allModels` you can't get the model the method is being acted on. - When using `model.$allModels` you can't get the types of the model the method is being acted on. ## Blockers ## Proposed API - Utilizes `@prisma-extensions/truncate` ```ts const xprisma = new PrismaClient() .$extends(factoryExtension) .$extends(truncateExtension) const UserFactory = xprisma.user.createFactory({ firstName: "Jane", lastName: "Doe", }) describe("Post", () => { describe("Standard" () => { afterEach(async () => { await xprisma.$truncate() }) test("example", async () => { const user = UserFactory.build() const post = await PostFactory.create({ title: "Foo", content: "Bar", author: { create: user, }, }) // ... }) }) // Tests should be isolated (use sparingly) describe("Persistent Record", () => { const user = UserFactory.create() afterAll(async () => { await xprisma.$truncate({ only: ["User"] }) }) afterEach(async () => { await xprisma.$truncate({ exclude: ["User"] }) }) test("example", async () => { const post = await PostFactory.create({ title: "Foo", content: "Bar", author: { connect: { id: user.id }, }, }) // ... }) }) }) ``` ## Useful Links Learn more about the power of Turborepo: - [Pipelines](https://turbo.build/repo/docs/core-concepts/monorepos/running-tasks) - [Caching](https://turbo.build/repo/docs/core-concepts/caching) - [Remote Caching](https://turbo.build/repo/docs/core-concepts/remote-caching) - [Filtering](https://turbo.build/repo/docs/core-concepts/monorepos/filtering) - [Configuration Options](https://turbo.build/repo/docs/reference/configuration) - [CLI Usage](https://turbo.build/repo/docs/reference/command-line-reference)