2023-02-27 01:09:49 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
|
|
|
|
filename = "localdb.json";
|
|
|
|
|
2023-02-27 01:28:01 +00:00
|
|
|
let db = {};
|
|
|
|
|
2023-02-26 08:36:27 +00:00
|
|
|
/**
|
|
|
|
* called at app startup, can be used to initialize any variables needed for database access
|
|
|
|
*/
|
2023-02-27 01:28:01 +00:00
|
|
|
function init () {
|
|
|
|
try {
|
2023-02-28 23:24:49 +00:00
|
|
|
db = JSON.parse(fs.readFileSync(filename));
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(db));
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
2023-04-19 02:42:35 +00:00
|
|
|
function load () {
|
|
|
|
db = JSON.parse(fs.readFileSync(filename));
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
2023-04-19 02:42:35 +00:00
|
|
|
function save () {
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(db));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResourceMeta () {
|
|
|
|
return db["resource-metadata"];
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
2023-02-27 01:09:49 +00:00
|
|
|
|
2023-04-19 01:03:55 +00:00
|
|
|
function getResourceUnits () {
|
2023-04-19 02:42:35 +00:00
|
|
|
return db["resource-units"];
|
2023-02-27 01:28:01 +00:00
|
|
|
}
|
2023-02-26 08:36:27 +00:00
|
|
|
|
2023-04-19 02:42:35 +00:00
|
|
|
function getUser (username) {
|
|
|
|
return db.users[username];
|
|
|
|
}
|
|
|
|
|
2023-04-19 20:36:48 +00:00
|
|
|
module.exports = {init, getResourceMeta, getResourceUnits, getUser};
|