forked from Ruslan-smp-ai/newspaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
98 lines (95 loc) · 2.35 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var sqlite3 = require("sqlite3").verbose();
var DBSOURCE = "./db.sqlite";
var db = new sqlite3.Database(DBSOURCE, (err) => {
if (err) {
// Cannot open database
console.error(err.message);
throw err;
} else {
console.log("Connected to the SQLite database.");
db.run(
`CREATE TABLE category (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name text
)`,
(err) => {
if (err) {
console.log("Table category is already created");
} else {
console.log("Table category is created");
db.run(
`INSERT INTO category (name) VALUES ('News'),
('Politics'),
('Sport'),
('Culture'),
('Covid');`,
(err) => {
if (err) {
console.log("Inserts is already created " + err);
} else {
console.log("Inserts is created");
}
}
);
}
}
);
db.run(
`CREATE TABLE post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title text,
description text,
text text,
image text,
date text,
userId integer,
categoryId integer,
FOREIGN KEY (userId) REFERENCES user(id)
FOREIGN KEY (categoryId) REFERENCES category(id)
)`,
(err) => {
if (err) {
console.log("Table post id already created:" + err.message);
} else {
console.log("Table post is created");
}
}
);
db.run(
`CREATE TABLE comment (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userId integer,
comment text,
idPost integer,
FOREIGN KEY (idPost) REFERENCES post(id)
FOREIGN KEY (userId) REFERENCES user(id)
)`,
(err) => {
if (err) {
console.log("Table comment id already created:" + err.message);
} else {
console.log("Table comment is created");
}
}
);
db.run(
`CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name text,
email text UNIQUE,
image text,
password text,
failed_logins INTEGER,
CONSTRAINT email_unique UNIQUE (email)
)`,
(err) => {
if (err) {
console.log("Table user is already created");
} else {
console.log("Table user is created");
}
}
);
}
});
module.exports = db;