-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
89 lines (75 loc) · 3.47 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
const sql = require('sqlite3');
const userSeeder = require(`${appRoot}/database/seeder/userSeeder.js`);
const challengeSeeder = require(`${appRoot}/database/seeder/challengeSeeder.js`);
const commentSeeder = require(`${appRoot}/database/seeder/commentSeeder.js`);
module.exports = class Database {
constructor() {}
static db(seeder = falses){
var db = new sql.Database(__dirname + '/mabase.db3',
(err) => {
if(err){
console.log(err);
}else{
console.log("Base ouverte");
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS users(
id integer PRIMARY KEY AUTOINCREMENT,
email text UNIQUE NOT NULL,
password text NOT NULL,
pseudo text NOT NULL
)`);
db.run(`CREATE TABLE IF NOT EXISTS challenges(
id integer PRIMARY KEY AUTOINCREMENT,
content text NOT NULL,
is_realized integer DEFAULT 0,
is_visible integer DEFAULT 1,
amount_like integer DEFAULT 0,
user_id integer NOT NULL,
created_at integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS likes(
user_id integer,
challenge_id integer,
PRIMARY KEY (user_id,challenge_id),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (challenge_id) REFERENCES challenges (id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS challenge_followed(
user_id integer,
challenge_id integer,
PRIMARY KEY (user_id,challenge_id),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (challenge_id) REFERENCES challenges (id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS comments(
id integer PRIMARY KEY AUTOINCREMENT,
content text NOT NULL,
is_proof integer DEFAULT 0,
media text,
type_media text,
is_accepted integer DEFAULT 0,
user_id integer NOT NULL,
challenge_id integer NOT NULL,
created_at integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (challenge_id) REFERENCES challenges (id)
)`);
if(seeder){
userSeeder.seed((user) => {
if(user.pseudo === "Tomo"){
challengeSeeder.seed((challenge) => {
if(challenge.id < 4){
commentSeeder.seed((comment) => {
},db,user.id,challenge.id);
}
},db,user.id);
}
},db);
}
});
}
});
return db;
}
};