Skip to content

Prisma client extension to predefine `select` and `include`

License

Notifications You must be signed in to change notification settings

LeoAso/prisma-extension-selecting

Repository files navigation

prisma-extension-selecting

A Prisma client extension for setting select and include permanently for a model.

Installation

npm install prisma-extension-selecting

Usage

import { PrismaClient } from '@prisma/client'
import selecting from 'prisma-extension-selecting'

// apply the extension
const prisma = new PrismaClient().$extends(selecting)

// set "select" and/or "include" once and those values
// will be used for all applicable queries on `users`
const users = prisma.user.selecting({
  select: {
    id: true,
    name: true,
    posts: { select: { title: true } },
  },
})

// type UserWithPosts = { name: string; id: number; posts: { title: string; }[] }
type UserWithPosts = Prisma.UserGetPayload<typeof users.selection>

// all these queries will return only the fields selected above
users.create({ data: { ... } });
users.findMany();
users.findFirst({ where: { ... } });
users.findUniqueOrThrow({ where: { ... } });
users.update({ where: { ... }, data: { ... } });
users.delete({ where: { ... } });

About

Prisma client extension to predefine `select` and `include`

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published