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 built files from builder stage
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
COPY --from=builder /app/frontend/build ./frontend/build
|
COPY --from=builder /app/frontend/build ./frontend/build
|
||||||
|
COPY --from=builder /app/database.sqlite ./database.sqlite
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|||||||
@@ -13,23 +13,7 @@ services:
|
|||||||
- /app/node_modules
|
- /app/node_modules
|
||||||
environment:
|
environment:
|
||||||
- NODE_ENV=development
|
- NODE_ENV=development
|
||||||
- DB_HOST=mysql
|
restart: unless-stopped
|
||||||
- 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
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mysql_data:
|
mysql_data:
|
||||||
@@ -11,14 +11,16 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"mysql2": "^3.6.5",
|
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.3.1"
|
"dotenv": "^16.3.1",
|
||||||
|
"sqlite3": "^5.1.7",
|
||||||
|
"sqlite": "^5.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/node": "^20.8.2",
|
"@types/node": "^20.8.2",
|
||||||
"@types/cors": "^2.8.13",
|
"@types/cors": "^2.8.13",
|
||||||
|
"@types/sqlite3": "^3.1.11",
|
||||||
"ts-node-dev": "^2.0.0",
|
"ts-node-dev": "^2.0.0",
|
||||||
"typescript": "^4.9.5",
|
"typescript": "^4.9.5",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
|
|||||||
75
src/index.ts
75
src/index.ts
@@ -1,6 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import mysql from 'mysql2/promise';
|
import sqlite3 from 'sqlite3';
|
||||||
|
import { open } from 'sqlite';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
@@ -14,26 +15,27 @@ app.use(cors());
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Database connection
|
// Database connection
|
||||||
const pool = mysql.createPool({
|
let db;
|
||||||
host: process.env.DB_HOST || 'mysql',
|
async function initializeDatabase() {
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
// Test database connection
|
|
||||||
async function testDatabaseConnection() {
|
|
||||||
try {
|
try {
|
||||||
const connection = await pool.getConnection();
|
db = await open({
|
||||||
console.log('Database connection successful');
|
filename: './database.sqlite',
|
||||||
connection.release();
|
driver: sqlite3.Database
|
||||||
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) {
|
} catch (error) {
|
||||||
console.error('Database connection failed:', error);
|
console.error('Error initializing database:', error);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,11 +45,7 @@ app.use(express.static(path.join(__dirname, '../frontend/build')));
|
|||||||
// API Routes
|
// API Routes
|
||||||
app.get('/api/events', async (req, res) => {
|
app.get('/api/events', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const isConnected = await testDatabaseConnection();
|
const rows = await db.all('SELECT * FROM events');
|
||||||
if (!isConnected) {
|
|
||||||
throw new Error('Database connection failed');
|
|
||||||
}
|
|
||||||
const [rows] = await pool.query('SELECT * FROM events');
|
|
||||||
console.log('Fetched events:', rows);
|
console.log('Fetched events:', rows);
|
||||||
res.json(rows);
|
res.json(rows);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -61,13 +59,9 @@ app.get('/api/events', async (req, res) => {
|
|||||||
|
|
||||||
app.post('/api/events', async (req, res) => {
|
app.post('/api/events', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const isConnected = await testDatabaseConnection();
|
|
||||||
if (!isConnected) {
|
|
||||||
throw new Error('Database connection failed');
|
|
||||||
}
|
|
||||||
const { title, description, date, location } = req.body;
|
const { title, description, date, location } = req.body;
|
||||||
console.log('Creating event with data:', { title, description, date, location });
|
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 (?, ?, ?, ?)',
|
'INSERT INTO events (title, description, date, location) VALUES (?, ?, ?, ?)',
|
||||||
[title, description, date, location]
|
[title, description, date, location]
|
||||||
);
|
);
|
||||||
@@ -87,29 +81,6 @@ app.get('*', (req, res) => {
|
|||||||
res.sendFile(path.join(__dirname, '../frontend/build/index.html'));
|
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
|
// Start server
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server running on port ${port}`);
|
console.log(`Server running on port ${port}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user