feat: add PostgreSQL database with Prisma ORM
Phase 1 - Step 2: PostgreSQL Setup **Infrastructure:** - Add PostgreSQL 15 Alpine container to docker-compose.yml - Configure persistent volume for database data - Update backend Dockerfile with OpenSSL for Prisma compatibility **Database Schema (Prisma):** - 6 tables: users, events, chat_rooms, messages, matches, ratings - Foreign key relationships and cascading deletes - Performance indexes on frequently queried columns - Unique constraints for data integrity **Prisma Setup:** - Prisma Client for database queries - Migration system with initial migration - Seed script with 4 test events and chat rooms - Database connection utility with singleton pattern **API Implementation:** - GET /api/events - List all events (with filtering and sorting) - GET /api/events/:id - Get single event with relations - Database connection test on server startup - Graceful database disconnect on shutdown **Seed Data:** - Warsaw Dance Festival 2025 - Swing Camp Barcelona 2025 - Blues Week Herräng 2025 - Krakow Swing Connection 2025 **Testing:** - Database connection verified ✅ - API endpoints returning data from PostgreSQL ✅ - Migrations applied successfully ✅ All systems operational 🚀
This commit is contained in:
@@ -27,10 +27,10 @@ app.get('/api/health', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// API routes (future)
|
||||
// API routes
|
||||
app.use('/api/events', require('./routes/events'));
|
||||
// app.use('/api/auth', require('./routes/auth'));
|
||||
// app.use('/api/users', require('./routes/users'));
|
||||
// app.use('/api/events', require('./routes/events'));
|
||||
// app.use('/api/matches', require('./routes/matches'));
|
||||
// app.use('/api/ratings', require('./routes/ratings'));
|
||||
|
||||
|
||||
71
backend/src/routes/events.js
Normal file
71
backend/src/routes/events.js
Normal file
@@ -0,0 +1,71 @@
|
||||
const express = require('express');
|
||||
const { prisma } = require('../utils/db');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// GET /api/events - List all events
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const events = await prisma.event.findMany({
|
||||
orderBy: {
|
||||
startDate: 'asc',
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
location: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
worldsdcId: true,
|
||||
participantsCount: true,
|
||||
description: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
count: events.length,
|
||||
data: events,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/events/:id - Get event by ID
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const event = await prisma.event.findUnique({
|
||||
where: {
|
||||
id: parseInt(id),
|
||||
},
|
||||
include: {
|
||||
chatRooms: true,
|
||||
_count: {
|
||||
select: {
|
||||
matches: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Event not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: event,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,31 +1,40 @@
|
||||
require('dotenv').config();
|
||||
const app = require('./app');
|
||||
const { testConnection, disconnect } = require('./utils/db');
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
const server = app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log('=================================');
|
||||
console.log('🚀 spotlight.cam Backend Started');
|
||||
console.log('=================================');
|
||||
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
|
||||
console.log(`Server running on port: ${PORT}`);
|
||||
console.log(`Health check: http://localhost:${PORT}/api/health`);
|
||||
console.log('=================================');
|
||||
async function startServer() {
|
||||
// Test database connection
|
||||
await testConnection();
|
||||
|
||||
const server = app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log('=================================');
|
||||
console.log('🚀 spotlight.cam Backend Started');
|
||||
console.log('=================================');
|
||||
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
|
||||
console.log(`Server running on port: ${PORT}`);
|
||||
console.log(`Health check: http://localhost:${PORT}/api/health`);
|
||||
console.log('=================================');
|
||||
});
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
startServer().catch((err) => {
|
||||
console.error('Failed to start server:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGTERM', () => {
|
||||
process.on('SIGTERM', async () => {
|
||||
console.log('SIGTERM received, shutting down gracefully...');
|
||||
server.close(() => {
|
||||
console.log('Server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
await disconnect();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('SIGINT received, shutting down gracefully...');
|
||||
server.close(() => {
|
||||
console.log('Server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
await disconnect();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
41
backend/src/utils/db.js
Normal file
41
backend/src/utils/db.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
|
||||
// Singleton instance of Prisma Client
|
||||
let prisma;
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
prisma = new PrismaClient();
|
||||
} else {
|
||||
// In development, use a global variable to prevent multiple instances
|
||||
// during hot reload
|
||||
if (!global.prisma) {
|
||||
global.prisma = new PrismaClient({
|
||||
log: ['error', 'warn'],
|
||||
});
|
||||
}
|
||||
prisma = global.prisma;
|
||||
}
|
||||
|
||||
// Test database connection
|
||||
async function testConnection() {
|
||||
try {
|
||||
await prisma.$connect();
|
||||
console.log('✅ Database connected successfully');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('❌ Database connection failed:', error.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Graceful shutdown
|
||||
async function disconnect() {
|
||||
await prisma.$disconnect();
|
||||
console.log('Database disconnected');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
prisma,
|
||||
testConnection,
|
||||
disconnect,
|
||||
};
|
||||
Reference in New Issue
Block a user