Adds prisma helper function whereAndSelect

pull/345/head
Alex van Andel 2021-07-06 16:10:22 +00:00
parent 90d6f8faee
commit 47d7634638
1 changed files with 18 additions and 4 deletions

View File

@ -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;
const whereAndSelect = (modelQuery, criteria: Record<string, unknown>, pluckedAttributes: string[]) =>
modelQuery({
where: criteria,
select: pluckedAttributes.reduce(
(select: { [string]: boolean }, attr: string) => ({
...select,
[attr]: true,
}),
{}
),
});
export { whereAndSelect };
export default prisma;