Skip to content

Commit

Permalink
Chore: Remove whereAndSelect and fix typescript of main PrismaClient …
Browse files Browse the repository at this point in the history
…adapter (calcom#966)
  • Loading branch information
emrysal authored Oct 15, 2021
1 parent 78182db commit c01004b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 129 deletions.
47 changes: 10 additions & 37 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
import { PrismaClient } from "@prisma/client";

let prisma: PrismaClient;
const globalAny: any = global;

if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient();
} else {
if (!globalAny.prisma) {
globalAny.prisma = new PrismaClient({
log: ["query", "error", "warn"],
});
}
prisma = globalAny.prisma;
declare global {
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined;
}

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,
select: pluckedAttributes.reduce(pluck, {}),
export const prisma =
globalThis.prisma ||
new PrismaClient({
log: ["query", "error", "warn"],
});

export { whereAndSelect };
if (process.env.NODE_ENV !== "production") {
globalThis.prisma = prisma;
}

export default prisma;
92 changes: 0 additions & 92 deletions test/lib/prisma.test.ts

This file was deleted.

0 comments on commit c01004b

Please sign in to comment.