PRIMEIRO ENVIO
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
// Simple logger for database operations
|
||||
const logger = {
|
||||
info: (message: string, ...args: any[]) => console.log(`[INFO] ${message}`, ...args),
|
||||
error: (message: string, ...args: any[]) => console.error(`[ERROR] ${message}`, ...args),
|
||||
warn: (message: string, ...args: any[]) => console.warn(`[WARN] ${message}`, ...args),
|
||||
};
|
||||
|
||||
let prisma: PrismaClient;
|
||||
|
||||
declare global {
|
||||
var __prisma: PrismaClient | undefined;
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
prisma = new PrismaClient({
|
||||
log: ['error', 'warn'],
|
||||
});
|
||||
} else {
|
||||
if (!global.__prisma) {
|
||||
global.__prisma = new PrismaClient({
|
||||
log: ['query', 'error', 'warn'],
|
||||
});
|
||||
}
|
||||
prisma = global.__prisma;
|
||||
}
|
||||
|
||||
// Test database connectivity
|
||||
export async function testDatabaseConnection(): Promise<boolean> {
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$queryRaw`SELECT 1`;
|
||||
logger.info('Database connection successful');
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error('Database connection failed:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Graceful shutdown
|
||||
export async function disconnectDatabase(): Promise<void> {
|
||||
try {
|
||||
await prisma.$disconnect();
|
||||
logger.info('Database disconnected successfully');
|
||||
} catch (error) {
|
||||
logger.error('Error disconnecting from database:', error);
|
||||
}
|
||||
}
|
||||
|
||||
export { prisma };
|
||||
export default prisma;
|
||||
Reference in New Issue
Block a user