diff --git a/api/src/lib/auth.ts b/api/src/lib/auth.ts index e7a04837..7367e9f3 100644 --- a/api/src/lib/auth.ts +++ b/api/src/lib/auth.ts @@ -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 }, diff --git a/api/src/lib/db.ts b/api/src/lib/db.ts index 246fd7c2..ad1d9ca9 100644 --- a/api/src/lib/db.ts +++ b/api/src/lib/db.ts @@ -35,28 +35,40 @@ async function ssmAuthForURL(databaseURL: string): Promise { 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) + })