cal.pub0.org/lib/prisma.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

import { PrismaClient } from "@prisma/client";
2021-03-22 13:48:48 +00:00
2021-04-11 17:12:18 +00:00
let prisma: PrismaClient;
const globalAny: any = global;
2021-03-22 13:48:48 +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({
log: ["query", "error", "warn"],
});
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 18:20:25 +00:00
const pluck = (select: Record<string, boolean>, attr: string) => {
const parts = attr.split(".");
const alwaysAttr = parts[0];
const pluckedValue =
parts.length > 1
? {
select: pluck(select[alwaysAttr] ? select[alwaysAttr].select : {}, parts.slice(1).join(".")),
}
: true;
return {
...select,
[alwaysAttr]: pluckedValue,
};
};
/**
* @deprecated
* This function will always return `any` type,
* See https://github.com/calendso/calendso/pull/460 for alternative approach
*/
const whereAndSelect = (modelQuery, criteria: Record<string, unknown>, pluckedAttributes: string[]) =>
modelQuery({
where: criteria,
2021-07-06 18:20:25 +00:00
select: pluckedAttributes.reduce(pluck, {}),
});
export { whereAndSelect };
export default prisma;