1.0.2 • Published 2 years ago

prisma-exclude v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago
prisma.user.findMany({
  select: prisma.$exclude("user", ["password"]),
})

codecov


Table of contents

Installation

It is assumed that both prisma and @prisma/client packages are installed and that the Prisma Client has been generated.

$ yarn add prisma-exclude

or

$ npm install prisma-exclude

Usage

prisma-exclude can be used in two different ways. You can wrap your instance of the Prisma Client with withExclude or directly pass your client to the prismaExclude function.

Both ways give you access to the exclude function which accepts a model name and an array of fields to exclude, all while maintaining type safety. This means that both model names and fields will have access to autocompletion (depending on your IDE).

Using withExclude

Wrap your Prisma Client with withExclude when initializing your instance.

import { PrismaClient } from "@prisma/client";
import { withExclude } from "prisma-exclude";

export const prisma = withExclude(new PrismaClient());

Then use the new available method $exclude to omit fields from your queries

import { prisma } from "./instance";

const users = await prisma.user.findMany({
  select: prisma.$exclude("user", ["password"]);
});

Using prismaExclude

If you don't want an extra method in your Prisma Client instance, you can initialize your own exclude function by providing the instance to prismaExclude

import { PrismaClient } from "@prisma/client";
import { prismaExclude } from "prisma-exclude";

export const prisma = new PrismaClient();
export const exclude = prismaExclude(prisma); 

Then you can use it in your queries

import { prisma, exclude } from "./instance";

const addresses = await prisma.address.findMany({
  where: {
    active: true,
  },
  include: {
    user: {
      select: exclude("user", ["password"]);
    }
  }
});