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:
139
backend/prisma/migrations/20251112205214_init/migration.sql
Normal file
139
backend/prisma/migrations/20251112205214_init/migration.sql
Normal file
@@ -0,0 +1,139 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "users" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"username" VARCHAR(50) NOT NULL,
|
||||
"email" VARCHAR(255) NOT NULL,
|
||||
"password_hash" VARCHAR(255) NOT NULL,
|
||||
"avatar" VARCHAR(255),
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "events" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"name" VARCHAR(255) NOT NULL,
|
||||
"location" VARCHAR(255) NOT NULL,
|
||||
"start_date" DATE NOT NULL,
|
||||
"end_date" DATE NOT NULL,
|
||||
"worldsdc_id" VARCHAR(100),
|
||||
"participants_count" INTEGER NOT NULL DEFAULT 0,
|
||||
"description" TEXT,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "events_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "chat_rooms" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"event_id" INTEGER,
|
||||
"type" VARCHAR(20) NOT NULL,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "chat_rooms_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "messages" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"room_id" INTEGER NOT NULL,
|
||||
"user_id" INTEGER NOT NULL,
|
||||
"content" TEXT NOT NULL,
|
||||
"type" VARCHAR(20) NOT NULL,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "matches" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"user1_id" INTEGER NOT NULL,
|
||||
"user2_id" INTEGER NOT NULL,
|
||||
"event_id" INTEGER NOT NULL,
|
||||
"room_id" INTEGER,
|
||||
"status" VARCHAR(20) NOT NULL DEFAULT 'pending',
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "matches_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "ratings" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"match_id" INTEGER NOT NULL,
|
||||
"rater_id" INTEGER NOT NULL,
|
||||
"rated_id" INTEGER NOT NULL,
|
||||
"score" INTEGER NOT NULL,
|
||||
"comment" TEXT,
|
||||
"would_collaborate_again" BOOLEAN NOT NULL DEFAULT false,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "ratings_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "events_worldsdc_id_key" ON "events"("worldsdc_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "messages_room_id_idx" ON "messages"("room_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "messages_created_at_idx" ON "messages"("created_at");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "matches_user1_id_idx" ON "matches"("user1_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "matches_user2_id_idx" ON "matches"("user2_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "matches_event_id_idx" ON "matches"("event_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "matches_user1_id_user2_id_event_id_key" ON "matches"("user1_id", "user2_id", "event_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ratings_rated_id_idx" ON "ratings"("rated_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "ratings_match_id_rater_id_rated_id_key" ON "ratings"("match_id", "rater_id", "rated_id");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "chat_rooms" ADD CONSTRAINT "chat_rooms_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "events"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_room_id_fkey" FOREIGN KEY ("room_id") REFERENCES "chat_rooms"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "matches" ADD CONSTRAINT "matches_user1_id_fkey" FOREIGN KEY ("user1_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "matches" ADD CONSTRAINT "matches_user2_id_fkey" FOREIGN KEY ("user2_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "matches" ADD CONSTRAINT "matches_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "events"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "matches" ADD CONSTRAINT "matches_room_id_fkey" FOREIGN KEY ("room_id") REFERENCES "chat_rooms"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ratings" ADD CONSTRAINT "ratings_match_id_fkey" FOREIGN KEY ("match_id") REFERENCES "matches"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ratings" ADD CONSTRAINT "ratings_rater_id_fkey" FOREIGN KEY ("rater_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ratings" ADD CONSTRAINT "ratings_rated_id_fkey" FOREIGN KEY ("rated_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
3
backend/prisma/migrations/migration_lock.toml
Normal file
3
backend/prisma/migrations/migration_lock.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
128
backend/prisma/schema.prisma
Normal file
128
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,128 @@
|
||||
// Prisma schema for spotlight.cam
|
||||
// Database: PostgreSQL 15
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
// Users table
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique @db.VarChar(50)
|
||||
email String @unique @db.VarChar(255)
|
||||
passwordHash String @map("password_hash") @db.VarChar(255)
|
||||
avatar String? @db.VarChar(255)
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
// Relations
|
||||
messages Message[]
|
||||
matchesAsUser1 Match[] @relation("MatchUser1")
|
||||
matchesAsUser2 Match[] @relation("MatchUser2")
|
||||
ratingsGiven Rating[] @relation("RaterRatings")
|
||||
ratingsReceived Rating[] @relation("RatedRatings")
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
// Events table (dance events from worldsdc.com)
|
||||
model Event {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @db.VarChar(255)
|
||||
location String @db.VarChar(255)
|
||||
startDate DateTime @map("start_date") @db.Date
|
||||
endDate DateTime @map("end_date") @db.Date
|
||||
worldsdcId String? @unique @map("worldsdc_id") @db.VarChar(100)
|
||||
participantsCount Int @default(0) @map("participants_count")
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
|
||||
// Relations
|
||||
chatRooms ChatRoom[]
|
||||
matches Match[]
|
||||
|
||||
@@map("events")
|
||||
}
|
||||
|
||||
// Chat rooms (event chat and private 1:1 chat)
|
||||
model ChatRoom {
|
||||
id Int @id @default(autoincrement())
|
||||
eventId Int? @map("event_id")
|
||||
type String @db.VarChar(20) // 'event' or 'private'
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
|
||||
// Relations
|
||||
event Event? @relation(fields: [eventId], references: [id])
|
||||
messages Message[]
|
||||
matches Match[]
|
||||
|
||||
@@map("chat_rooms")
|
||||
}
|
||||
|
||||
// Messages (text messages and video links)
|
||||
model Message {
|
||||
id Int @id @default(autoincrement())
|
||||
roomId Int @map("room_id")
|
||||
userId Int @map("user_id")
|
||||
content String @db.Text
|
||||
type String @db.VarChar(20) // 'text', 'link', 'video'
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
|
||||
// Relations
|
||||
room ChatRoom @relation(fields: [roomId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
|
||||
@@index([roomId])
|
||||
@@index([createdAt])
|
||||
@@map("messages")
|
||||
}
|
||||
|
||||
// Matches (pairs of users for collaboration)
|
||||
model Match {
|
||||
id Int @id @default(autoincrement())
|
||||
user1Id Int @map("user1_id")
|
||||
user2Id Int @map("user2_id")
|
||||
eventId Int @map("event_id")
|
||||
roomId Int? @map("room_id")
|
||||
status String @default("pending") @db.VarChar(20) // 'pending', 'accepted', 'completed'
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
|
||||
// Relations
|
||||
user1 User @relation("MatchUser1", fields: [user1Id], references: [id])
|
||||
user2 User @relation("MatchUser2", fields: [user2Id], references: [id])
|
||||
event Event @relation(fields: [eventId], references: [id])
|
||||
room ChatRoom? @relation(fields: [roomId], references: [id])
|
||||
ratings Rating[]
|
||||
|
||||
@@unique([user1Id, user2Id, eventId])
|
||||
@@index([user1Id])
|
||||
@@index([user2Id])
|
||||
@@index([eventId])
|
||||
@@map("matches")
|
||||
}
|
||||
|
||||
// Ratings (user ratings after collaboration)
|
||||
model Rating {
|
||||
id Int @id @default(autoincrement())
|
||||
matchId Int @map("match_id")
|
||||
raterId Int @map("rater_id")
|
||||
ratedId Int @map("rated_id")
|
||||
score Int // 1-5
|
||||
comment String? @db.Text
|
||||
wouldCollaborateAgain Boolean @default(false) @map("would_collaborate_again")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
|
||||
// Relations
|
||||
match Match @relation(fields: [matchId], references: [id])
|
||||
rater User @relation("RaterRatings", fields: [raterId], references: [id])
|
||||
rated User @relation("RatedRatings", fields: [ratedId], references: [id])
|
||||
|
||||
@@unique([matchId, raterId, ratedId])
|
||||
@@index([ratedId])
|
||||
@@map("ratings")
|
||||
}
|
||||
86
backend/prisma/seed.js
Normal file
86
backend/prisma/seed.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('🌱 Seeding database...');
|
||||
|
||||
// Create events
|
||||
const events = await Promise.all([
|
||||
prisma.event.create({
|
||||
data: {
|
||||
name: 'Warsaw Dance Festival 2025',
|
||||
location: 'Warsaw, Poland',
|
||||
startDate: new Date('2025-03-15'),
|
||||
endDate: new Date('2025-03-17'),
|
||||
worldsdcId: 'wdf-2025',
|
||||
participantsCount: 156,
|
||||
description: 'The biggest West Coast Swing event in Central Europe',
|
||||
},
|
||||
}),
|
||||
prisma.event.create({
|
||||
data: {
|
||||
name: 'Swing Camp Barcelona 2025',
|
||||
location: 'Barcelona, Spain',
|
||||
startDate: new Date('2025-04-20'),
|
||||
endDate: new Date('2025-04-23'),
|
||||
worldsdcId: 'scb-2025',
|
||||
participantsCount: 203,
|
||||
description: 'International swing dance camp with workshops and socials',
|
||||
},
|
||||
}),
|
||||
prisma.event.create({
|
||||
data: {
|
||||
name: 'Blues Week Herräng 2025',
|
||||
location: 'Herräng, Sweden',
|
||||
startDate: new Date('2025-07-14'),
|
||||
endDate: new Date('2025-07-20'),
|
||||
worldsdcId: 'bwh-2025',
|
||||
participantsCount: 89,
|
||||
description: 'Week-long blues dance intensive in the heart of Sweden',
|
||||
},
|
||||
}),
|
||||
prisma.event.create({
|
||||
data: {
|
||||
name: 'Krakow Swing Connection 2025',
|
||||
location: 'Krakow, Poland',
|
||||
startDate: new Date('2025-05-10'),
|
||||
endDate: new Date('2025-05-12'),
|
||||
worldsdcId: 'ksc-2025',
|
||||
participantsCount: 127,
|
||||
description: 'Three days of swing dancing in historic Krakow',
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
console.log(`✅ Created ${events.length} events`);
|
||||
|
||||
// Create event chat rooms for each event
|
||||
const chatRooms = await Promise.all(
|
||||
events.map((event) =>
|
||||
prisma.chatRoom.create({
|
||||
data: {
|
||||
eventId: event.id,
|
||||
type: 'event',
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
console.log(`✅ Created ${chatRooms.length} event chat rooms`);
|
||||
|
||||
console.log('🎉 Seeding completed successfully!');
|
||||
console.log('');
|
||||
console.log('Created:');
|
||||
console.log(` - ${events.length} events`);
|
||||
console.log(` - ${chatRooms.length} chat rooms`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error('❌ Seeding failed:', e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user