forked from zenflies/Itinerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
80 lines (69 loc) · 3.62 KB
/
Copy pathdb.js
File metadata and controls
80 lines (69 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const Database = require('better-sqlite3');
const path = require('path');
const DB_PATH = process.env.DB_PATH || path.join(__dirname, 'itinerate.db');
let db;
function getDB() {
if (!db) {
db = new Database(DB_PATH);
db.pragma('journal_mode = WAL'); // better concurrent read performance
db.pragma('foreign_keys = ON');
}
return db;
}
function initDB() {
const db = getDB();
// ── Users table ────────────────────────────────────────────────────────────
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL DEFAULT '',
email TEXT NOT NULL UNIQUE COLLATE NOCASE,
password_hash TEXT NOT NULL,
is_admin INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
`);
// Migrate existing databases that predate is_admin column
try { db.exec(`ALTER TABLE users ADD COLUMN is_admin INTEGER NOT NULL DEFAULT 0`); } catch (_) {}
// ── Quiz results table ─────────────────────────────────────────────────────
db.exec(`
CREATE TABLE IF NOT EXISTS quiz_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
personality_type TEXT NOT NULL,
answers_json TEXT NOT NULL, -- JSON array of {type, index}
taken_at TEXT NOT NULL DEFAULT (datetime('now'))
);
`);
// ── Saved itineraries table ────────────────────────────────────────────────
db.exec(`
CREATE TABLE IF NOT EXISTS itineraries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
destination_id TEXT NOT NULL,
destination_name TEXT NOT NULL,
personality_type TEXT NOT NULL,
itinerary_json TEXT NOT NULL, -- day-by-day itinerary
flight_json TEXT, -- selected flight object
hotel_json TEXT, -- selected hotel object
saved_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
-- one saved itinerary per user per destination
UNIQUE (user_id, destination_id)
);
`);
// Migrate itineraries columns added over time
try { db.exec(`ALTER TABLE itineraries ADD COLUMN flight_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN hotel_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN return_flight_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN destination_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN available_flights_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN available_return_flights_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN available_hotels_json TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN departure_date TEXT`); } catch (_) {}
try { db.exec(`ALTER TABLE itineraries ADD COLUMN return_date TEXT`); } catch (_) {}
console.log('✅ Database initialised at', DB_PATH);
return db;
}
module.exports = { getDB, initDB };