feat: bootstrap backend API, schema and forgejo task issues
This commit is contained in:
parent
77e837cc25
commit
09ea388190
15 changed files with 1557 additions and 0 deletions
15
backend/src/db/connection.js
Normal file
15
backend/src/db/connection.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import mysql from 'mysql2/promise';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
port: Number(process.env.DB_PORT || 3306),
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0
|
||||
});
|
||||
29
backend/src/db/init.js
Normal file
29
backend/src/db/init.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { pool } from './connection.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const run = async () => {
|
||||
const schemaPath = path.resolve(__dirname, '../../sql/schema.sql');
|
||||
const sql = await fs.readFile(schemaPath, 'utf8');
|
||||
const statements = sql
|
||||
.split(';')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
for (const statement of statements) {
|
||||
await pool.query(statement);
|
||||
}
|
||||
|
||||
console.log(`Applied ${statements.length} SQL statements.`);
|
||||
await pool.end();
|
||||
};
|
||||
|
||||
run().catch(async (err) => {
|
||||
console.error('Database init failed:', err.message);
|
||||
await pool.end();
|
||||
process.exit(1);
|
||||
});
|
||||
20
backend/src/db/seed.js
Normal file
20
backend/src/db/seed.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import bcrypt from 'bcryptjs';
|
||||
import { pool } from './connection.js';
|
||||
|
||||
const run = async () => {
|
||||
const hash = await bcrypt.hash('changeme123', 12);
|
||||
await pool.query(
|
||||
`INSERT INTO users (email, password_hash, display_name) VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE display_name = VALUES(display_name)`,
|
||||
['demo@helpyourneighbour.ch', hash, 'Demo User']
|
||||
);
|
||||
|
||||
console.log('Seed complete.');
|
||||
await pool.end();
|
||||
};
|
||||
|
||||
run().catch(async (err) => {
|
||||
console.error('Seed failed:', err.message);
|
||||
await pool.end();
|
||||
process.exit(1);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue