prisma-exclude
v1.0.2
Published
Exclude fields from your Prisma queries
Downloads
529
Maintainers
Readme
prisma.user.findMany({
select: prisma.$exclude("user", ["password"]),
})Table of contents
Installation
It is assumed that both
prismaand@prisma/clientpackages are installed and that the Prisma Client has been generated.
$ yarn add prisma-excludeor
$ npm install prisma-excludeUsage
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"]);
}
}
});