-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbService.js
86 lines (71 loc) · 2.33 KB
/
dbService.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 mysql = require("mysql");
const dotenv = require("dotenv");
let instance = null;
dotenv.config();
const connection = mysql.createConnection({
host: "localhost",
user: "myseard2_fsboreiUser",
password: "matrix1",
database: "myseard2_fsborei"
});
connection.connect((err) => {
if (err) console.log(err);
console.log("db " + connection.state);
});
class DbService {
static getDbServiceInstance() {
return instance ? instance : new DbService();
}
async getUserAll() {
try {
const response = await new Promise((resolve, reject) => {
const query = "SELECT * FROM tblusers;";
connection.query(query, (err, results) => {
if (err) reject(new Error(err.message));
resolve(results);
});
});
//console.log(response);
return response;
} catch (error) {
console.log(error);
}
}
async getUser(email) {
try {
const response = await new Promise((resolve, reject) => {
const query = "SELECT * FROM tblusers WHERE email = ?;";
connection.query(query, [email], (err, result) => {
if (err) {
reject(new Error(err.message));
}
else
resolve(result);
})
});
//console.log(response);
return response;
} catch (error) {
console.log(error);
}
}
async createUser(name, email, password){
try {
const insertId = await new Promise((resolve, reject) => {
const query = "INSERT INTO tblusers (name, email, password, role) VALUES(?, ?, ?, 0);"
connection.query(query, [name, email, password], (err, res) => {
if(err){
reject(new Error(err.message));
}
else
resolve(res.insertId);
});
});
console.log(insertId);
return insertId;
} catch (error) {
console.log(error);
}
}
}
module.exports = DbService;