-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries-pool.js
46 lines (45 loc) · 1.33 KB
/
queries-pool.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
const e = require("express");
const mysql = require("mysql");
const insert = (connection, data, callback) => {
let insertQuery = "INSERT INTO users (id, name) VALUES (?, ?)";
let query = mysql.format(insertQuery, [data.id, data.name]);
connection.query(query, (err, result) => {
if (err) throw err;
callback(result);
connection.end();
});
};
const read = (connection, callback) => {
let insertQuery = "SELECT * FROM users";
connection.query(insertQuery, (err, result) => {
if (err) throw err;
callback(result);
connection.end();
});
};
const update = (connection, data, callback) => {
const randomLetters = Math.random().toString(36).substring(5);
const newName = `${randomLetters}`;
let insertQuery = "UPDATE users SET name = ? WHERE id = ?";
let query = mysql.format(insertQuery, [newName, data.id]);
connection.query(query, (err, result) => {
if (err) throw err;
callback(result);
connection.end();
});
};
const remove = (connection, data, callback) => {
const insertQuery = "DELETE FROM users WHERE id = ?";
const query = mysql.format(insertQuery, [data.id]);
connection.query(query, (err, result) => {
if (err) throw err;
callback(result);
connection.end();
});
};
module.exports = {
insertPool: insert,
readPool: read,
updatePool: update,
removePool: remove,
};