53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
const Database = require('better-sqlite3');
|
|
const path = require('path');
|
|
|
|
const dbPath = path.resolve(__dirname, 'eventy.db');
|
|
const db = new Database(dbPath);
|
|
|
|
// Enable WAL mode for better concurrency
|
|
db.pragma('journal_mode = WAL');
|
|
|
|
function initDb() {
|
|
const schema = `
|
|
CREATE TABLE IF NOT EXISTS events (
|
|
id TEXT PRIMARY KEY,
|
|
name TEXT NOT NULL,
|
|
description TEXT,
|
|
start_date TEXT NOT NULL,
|
|
end_date TEXT NOT NULL,
|
|
created_at TEXT DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS participants (
|
|
id TEXT PRIMARY KEY,
|
|
event_id TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
created_at TEXT DEFAULT (datetime('now')),
|
|
FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS votes (
|
|
id TEXT PRIMARY KEY,
|
|
participant_id TEXT NOT NULL,
|
|
date TEXT NOT NULL,
|
|
status TEXT CHECK(status IN ('available', 'unavailable', 'maybe')) NOT NULL,
|
|
FOREIGN KEY (participant_id) REFERENCES participants(id) ON DELETE CASCADE,
|
|
UNIQUE(participant_id, date)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS event_dates (
|
|
event_id TEXT NOT NULL,
|
|
date TEXT NOT NULL,
|
|
PRIMARY KEY (event_id, date),
|
|
FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
|
|
);
|
|
`;
|
|
db.exec(schema);
|
|
console.log('Database initialized');
|
|
}
|
|
|
|
module.exports = {
|
|
db,
|
|
initDb
|
|
};
|