From 47d7634638ef0f5c5798b7d5aff510bbfa6657dc Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Tue, 6 Jul 2021 16:10:22 +0000 Subject: [PATCH] Adds prisma helper function whereAndSelect --- lib/prisma.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index 4d3f8470..b20a1b48 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -1,9 +1,9 @@ -import { PrismaClient } from '@prisma/client'; +import { PrismaClient } from "@prisma/client"; let prisma: PrismaClient; -const globalAny:any = global; +const globalAny: any = global; -if (process.env.NODE_ENV === 'production') { +if (process.env.NODE_ENV === "production") { prisma = new PrismaClient(); } else { if (!globalAny.prisma) { @@ -12,4 +12,18 @@ if (process.env.NODE_ENV === 'production') { prisma = globalAny.prisma; } -export default prisma; \ No newline at end of file +const whereAndSelect = (modelQuery, criteria: Record, pluckedAttributes: string[]) => + modelQuery({ + where: criteria, + select: pluckedAttributes.reduce( + (select: { [string]: boolean }, attr: string) => ({ + ...select, + [attr]: true, + }), + {} + ), + }); + +export { whereAndSelect }; + +export default prisma;