Files
towercontrol/services/database.ts
Julien Froidefond 124e8baee8 feat: overhaul TODO.md and enhance Kanban components
- Updated TODO.md to reflect the new project structure and phases, marking several tasks as completed.
- Enhanced Kanban components with a tech-inspired design, including new styles for columns and task cards.
- Removed the obsolete reminders service and task processor, streamlining the codebase for better maintainability.
- Introduced a modern API for task management, including CRUD operations and improved error handling.
- Updated global styles for a cohesive dark theme and added custom scrollbar styles.
2025-09-14 08:15:22 +02:00

65 lines
1.9 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
// Singleton pattern pour Prisma Client
declare global {
var __prisma: PrismaClient | undefined;
}
// Créer une instance unique de Prisma Client
export const prisma = globalThis.__prisma || new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
});
// En développement, stocker l'instance globalement pour éviter les reconnexions
if (process.env.NODE_ENV !== 'production') {
globalThis.__prisma = prisma;
}
// Fonction pour tester la connexion
export async function testDatabaseConnection(): Promise<boolean> {
try {
await prisma.$connect();
console.log('✅ Database connection successful');
return true;
} catch (error) {
console.error('❌ Database connection failed:', error);
return false;
}
}
// Fonction pour fermer la connexion proprement
export async function closeDatabaseConnection(): Promise<void> {
try {
await prisma.$disconnect();
console.log('✅ Database connection closed');
} catch (error) {
console.error('❌ Error closing database connection:', error);
}
}
// Fonction utilitaire pour les transactions
export async function withTransaction<T>(
callback: (tx: Omit<PrismaClient, '$connect' | '$disconnect' | '$on' | '$transaction' | '$extends'>) => Promise<T>
): Promise<T> {
return await prisma.$transaction(async (tx) => {
return await callback(tx);
});
}
// Fonction pour nettoyer la base (utile pour les tests)
export async function clearDatabase(): Promise<void> {
if (process.env.NODE_ENV === 'production') {
throw new Error('Cannot clear database in production');
}
await prisma.taskTag.deleteMany();
await prisma.task.deleteMany();
await prisma.tag.deleteMany();
await prisma.syncLog.deleteMany();
console.log('🧹 Database cleared');
}
// Export par défaut
export default prisma;