Spaces:
Running
Running
File size: 9,876 Bytes
1f536d9 | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | 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; |