-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
46 lines (40 loc) · 1.3 KB
/
Copy pathdatabase.js
File metadata and controls
46 lines (40 loc) · 1.3 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
// database.js
import mysql from 'mysql2/promise';
import dotenv from 'dotenv';
dotenv.config();
const pool = mysql.createPool({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
port: Number(process.env.MYSQL_PORT) || 3306,
database: process.env.MYSQL_DATABASE
});
// Get all employees
export async function getEmployees() {
const [rows] = await pool.query("SELECT * FROM employee");
return rows;
}
// Get employee by ID
export async function getEmployee(id) {
const [rows] = await pool.query("SELECT * FROM employee WHERE id = ?", [id]);
return rows;
}
// Create employee
export async function createEmployee(employee_fullname, employee_type) {
const [result] = await pool.query(
"INSERT INTO employee (employee_fullname, employee_type) VALUES (?, ?)",
[employee_fullname, employee_type]
);
return { id: result.insertId, employee_fullname, employee_type };
}
// Update employee
export async function updateEmployee(id, employee_fullname, employee_type) {
return await pool.query(
"UPDATE employee SET employee_fullname = ?, employee_type = ? WHERE id = ?",
[employee_fullname, employee_type, id]
);
}
// Delete employee
export async function deleteEmployee(id) {
return await pool.query("DELETE FROM employee WHERE id = ?", [id]);
}