-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
70 lines (64 loc) · 1.46 KB
/
database.js
File metadata and controls
70 lines (64 loc) · 1.46 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
66
67
68
69
70
const sqlite3 = require("sqlite3").verbose();
const file = "users-database";
const db = new sqlite3.Database(file);
async function checkIfUserExists(name, id) {
return new Promise((resolve, reject) => {
db.all(
"SELECT * FROM Users WHERE name = ? AND userID = ?",
name,
id,
(error, rows) => {
if (error) {
reject(error);
} else {
resolve(rows.length > 0);
}
}
);
});
}
async function insertUser(id, role, name, pass) {
console.log(id, role, name, pass);
return new Promise((resolve, reject) => {
db.run(
"INSERT INTO Users (userID, role, name, password) VALUES (?, ?, ?, ?)",
id,
role,
name,
pass,
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
}
);
});
}
async function getPass(id) {
console.log(id);
return new Promise((resolve, reject) => {
db.all("SELECT * FROM Users WHERE userID = ?", id, (error, rows) => {
if (error) {
reject(error);
} else {
console.log(rows);
resolve(rows[0]);
}
});
});
}
async function getAllUsers() {
return new Promise((resolve, reject) => {
db.all("SELECT * FROM Users", (error, rows) => {
if (error) {
reject(error);
} else {
console.log(rows);
resolve(rows);
}
});
});
}
module.exports = { getPass, insertUser, checkIfUserExists, getAllUsers };