mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 18:32:10 +01:00
- Implement repository pattern for data access layer - Add comprehensive service layer for business logic - Create scheduler management system with health monitoring - Add bounded buffer utility for memory management - Enhance security audit logging with retention policies
71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
// Enhanced Prisma client setup with connection pooling
|
|
import pkg from "@prisma/client";
|
|
|
|
const { PrismaClient } = pkg;
|
|
|
|
import { createEnhancedPrismaClient } from "./database-pool";
|
|
import { env } from "./env";
|
|
|
|
// Add prisma to the NodeJS global type
|
|
declare const global: {
|
|
prisma: InstanceType<typeof PrismaClient> | undefined;
|
|
};
|
|
|
|
// Connection pooling configuration
|
|
const createPrismaClient = () => {
|
|
// Use enhanced pooling in production or when explicitly enabled
|
|
const useEnhancedPooling =
|
|
process.env.NODE_ENV === "production" ||
|
|
process.env.USE_ENHANCED_POOLING === "true";
|
|
|
|
if (useEnhancedPooling) {
|
|
console.log("Using enhanced database connection pooling with PG adapter");
|
|
return createEnhancedPrismaClient();
|
|
}
|
|
|
|
// Default Prisma client with basic configuration
|
|
return new PrismaClient({
|
|
log:
|
|
process.env.NODE_ENV === "development"
|
|
? ["query", "error", "warn"]
|
|
: ["error"],
|
|
datasources: {
|
|
db: {
|
|
url: env.DATABASE_URL,
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
// Initialize Prisma Client with singleton pattern
|
|
const prisma = global.prisma || createPrismaClient();
|
|
|
|
// Save in global if we're in development to prevent multiple instances
|
|
if (process.env.NODE_ENV !== "production") {
|
|
global.prisma = prisma;
|
|
}
|
|
|
|
// Graceful shutdown handling
|
|
const gracefulShutdown = async () => {
|
|
console.log("Gracefully disconnecting from database...");
|
|
await prisma.$disconnect();
|
|
process.exit(0);
|
|
};
|
|
|
|
// Handle process termination signals
|
|
process.on("SIGINT", gracefulShutdown);
|
|
process.on("SIGTERM", gracefulShutdown);
|
|
|
|
// Connection health check
|
|
export const checkDatabaseConnection = async (): Promise<boolean> => {
|
|
try {
|
|
await prisma.$queryRaw`SELECT 1`;
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Database connection failed:", error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export { prisma };
|