-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdb.ts
More file actions
68 lines (61 loc) · 2.03 KB
/
db.ts
File metadata and controls
68 lines (61 loc) · 2.03 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
export type Employee = {
employee_id: string;
name: string;
position: string;
image_url: string;
join_date: string;
location_id: number;
department_id: number;
location_name: string;
department_name: string;
image_file?: File;
};
export const findAllEmployees = async (db: D1Database) => {
const query = `
SELECT employees.*, locations.location_name, departments.department_name
FROM employees
JOIN locations ON employees.location_id = locations.location_id
JOIN departments ON employees.department_id = departments.department_id
`;
const { results } = await db.prepare(query).all();
const employees = results;
return employees;
};
export const findEmployeeById = async (db: D1Database, id: string) => {
const query = `
SELECT employees.*, locations.location_name, departments.department_name
FROM employees
JOIN locations ON employees.location_id = locations.location_id
JOIN departments ON employees.department_id = departments.department_id
WHERE employee_id = ?`;
const employee = await db.prepare(query).bind(id).first();
return employee;
};
export const createEmployee = async (
db: D1Database,
employee: Employee
) => {
const query = `
INSERT INTO employees (name, position, join_date, image_url, department_id, location_id)
VALUES (?, ?, ?, ?, ?, ?)`;
const results = await db
.prepare(query)
.bind(employee.name, employee.position, employee.join_date, employee.image_url, employee.department_id, employee.location_id)
.run();
const employees = results;
return employees;
};
export const findAllLocations = async (db: D1Database) => {
const { results } = await db
.prepare("SELECT * FROM locations ORDER BY location_name ASC")
.all();
const locations = results;
return locations;
};
export const findAllDepartments = async (db: D1Database) => {
const { results } = await db
.prepare("SELECT * FROM departments ORDER BY department_name ASC")
.all();
const locations = results;
return locations;
};