feat: initialize database and authentication routes in backend

This commit is contained in:
LaurensBartelds committed 2026-04-21 22:51:22 +02:00
1 parent be69db04a5
commit eb013d4978
22 files changed
+1808 -8

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import type Database from 'better-sqlite3'
export function runMigrations(db: Database.Database): void {
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
display_name TEXT NOT NULL,
email_verified INTEGER DEFAULT 0,
verify_token TEXT,
verify_expires INTEGER,
reset_token TEXT,
reset_expires INTEGER,
created_at INTEGER DEFAULT (unixepoch()),
updated_at INTEGER DEFAULT (unixepoch())
);
CREATE TABLE IF NOT EXISTS saved_exams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
subtitle TEXT,
exam_json TEXT NOT NULL,
created_at INTEGER DEFAULT (unixepoch()),
updated_at INTEGER DEFAULT (unixepoch())
);
CREATE TABLE IF NOT EXISTS exam_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
exam_id INTEGER REFERENCES saved_exams(id) ON DELETE SET NULL,
title TEXT NOT NULL,
score_points REAL NOT NULL,
total_points REAL NOT NULL,
correct_count INTEGER NOT NULL,
graded_count INTEGER NOT NULL,
passing_score REAL DEFAULT 60,
elapsed_ms INTEGER NOT NULL,
state_json TEXT NOT NULL,
exam_json TEXT NOT NULL,
completed_at INTEGER DEFAULT (unixepoch())
);
CREATE INDEX IF NOT EXISTS idx_saved_exams_user ON saved_exams(user_id);
CREATE INDEX IF NOT EXISTS idx_exam_results_user ON exam_results(user_id);
`)
}