-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
35 lines (29 loc) · 1.19 KB
/
db.ts
File metadata and controls
35 lines (29 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { PrismaClient } from '../../prisma/generated/client';
import { CustomPrismaClient } from '../types';
import logger from './logger';
const globalForPrisma = global as typeof globalThis & {
prisma: CustomPrismaClient | undefined;
};
export let prismaClient: CustomPrismaClient;
logger.info(`using prisma client in ${process.env.NODE_ENV} mode`);
if (globalForPrisma.prisma) {
logger.info('using cached prisma client...');
prismaClient = globalForPrisma.prisma;
} else {
logger.info('instantiating new prisma client...');
prismaClient = new PrismaClient({
// Read the URL programmatically to support replacing .env with .env.test in CLI
datasourceUrl: process.env.DATABASE_URL,
// Globally omit the password field; need to be sat to false explicitly, to retrieve a user with password
omit: { user: { password: true } },
});
}
if (process.env.NODE_ENV !== 'production') {
logger.info('caching prisma client...');
const { protocol, host, pathname } = new URL(
process.env.DATABASE_URL ?? 'postgres://x:y@z:5432/expect_db_url_env_var'
);
logger.info(`DB URL: ${protocol}//xxx:***@${host}${pathname}`);
globalForPrisma.prisma = prismaClient;
}
export default prismaClient;