ProxmoxAAS-API/src/db.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

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;
#data = null;
2023-08-03 19:31:11 +00:00
constructor (path) {
try {
2023-08-03 19:31:11 +00:00
this.#path = path;
2023-07-10 06:50:29 +00:00
this.load();
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.`);
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-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-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.
*/
getGlobalConfig () {
return this.#data.global;
}
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) {
return this.#data.users[username];
}
}
2023-08-03 19:31:11 +00:00
export default LocalDB;