-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
65 lines (48 loc) · 1.41 KB
/
setup.js
File metadata and controls
65 lines (48 loc) · 1.41 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
const path = require("path");
const sqlite = require("sqlite3");
const dbFilePath = path.join(__dirname, "dev.db");
const createUsersQuery = `
CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
`;
const createCommentsQuery = `
CREATE TABLE IF NOT EXISTS comment (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT DEFAULT '' NOT NULL,
user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
`;
const createCommentsReplyQuery = `
CREATE TABLE IF NOT EXISTS reply (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT DEFAULT '' NOT NULL,
user_id INTEGER NOT NULL,
comment_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
FOREIGN KEY (comment_id) REFERENCES comment (id) ON DELETE CASCADE
);
`;
const insertUserQuery = `
INSERT INTO user
(name)
VALUES
('Bob');
`;
const db = new sqlite.Database(
dbFilePath,
sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE
);
db.serialize(() => {
db.run(createUsersQuery);
console.log("[✔️] Added user table successfully");
db.run(createCommentsQuery);
console.log("[✔️] Added comment table successfully");
db.run(createCommentsReplyQuery);
console.log("[✔️] Added reply table successfully");
});
db.close();