-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.js
More file actions
49 lines (45 loc) · 1018 Bytes
/
MySQL.js
File metadata and controls
49 lines (45 loc) · 1018 Bytes
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
const mysql = require("mysql2/promise");
const pool = mysql.createPool({
host: "localhost",
user: "root",
password: "admin",
database: "authentication",
});
const connection = async function () {
try {
pool.getConnection((err) => {
if (err) throw err;
console.log("connected");
});
} catch (err) {
console.log(err);
}
};
connection()
.then(() => {
console.log("Connected to DB server");
})
.catch((err) => {
console.log(err);
});
const users = {
async createUser(username, password) {
const [row] = await pool.query(
"insert into users (username, password) values (?, ?)",
[username, password]
);
return row.insertId;
},
async getUser(username) {
const [row] = await pool.query(
"select * from users where username = ?",
username
);
return row[0];
},
async getUserById(id) {
const [row] = await pool.query("select * from users where id = ?", id);
return row[0];
},
};
module.exports = users;