2023-06-09 03:58:38 +00:00
|
|
|
import { readFileSync, writeFileSync } from "fs";
|
|
|
|
import { exit } from "process";
|
|
|
|
|
2023-06-29 21:35:19 +00:00
|
|
|
class LocalDB {
|
2023-08-03 19:31:11 +00:00
|
|
|
#path = null;
|
2023-06-09 03:58:38 +00:00
|
|
|
#data = null;
|
2023-08-03 19:31:11 +00:00
|
|
|
constructor (path) {
|
2023-06-09 03:58:38 +00:00
|
|
|
try {
|
2023-08-03 19:31:11 +00:00
|
|
|
this.#path = path;
|
2023-07-10 06:50:29 +00:00
|
|
|
this.load();
|
2023-07-27 20:10:46 +00:00
|
|
|
this.pveAPI = this.getGlobalConfig().application.pveAPI;
|
|
|
|
this.pveAPIToken = this.getGlobalConfig().application.pveAPIToken;
|
|
|
|
this.listenPort = this.getGlobalConfig().application.listenPort;
|
|
|
|
this.hostname = this.getGlobalConfig().application.hostname;
|
|
|
|
this.domain = this.getGlobalConfig().application.domain;
|
2023-06-29 22:09:57 +00:00
|
|
|
}
|
|
|
|
catch {
|
2023-08-03 19:31:11 +00:00
|
|
|
console.log(`Error: ${path} was not found. Please follow the directions in the README to initialize localdb.json.`);
|
2023-06-09 03:58:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
2023-07-10 06:50:29 +00:00
|
|
|
/**
|
|
|
|
* Load db from local file system. Reads from file path store in filename.
|
|
|
|
*/
|
|
|
|
load () {
|
2023-08-03 19:31:11 +00:00
|
|
|
this.#data = JSON.parse(readFileSync(this.#path));
|
2023-06-09 03:58:38 +00:00
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
2023-07-10 06:50:29 +00:00
|
|
|
/**
|
|
|
|
* Save db to local file system. Saves to file path stored in filename.
|
|
|
|
*/
|
|
|
|
save () {
|
2023-08-03 19:31:11 +00:00
|
|
|
writeFileSync(this.#path, JSON.stringify(this.#data));
|
2023-06-09 03:58:38 +00:00
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
2023-07-10 06:50:29 +00:00
|
|
|
/**
|
|
|
|
* Gets the global config object from db.
|
|
|
|
* @returns {Object} global config data.
|
|
|
|
*/
|
2023-07-05 23:14:45 +00:00
|
|
|
getGlobalConfig () {
|
|
|
|
return this.#data.global;
|
2023-06-09 03:58:38 +00:00
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
2023-07-10 06:50:29 +00:00
|
|
|
/**
|
|
|
|
* Gets a specific user's config from db.
|
|
|
|
* @param {string} username of user to get config.
|
|
|
|
* @returns {Object} specific user config data.
|
|
|
|
*/
|
2023-06-29 21:35:19 +00:00
|
|
|
getUserConfig (username) {
|
2023-07-05 23:14:45 +00:00
|
|
|
return this.#data.users[username];
|
2023-06-09 03:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:31:11 +00:00
|
|
|
export default LocalDB;
|