Skip to content

Commit

Permalink
fix: ensure db is always a prismaclient (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
as1729 authored May 15, 2024
1 parent 79d0e9d commit 9188ee5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions api/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const getCurrentUser = async (
// Verify that the request is coming from the local development environment
// and is only being processed within the local environment
if (process.env.AUTH_PROVIDER === 'local') {
logger.info('Local development environment detected.')
const user: CurrentUser = await db.user.findFirst({
where: { email: token },
include: { agency: true },
Expand Down
36 changes: 24 additions & 12 deletions api/src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,40 @@ async function ssmAuthForURL(databaseURL: string): Promise<string> {
return url.toString()
}

/*
* Instance of the Prisma Client
*/
export let db: PrismaClient
async function createPrismaClient() {
async function getDataSourceURL() {
let datasourceUrl: string
if (process.env.DATABASE_SECRET_SOURCE === 'iam') {
datasourceUrl = await rdsIAMAuthForURL(process.env.DATABASE_URL)
} else if (process.env.DATABASE_SECRET_SOURCE === 'ssm') {
datasourceUrl = await ssmAuthForURL(process.env.DATABASE_URL)
}
return datasourceUrl
}

db = new PrismaClient({
async function getPrismaClient() {
const datasourceUrl = await getDataSourceURL()
const client = new PrismaClient({
log: emitLogLevels(['info', 'warn', 'error']),
datasourceUrl: datasourceUrl,
datasourceUrl,
})
}

createPrismaClient().then(() => {
handlePrismaLogging({
db,
db: client,
logger,
logLevels: ['info', 'warn', 'error'],
})
})
return client
}

/*
* Instance of the Prisma Client
*/
getPrismaClient()
.then((db) => {
logger.info('Prisma Client initialized')
module.exports.db = db
})
.catch((error) => {
logger.error(error)
logger.error('Failed to initialize the Prisma Client. Exiting...')
process.exit(1)
})

0 comments on commit 9188ee5

Please sign in to comment.