2021-07-06 16:10:22 +00:00
|
|
|
import { PrismaClient } from "@prisma/client";
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2021-04-11 17:12:18 +00:00
|
|
|
let prisma: PrismaClient;
|
2021-07-06 16:10:22 +00:00
|
|
|
const globalAny: any = global;
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2021-07-06 16:10:22 +00:00
|
|
|
if (process.env.NODE_ENV === "production") {
|
2021-04-11 17:12:18 +00:00
|
|
|
prisma = new PrismaClient();
|
2021-03-22 13:48:48 +00:00
|
|
|
} else {
|
2021-04-29 13:47:01 +00:00
|
|
|
if (!globalAny.prisma) {
|
|
|
|
globalAny.prisma = new PrismaClient();
|
2021-03-22 13:48:48 +00:00
|
|
|
}
|
2021-04-29 13:47:01 +00:00
|
|
|
prisma = globalAny.prisma;
|
2021-03-22 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:10:22 +00:00
|
|
|
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;
|