-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
70 lines (65 loc) · 1.69 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("chatapp.db", (err) => {
if (err) {
console.error("Error opening database:", err.message);
} else {
console.log("Connected to SQLite database");
// Create Users table
db.run(
`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
)
`,
(err) => {
if (err) {
console.error("Error creating users table:", err.message);
} else {
console.log("Users table ready");
}
}
);
// Create Messages table
db.run(
`
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
chat_id INTEGER,
content TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (chat_id) REFERENCES chats(id)
)
`,
(err) => {
if (err) {
console.error("Error creating new messages table:", err.message);
} else {
console.log("New messages table ready");
}
}
);
db.run(
`
CREATE TABLE IF NOT EXISTS chats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user1_id INTEGER NOT NULL,
user2_id INTEGER NOT NULL,
FOREIGN KEY (user1_id) REFERENCES users(id),
FOREIGN KEY (user2_id) REFERENCES users(id)
)
`,
(err) => {
if (err) {
console.error("Error creating chats table:", err.message);
} else {
console.log("Chats table ready");
}
}
);
}
});
module.exports = db;