Spaces:
Running
Running
Download database.js from ThePCRunners/hr-management-pro: direct link, hf CLI and curl.
- Browser
- Download file 9.88 kB
-
https://huggingface.co/spaces/ThePCRunners/hr-management-pro/resolve/main/database.js
- Command line
-
hf download hf://spaces/ThePCRunners/hr-management-pro/database.js
-
curl -L -o database.js https://huggingface.co/spaces/ThePCRunners/hr-management-pro/resolve/main/database.js
9.88 kB
| const Database = require('better-sqlite3'); | |
| const Store = require('electron-store'); | |
| const path = require('path'); | |
| const { app } = require('electron'); | |
| class HRDatabase { | |
| constructor() { | |
| this.store = new Store(); | |
| this.db = null; | |
| } | |
| async init() { | |
| try { | |
| let dbPath = this.store.get('dbPath'); | |
| if (!dbPath) { | |
| // First run - ask user for database location | |
| const { canceled, filePath } = await dialog.showSaveDialog({ | |
| title: 'ืืืจ ืืืงืื ืขืืืจ ืืกื ืื ืชืื ืื', | |
| defaultPath: 'app_data.sqlite', | |
| filters: [{ name: 'SQLite Database', extensions: ['sqlite'] }] | |
| }); | |
| if (canceled) { | |
| throw new Error('User cancelled database selection'); | |
| } | |
| dbPath = filePath; | |
| this.store.set('dbPath', dbPath); | |
| } | |
| this.db = new Database(dbPath); | |
| // Enable WAL mode for concurrent access | |
| this.db.pragma('journal_mode = WAL'); | |
| // Create tables if they don't exist | |
| this.createTables(); | |
| return { success: true, path: dbPath }; | |
| } catch (error) { | |
| console.error('Database initialization error:', error); | |
| return { success: false, error: error.message }; | |
| } | |
| } | |
| createTables() { | |
| // Units table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS units ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| name TEXT NOT NULL | |
| ) | |
| `); | |
| // Complexes table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS complexes ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| name TEXT NOT NULL, | |
| unit_id INTEGER, | |
| FOREIGN KEY (unit_id) REFERENCES units (id) | |
| ) | |
| `); | |
| // Desks table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS desks ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| name TEXT NOT NULL, | |
| complex_id INTEGER, | |
| FOREIGN KEY (complex_id) REFERENCES complexes (id) | |
| ) | |
| `); | |
| // Persons table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS persons ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| fullName TEXT NOT NULL, | |
| role TEXT NOT NULL, | |
| parent_id INTEGER NOT NULL, | |
| parent_type TEXT NOT NULL CHECK(parent_type IN ('unit', 'complex', 'desk')), | |
| status TEXT NOT NULL CHECK(status IN ('ืคืขืื', 'ืื ืคืขืื', 'ืืืืคืฉื')) | |
| ) | |
| `); | |
| // Trainings table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS trainings ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| person_id INTEGER NOT NULL, | |
| trainingName TEXT NOT NULL, | |
| dateCompleted DATE, | |
| expiryDate DATE, | |
| FOREIGN KEY (person_id) REFERENCES persons (id) | |
| ) | |
| `); | |
| // Drills table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS drills ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| person_id INTEGER NOT NULL, | |
| drillName TEXT NOT NULL, | |
| datePerformed DATE, | |
| score INTEGER, | |
| FOREIGN KEY (person_id) REFERENCES persons (id) | |
| ) | |
| `); | |
| // Calendar events table | |
| this.db.exec(` | |
| CREATE TABLE IF NOT EXISTS calendar_events ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| person_id INTEGER NOT NULL, | |
| title TEXT NOT NULL, | |
| startTime DATETIME NOT NULL, | |
| endTime DATETIME NOT NULL, | |
| type TEXT NOT NULL CHECK(type IN ('ืืฉืืจืช', 'ืืืฉืจื', 'ืืืืื', 'ืืืคืฉื', 'ืืืจ')), | |
| notes TEXT, | |
| FOREIGN KEY (person_id) REFERENCES persons (id) | |
| ) | |
| `); | |
| } | |
| // Unit operations | |
| getUnits() { | |
| return this.db.prepare('SELECT * FROM units').all(); | |
| } | |
| addUnit(unit) { | |
| const stmt = this.db.prepare('INSERT INTO units (name) VALUES (?)'); | |
| const info = stmt.run(unit.name); | |
| return { id: info.lastInsertRowid, ...unit }; | |
| } | |
| updateUnit(id, unit) { | |
| const stmt = this.db.prepare('UPDATE units SET name = ? WHERE id = ?'); | |
| stmt.run(unit.name, id); | |
| return { id, ...unit }; | |
| } | |
| deleteUnit(id) { | |
| const stmt = this.db.prepare('DELETE FROM units WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| // Complex operations | |
| getComplexes(unitId) { | |
| const stmt = this.db.prepare('SELECT * FROM complexes WHERE unit_id = ?'); | |
| return stmt.all(unitId); | |
| } | |
| addComplex(complex) { | |
| const stmt = this.db.prepare('INSERT INTO complexes (name, unit_id) VALUES (?, ?)'); | |
| const info = stmt.run(complex.name, complex.unit_id); | |
| return { id: info.lastInsertRowid, ...complex }; | |
| } | |
| updateComplex(id, complex) { | |
| const stmt = this.db.prepare('UPDATE complexes SET name = ?, unit_id = ? WHERE id = ?'); | |
| stmt.run(complex.name, complex.unit_id, id); | |
| return { id, ...complex }; | |
| } | |
| deleteComplex(id) { | |
| const stmt = this.db.prepare('DELETE FROM complexes WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| // Desk operations | |
| getDesks(complexId) { | |
| const stmt = this.db.prepare('SELECT * FROM desks WHERE complex_id = ?'); | |
| return stmt.all(complexId); | |
| } | |
| addDesk(desk) { | |
| const stmt = this.db.prepare('INSERT INTO desks (name, complex_id) VALUES (?, ?)'); | |
| const info = stmt.run(desk.name, desk.complex_id); | |
| return { id: info.lastInsertRowid, ...desk }; | |
| } | |
| updateDesk(id, desk) { | |
| const stmt = this.db.prepare('UPDATE desks SET name = ?, complex_id = ? WHERE id = ?'); | |
| stmt.run(desk.name, desk.complex_id, id); | |
| return { id, ...desk }; | |
| } | |
| deleteDesk(id) { | |
| const stmt = this.db.prepare('DELETE FROM desks WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| // Person operations | |
| getPersons(parentId, parentType) { | |
| const stmt = this.db.prepare('SELECT * FROM persons WHERE parent_id = ? AND parent_type = ?'); | |
| return stmt.all(parentId, parentType); | |
| } | |
| addPerson(person) { | |
| const stmt = this.db.prepare( | |
| 'INSERT INTO persons (fullName, role, parent_id, parent_type, status) VALUES (?, ?, ?, ?, ?)' | |
| ); | |
| const info = stmt.run( | |
| person.fullName, | |
| person.role, | |
| person.parent_id, | |
| person.parent_type, | |
| person.status | |
| ); | |
| return { id: info.lastInsertRowid, ...person }; | |
| } | |
| updatePerson(id, person) { | |
| const stmt = this.db.prepare( | |
| 'UPDATE persons SET fullName = ?, role = ?, parent_id = ?, parent_type = ?, status = ? WHERE id = ?' | |
| ); | |
| stmt.run( | |
| person.fullName, | |
| person.role, | |
| person.parent_id, | |
| person.parent_type, | |
| person.status, | |
| id | |
| ); | |
| return { id, ...person }; | |
| } | |
| deletePerson(id) { | |
| const stmt = this.db.prepare('DELETE FROM persons WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| getPersonDetails(id) { | |
| const stmt = this.db.prepare('SELECT * FROM persons WHERE id = ?'); | |
| return stmt.get(id); | |
| } | |
| // Training operations | |
| getTrainings(personId) { | |
| const stmt = this.db.prepare('SELECT * FROM trainings WHERE person_id = ?'); | |
| return stmt.all(personId); | |
| } | |
| addTraining(training) { | |
| const stmt = this.db.prepare( | |
| 'INSERT INTO trainings (person_id, trainingName, dateCompleted, expiryDate) VALUES (?, ?, ?, ?)' | |
| ); | |
| const info = stmt.run( | |
| training.person_id, | |
| training.trainingName, | |
| training.dateCompleted, | |
| training.expiryDate | |
| ); | |
| return { id: info.lastInsertRowid, ...training }; | |
| } | |
| updateTraining(id, training) { | |
| const stmt = this.db.prepare( | |
| 'UPDATE trainings SET trainingName = ?, dateCompleted = ?, expiryDate = ? WHERE id = ?' | |
| ); | |
| stmt.run( | |
| training.trainingName, | |
| training.dateCompleted, | |
| training.expiryDate, | |
| id | |
| ); | |
| return { id, ...training }; | |
| } | |
| deleteTraining(id) { | |
| const stmt = this.db.prepare('DELETE FROM trainings WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| // Drill operations | |
| getDrills(personId) { | |
| const stmt = this.db.prepare('SELECT * FROM drills WHERE person_id = ?'); | |
| return stmt.all(personId); | |
| } | |
| addDrill(drill) { | |
| const stmt = this.db.prepare( | |
| 'INSERT INTO drills (person_id, drillName, datePerformed, score) VALUES (?, ?, ?, ?)' | |
| ); | |
| const info = stmt.run( | |
| drill.person_id, | |
| drill.drillName, | |
| drill.datePerformed, | |
| drill.score | |
| ); | |
| return { id: info.lastInsertRowid, ...drill }; | |
| } | |
| updateDrill(id, drill) { | |
| const stmt = this.db.prepare( | |
| 'UPDATE drills SET drillName = ?, datePerformed = ?, score = ? WHERE id = ?' | |
| ); | |
| stmt.run( | |
| drill.drillName, | |
| drill.datePerformed, | |
| drill.score, | |
| id | |
| ); | |
| return { id, ...drill }; | |
| } | |
| deleteDrill(id) { | |
| const stmt = this.db.prepare('DELETE FROM drills WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| // Calendar operations | |
| getCalendarEvents() { | |
| return this.db.prepare('SELECT * FROM calendar_events').all(); | |
| } | |
| addCalendarEvent(calendarEvent) { | |
| const stmt = this.db.prepare( | |
| 'INSERT INTO calendar_events (person_id, title, startTime, endTime, type, notes) VALUES (?, ?, ?, ?, ?, ?)' | |
| ); | |
| const info = stmt.run( | |
| calendarEvent.person_id, | |
| calendarEvent.title, | |
| calendarEvent.startTime, | |
| calendarEvent.endTime, | |
| calendarEvent.type, | |
| calendarEvent.notes | |
| ); | |
| return { id: info.lastInsertRowid, ...calendarEvent }; | |
| } | |
| updateCalendarEvent(id, calendarEvent) { | |
| const stmt = this.db.prepare( | |
| 'UPDATE calendar_events SET title = ?, startTime = ?, endTime = ?, type = ?, notes = ? WHERE id = ?' | |
| ); | |
| stmt.run( | |
| calendarEvent.title, | |
| calendarEvent.startTime, | |
| calendarEvent.endTime, | |
| calendarEvent.type, | |
| calendarEvent.notes, | |
| id | |
| ); | |
| return { id, ...calendarEvent }; | |
| } | |
| deleteCalendarEvent(id) { | |
| const stmt = this.db.prepare('DELETE FROM calendar_events WHERE id = ?'); | |
| stmt.run(id); | |
| return id; | |
| } | |
| } | |
| module.exports = HRDatabase; |