Skip to content

Commit

Permalink
create prisma wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
abhayrp2000 committed Jul 14, 2023
1 parent e71985a commit 2c53cc3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/utils/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PrismaClient } from "@prisma/client";
import { CustomError } from "./custom-error";
import { logger } from "..";

class PrismaWrapper {
private _prismaClient?: PrismaClient;

get prismaClient() {
if (!this._prismaClient) {
console.error(`Error connecting to DB`);
throw new Error("Server Error");
}

return this._prismaClient;
}

async connect() {
this._prismaClient = new PrismaClient({
datasources: {
db: { url: process.env.POSTGRES_URL },
},
});

await this._prismaClient.$connect();
logger.info(`POSTGRES CONNECTED`);
}

async disconnect() {
this._prismaClient = new PrismaClient({
datasources: {
db: {
url: process.env.POSTGRES_URL,
},
},
});

await this._prismaClient.$disconnect();
logger.info(`POSTGRES DISCONNECTED`);
}
}

export const prismaWrapper = new PrismaWrapper();

0 comments on commit 2c53cc3

Please sign in to comment.