-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueries.js
93 lines (83 loc) · 2.66 KB
/
queries.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
87
88
89
90
91
92
93
// Dependency
const mysql = require(`mysql`);
const cTable = require(`console.table`);
// DB connection
const connection = mysql.createConnection({
host: `localhost`,
port: 3306,
user: `root`,
password: `root`,
database: `employees_db`
});
// Connect
connection.connect((err) => {
if (err) throw err;
});
// called by callDep() in server.js, returns array of Department names
const queryDep = () => {
connection.query(`SELECT * FROM department`, (err, res) => {
if (err) throw err;
const table = cTable.getTable(res);
console.log(table);
})
};
// called by viewDep, returns table of employees by department
const queryEmpByDep = (id) => {
connection.query(`SELECT employee.id, employee.first_name, employee.last_name, role.title, concat(manager.first_name, " ", manager.last_name) AS manager FROM employee INNER JOIN employee AS manager ON employee.manager_id = manager.id INNER JOIN role ON employee.role_id = role.id INNER JOIN department ON department.id = role.department_id WHERE department.id = ${id}`,
(err, res) => {
if (err) throw err;
const table = cTable.getTable(res);
console.log(table);
})
}
// called by callEmp, returns table
const queryEmp = () => {
connection.query(`SELECT * FROM employee`, (err, res) => {
if (err) throw err;
const table = cTable.getTable(res);
console.log(table);
})
};
// called by addEmp, returns table
const queryRole = () => {
connection.query(`SELECT id, title FROM role`, (err, res) => {
if (err) throw err;
const table = cTable.getTable(res);
console.log(table);
})
};
// sends addEmp() info to MySQL Database
const empSQL = (newEmp) => {
connection.query(`INSERT INTO employee SET ?`, newEmp, (err, res) => {
if (err) throw err;
})
}
// sends addDep() info to MySQL Database
const depSQL = (newDep) => {
connection.query(`INSERT INTO department SET ?`, newDep, (err, res) => {
if (err) throw err;
})
}
// sends addRole() info to MySQL Database
const roleSQL = (newRole) => {
connection.query(`INSERT INTO role SET ?`, newRole, (err, res) => {
if (err) throw err;
})
}
const updateRoleSQL = (updateRole) => {
connection.query(`UPDATE employee SET role_id = ? WHERE id = ?`, updateRole, (err, res) => {
if (err) throw err;
console.log(`Rows affected`, res.affectedRows);
})
}
// Export as Obj
module.exports = {
queryDep: queryDep,
queryEmpByDep: queryEmpByDep,
queryEmp: queryEmp,
queryRole: queryRole,
empSQL: empSQL,
depSQL: depSQL,
roleSQL: roleSQL,
updateRoleSQL: updateRoleSQL
};