mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-02-13 18:55:43 +01:00
- Fix syntax errors in skills markdown files (.github/skills, .opencode/skills) - Change typescript to tsx for code blocks with JSX - Replace ellipsis (...) in array examples with valid syntax - Separate CSS from TypeScript into distinct code blocks - Convert JavaScript object examples to valid JSON in docs - Fix enum definitions with proper comma separation
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
// Enhanced Prisma client setup with connection pooling
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
// Add prisma to the NodeJS global type
|
|
declare const global: {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
// Create standard Prisma client (no enhanced pooling at build time)
|
|
const createPrismaClient = (): PrismaClient => {
|
|
return new PrismaClient({
|
|
log:
|
|
process.env.NODE_ENV === "development"
|
|
? ["query", "error", "warn"]
|
|
: ["error"],
|
|
});
|
|
};
|
|
|
|
// Async factory for enhanced pooling (only call at runtime, not build time)
|
|
export const createEnhancedClient = async (): Promise<PrismaClient> => {
|
|
const { createEnhancedPrismaClient } = await import("./database-pool");
|
|
console.log("Using enhanced database connection pooling with PG adapter");
|
|
return createEnhancedPrismaClient();
|
|
};
|
|
|
|
// Lazy initialization - only create when first accessed
|
|
let _prisma: PrismaClient | undefined;
|
|
|
|
const getPrisma = (): PrismaClient => {
|
|
if (!_prisma) {
|
|
_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;
|
|
}
|
|
}
|
|
return _prisma!;
|
|
};
|
|
|
|
// Export a proxy that lazily initializes the client
|
|
export const prisma = new Proxy({} as PrismaClient, {
|
|
get(_target, prop) {
|
|
return getPrisma()[prop as keyof PrismaClient];
|
|
},
|
|
});
|
|
|
|
// 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;
|
|
}
|
|
};
|