Prisma Client Extensions

Go to GitHub

Simplify Working With Your Database


Factory

Create fake data and records with ease!

npm add @prisma-extensions/factory
import { PrismaClient } from '@prisma/client';
import { factoryExtension } from '@prisma-extensions/factory';

const prisma = new PrismaClient();
const db = prisma.$extends(factoryExtension);

const UserFactory = db.user.$factory({
  firstName: 'Tom',
  lastName: 'Milewski',
})

UserFactory.attributes();

UserFactory.create();

Soft Delete

Hide records without deleting them.

npm add @prisma-extensions/soft-delete
// Requires relevent models to have a `deletedAt` field.

import { PrismaClient } from '@prisma/client';
import { softDeleteExtension } from '@prisma-extensions/soft-delete';

const prisma = new PrismaClient();
const db = prisma.$extends(softDeleteExtension());

await db.user.softDelete({ where: { id: 1 } });
await db.user.softRestore({ where: { id: 1 } });

Truncate

Remove all data, but faster!

npm add @prisma-extensions/truncate
import { PrismaClient } from '@prisma/client';
import { truncateExtension } from '@prisma-extensions/truncate';

const prisma = new PrismaClient();
const db = prisma.$extends(truncateExtension('postgres'));

await db.user.$truncate();

Read Only

No more pesky writes!

npm add @prisma-extensions/read-only
import { PrismaClient } from '@prisma/client';
import { readOnlyExtension } from '@prisma-extensions/read-only';

const prisma = new PrismaClient();
const db = prisma.$extends(readOnlyExtension());

// Throws "Calling `create` method on a read-only client is not allowed"
await db.user.create({
  data: {
    name: 'Tom',
  },
});

Find or Create

Easily create missing records!

npm add @prisma-extensions/find-or-create
import { PrismaClient } from '@prisma/client';
import { findOrCreateExtension } from '@prisma-extensions/find-or-create';

const prisma = new PrismaClient();
const db = prisma.$extends(findOrCreateExtension);

await db.user.findOrCreate({
  where: {
    id: 0,
  },
  data: {
    firstName: 'A',
    lastName: 'Name',
  },
});