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

@ubie/prisma-cleaner

v1.1.3

Published

Prisma cleanup tables extension for testing

Downloads

2,737

Readme

prisma-cleaner

prisma-cleaner is a prisma extension to automatically delete data when running tests using prisma. Performance is optimized by executing deletion only on tables where data has been added.

import { Prisma, PrismaClient } from "@prisma/client";
import { PrismaCleaner } from "@ubie/prisma-cleaner";

const cleaner = new PrismaCleaner({
  prisma: new PrismaClient(),
  models: Prisma.dmmf.datamodel.models,
});

const prisma = new PrismaClient().$extends(cleaner.withCleaner());

(async () => {
  await prisma.user.create({ ... });

  // Delete only user table
  await cleaner.cleanup(); // => TRUNCATE TABLE "public"."user" CASCADE

  await prisma.user.create({ ... });
  await prisma.comment.create({ ... });

  // Delete post and comment tables, user table is not included
  await cleaner.cleanup(); // => TRUNCATE TABLE "public"."post", "public"."comment" CASCADE
})();

[!WARNING] Currently only PostgreSQL is supported.

Installation

$ npm install --save-dev @ubie/prisma-cleaner

Usage

with jest

Application code

// ./src/client.ts
import { PrismaClient } from "@prisma/client";

export const prisma = new PrismaClient();
// ./src/UserService.ts
import type { PrismaClient } from "@prisma/client";

export class UserService {
  constructor(private readonly prisma: PrismaClient) {}
  createUser(name: string) {
    return this.prisma.user.create({
      data: { name },
    });
  }
}

Test config

// jest.config.ts
const config: Config = {
  // ...
  globalSetup: "<rootDir>/test/global-setup.ts",
  setupFilesAfterEnv: ["<rootDir>/test/setup.ts"],
};
// ./test/cleander.ts
import { Prisma, PrismaClient } from "@prisma/client";
import { PrismaCleaner } from "@ubie/prisma-cleaner";

export const cleaner = new PrismaCleaner({
  prisma: new PrismaClient(),
  models: Prisma.dmmf.datamodel.models,
});
// ./test/global-setup.ts
import { cleaner } from "./cleaner";

export default async function setup() {
  // This is optional, cleaning all tables first increases the stability of the test.
  await cleaner.cleanupAllTables();
}
// ./test/setup.ts
import { PrismaClient } from "@prisma/client";
import { cleaner } from "./cleaner";

afterEach(async () => {
  await cleaner.cleanup();
});

Test code

// ./src/UserService.test.ts
import { PrismaClient } from "@prisma/client";
import { UserService } from "./UserService";
import { cleaner } from "../test/cleaner";

describe("UserService", () => {
  const prisma = new PrismaClient().$extends(cleaner.withCleaner()) as PrismaClient;
  const userService = new UserService(prisma);

  it("should create a new user", async () => {
    // this record will delete by prisma-cleaner in afterEach defined by setup.ts
    const user = await userService.createUser("xxx");
    expect(user.name).toEqual("xxx");
    expect(await prisma.user.count()).toEqual(1);
  });

  it("should be cleanup user table by cleaner", async () => {
    const count = await prisma.user.count();
    expect(count).toEqual(0);
  });
});

See more examples.

manually cleanup

Prisma-cleaner adds tables targeted for deletion triggered by the execution of Prisma's create, createMany, upsert. If added via $executeRaw or similar, they will not be automatically deleted, so you will need to manually delete them.

await prisma.$executeRaw`INSERT INTO "User" (name) VALUES ('xxx')`;

// You should delete manually.
await cleaner.cleanupTables(["User"]);

License

MIT License.