Commit edfe1a97 authored by Family Friendly#0001's avatar Family Friendly#0001 💬

Add new file

parents
Pipeline #54404064 failed with stages
in 1 minute and 27 seconds
var fs = require("fs");
exports.database = class database {
constructor(path, options) {
//ensure path is passed
if (!path) {
throw new Error("no path defined in class creation.");
}
this.path = path;
//strict settings
if (options && options.strict && options.strict === true) {
this.strict = true;
} else {
this.strict = false;
};
//make sure the file exists
if (!fs.existsSync(path)) {
fs.writeFile(this.path, '{}', function(err) {
if (err) console.error(err);
});
}
if (options && options.settingsPath) this.settingsPath = options.settingsPath;
else this.settingsPath = "./DBsettings.json";
}
//psuedo function retrieve data
_getdata() {
var data = JSON.parse(fs.readFileSync(this.path, 'utf8'));
return data;
}
//write data
write(header, _data) {
var data = this._getdata();
if (!header || !_data) throw new Error("no data/header")
if (!data[header]) data[header] = _data;
else if (this.strict === true) {
throw new Error("(strict) can't write to existing file. use the update function instead");
} else {
if (this._lockrecord(header)) throw new Error(`record ${header} is locked`);
data[header] = _data;
}
fs.writeFileSync(this.path, JSON.stringify(data), (err) => {
throw err
})
}
update(header, _data) {
if (!header || !_data) throw new Error(`you did not pass any header or data`);
var data = this._getdata();
if (!data[header]) throw new Error(`record "${header}" does not exist in ${this.path}`);
if (this._lockrecord(header)) throw new Error(`record ${header} is locked`);
data[header] = _data;
fs.writeFileSync(this.path, JSON.stringify(data), (err) => {
throw err
})
}
get(header) {
var data = this._getdata();
if (header && data[header]) return data[header];
else return data;
}
math(header, prop, _math) {
const math = require("mathjs");
var data = this._getdata();
if (!header || !prop || !data[header] || !math) throw new Error(`no header/property/math passed${header ? ` or the record ${header} does not exist on ${this.path}`:``}`);
if (!data[header][prop]) throw new Error(`property ${prop} does not exist on ${header}`);
if (this._lockrecord(header)) throw new Error(`record ${header} is locked`);
_math = data[header][prop] + _math;
data[header][prop] = math.eval(_math);
fs.writeFileSync(this.path, JSON.stringify(data), (err) => {
throw err
});
}
updateprop(header, prop, data) {
var data = this._getdata();
if (!header || !prop || !data[header] || !data) throw new Error(`no header/property/data passed${header ? ` or the record ${header} does not exist on ${this.path}`:``}`);
if (!data[header][prop]) throw new Error(`property ${prop} does not exist on ${header}`);
if (this._lockrecord(header)) throw new Error(`record ${header} is locked`);
data[header][prop] = data;
fs.writeFileSync(this.path, JSON.stringify(data), (err) => {
throw err
});
}
deleteprop(header, prop) {
var data = this._getdata();
if (!header || !prop || !data[header] || !data) throw new Error(`no header/property/data passed${header ? ` or the record ${header} does not exist on ${this.path}`:``}`);
if (!data[header][prop]) throw new Error(`property ${prop} does not exist on ${header}`);
if (this._lockrecord(header)) throw new Error(`record ${header} is locked`);
delete data[header][prop];
fs.writeFileSync(this.path, JSON.stringify(data), (err) => {
throw err
});
}
delete(header) {
var data = this._getdata();
if (!header || !data[header]) throw new Error(`${header} cant be found on ${this.path}`);
if (this._lockrecord(header)) throw new Error(`record ${header} is locked`);
delete data[header];
fs.writeFileSync(this.path, JSON.stringify(data), (err) => {
throw err
});
}
lockrecord(header) {
if (!fs.existsSync(this.settingsPath)) {
fs.writeFile(this.settingsPath, '{}', function(err) {
if (err) console.error(err);
});
}
var data = JSON.parse(fs.readFileSync(this.settingsPath, 'utf8'));
if (!data[header]) data[header] = "locked";
else delete data[header];
fs.writeFileSync(this.settingsPath, JSON.stringify(data), (err) => {
throw err
})
}
_lockrecord(header) {
if (!fs.existsSync(this.settingsPath)) {
return undefined;
}
var data = JSON.parse(fs.readFileSync(this.settingsPath, 'utf8'));
if (data[header]) return true;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment