feat: basic db connection

This commit is contained in:
eneller
2026-03-06 00:52:27 +01:00
parent 4f206d9ad9
commit 48db9386ae
7 changed files with 100 additions and 26 deletions

17
server/compose.yml Normal file
View File

@@ -0,0 +1,17 @@
services:
postgres:
image: postgres
environment:
POSTGRES_PASSWORD: pass
ports:
- "5432:5432"
adminer:
image: adminer
restart: always
environment:
ADMINER_DEFAULT_SERVER: postgres # Automatically connect to the "postgres" service
ports:
- "8080:8080"
depends_on:
- postgres # Ensure PostgreSQL starts first

View File

@@ -8,16 +8,20 @@
"main": "../dist/out-tsc/server/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon",
"node": "nodemon",
"start": "ts-node src/index.ts",
"build": "tsc",
"serve": "node ../dist/out-tsc/server/index.js"
"serve": "node ../dist/out-tsc/server/index.js",
"setup": "docker compose up -d",
"teardown": "docker compose down",
"dev": "npm run setup && npm run node; npm run teardown"
},
"dependencies": {
"cors": "^2.8.6",
"dotenv": "^17.3.1",
"express": "^5.2.1",
"pg": "^8.19.0",
"pg": "^8.20.0",
"pg-hstore": "^2.3.4",
"sequelize": "^6.37.7"
},
"devDependencies": {

View File

@@ -1,7 +1,26 @@
const { Pool } = require("pg");
import { Sequelize } from 'sequelize';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
// Initialize Sequelize
const db = new Sequelize({
dialect: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME || 'postgres',
username: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'pass',
logging: false, // Disable logging in production
});
module.exports = pool;
// Test the connection
async function testConnection() {
try {
await db.authenticate();
console.log('✅ Database connection established.');
} catch (err) {
console.error('❌ Unable to connect to the database:', err);
process.exit(1); // Exit if DB connection fails
}
}
// Export Sequelize instance and models
export { db, testConnection };

View File

@@ -2,6 +2,7 @@ import express, { Express, Request, Response } from "express";
import cors from "cors";
import * as dotenv from "dotenv";
import transactionsRouter from './routes/transactions';
import { db, testConnection } from "./db";
dotenv.config();
@@ -17,6 +18,19 @@ app.get("/api/health", (req: Request, res: Response) => {
app.use('/api/transactions', transactionsRouter);
const PORT: number = parseInt(process.env.PORT as string) || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
// Start server after DB connection is established
async function startServer() {
await testConnection(); // Test DB connection first
// Sync models (use migrations in production!)
await db.sync({ alter: true }); // Use { force: true } to drop and recreate tables (development only!)
app.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
});
}
startServer().catch((err) => {
console.error('Failed to start server:', err);
process.exit(1);
});