-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e71985a
commit 2c53cc3
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |