Switch from MySQL to SQLite for simpler database setup
This commit is contained in:
@@ -37,6 +37,7 @@ RUN npm install --production
|
||||
# Copy built files from builder stage
|
||||
COPY --from=builder /app/dist ./dist
|
||||
COPY --from=builder /app/frontend/build ./frontend/build
|
||||
COPY --from=builder /app/database.sqlite ./database.sqlite
|
||||
|
||||
# Expose port
|
||||
EXPOSE 3000
|
||||
|
||||
@@ -13,23 +13,7 @@ services:
|
||||
- /app/node_modules
|
||||
environment:
|
||||
- NODE_ENV=development
|
||||
- DB_HOST=mysql
|
||||
- DB_USER=root
|
||||
- DB_PASSWORD=password
|
||||
- DB_NAME=rsvp_db
|
||||
depends_on:
|
||||
- mysql
|
||||
|
||||
mysql:
|
||||
container_name: rsvp_manager_db
|
||||
image: mysql:8.0
|
||||
ports:
|
||||
- "3306:3306"
|
||||
environment:
|
||||
- MYSQL_ROOT_PASSWORD=password
|
||||
- MYSQL_DATABASE=rsvp_db
|
||||
volumes:
|
||||
- mysql_data:/var/lib/mysql
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
mysql_data:
|
||||
@@ -11,14 +11,16 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"mysql2": "^3.6.5",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.3.1"
|
||||
"dotenv": "^16.3.1",
|
||||
"sqlite3": "^5.1.7",
|
||||
"sqlite": "^5.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/node": "^20.8.2",
|
||||
"@types/cors": "^2.8.13",
|
||||
"@types/sqlite3": "^3.1.11",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^4.9.5",
|
||||
"jest": "^29.7.0",
|
||||
|
||||
73
src/index.ts
73
src/index.ts
@@ -1,6 +1,7 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import mysql from 'mysql2/promise';
|
||||
import sqlite3 from 'sqlite3';
|
||||
import { open } from 'sqlite';
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
|
||||
@@ -14,26 +15,27 @@ app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Database connection
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST || 'mysql',
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || 'password',
|
||||
database: process.env.DB_NAME || 'rsvp_db',
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0
|
||||
let db;
|
||||
async function initializeDatabase() {
|
||||
try {
|
||||
db = await open({
|
||||
filename: './database.sqlite',
|
||||
driver: sqlite3.Database
|
||||
});
|
||||
|
||||
// Test database connection
|
||||
async function testDatabaseConnection() {
|
||||
try {
|
||||
const connection = await pool.getConnection();
|
||||
console.log('Database connection successful');
|
||||
connection.release();
|
||||
return true;
|
||||
await db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
date TEXT NOT NULL,
|
||||
location TEXT,
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
console.log('Database initialized successfully');
|
||||
} catch (error) {
|
||||
console.error('Database connection failed:', error);
|
||||
return false;
|
||||
console.error('Error initializing database:', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,11 +45,7 @@ app.use(express.static(path.join(__dirname, '../frontend/build')));
|
||||
// API Routes
|
||||
app.get('/api/events', async (req, res) => {
|
||||
try {
|
||||
const isConnected = await testDatabaseConnection();
|
||||
if (!isConnected) {
|
||||
throw new Error('Database connection failed');
|
||||
}
|
||||
const [rows] = await pool.query('SELECT * FROM events');
|
||||
const rows = await db.all('SELECT * FROM events');
|
||||
console.log('Fetched events:', rows);
|
||||
res.json(rows);
|
||||
} catch (error) {
|
||||
@@ -61,13 +59,9 @@ app.get('/api/events', async (req, res) => {
|
||||
|
||||
app.post('/api/events', async (req, res) => {
|
||||
try {
|
||||
const isConnected = await testDatabaseConnection();
|
||||
if (!isConnected) {
|
||||
throw new Error('Database connection failed');
|
||||
}
|
||||
const { title, description, date, location } = req.body;
|
||||
console.log('Creating event with data:', { title, description, date, location });
|
||||
const [result] = await pool.query(
|
||||
const result = await db.run(
|
||||
'INSERT INTO events (title, description, date, location) VALUES (?, ?, ?, ?)',
|
||||
[title, description, date, location]
|
||||
);
|
||||
@@ -87,29 +81,6 @@ app.get('*', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '../frontend/build/index.html'));
|
||||
});
|
||||
|
||||
// Initialize database tables
|
||||
async function initializeDatabase() {
|
||||
try {
|
||||
const isConnected = await testDatabaseConnection();
|
||||
if (!isConnected) {
|
||||
throw new Error('Database connection failed');
|
||||
}
|
||||
await pool.query(`
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
date DATETIME NOT NULL,
|
||||
location VARCHAR(255),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
console.log('Database initialized successfully');
|
||||
} catch (error) {
|
||||
console.error('Error initializing database:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Start server
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
|
||||
Reference in New Issue
Block a user