-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdatabase.js
51 lines (43 loc) · 1.05 KB
/
database.js
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
import Database from "better-sqlite3";
let db = null;
export function initDb() {
if (db) {
return db;
}
db = new Database("events.db", { verbose: console.log });
// Create users table
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
)
`);
// Create events table
db.exec(`
CREATE TABLE IF NOT EXISTS events (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
address TEXT,
date TEXT NOT NULL,
image TEXT NOT NULL,
user_id TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id)
)
`);
// Create registrations table
db.exec(`
CREATE TABLE IF NOT EXISTS registrations (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
event_id TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (event_id) REFERENCES events (id)
)
`);
return db;
}
export function getDb() {
return initDb(); // This will either return existing db or initialize a new one
}