fastnukleon
Version:
simple - json based database module
300 lines (280 loc) • 9.35 kB
JavaScript
const fs = require("node:fs"); //fs safeload- provided
const load = (file) => JSON.parse(fs.readFileSync(file, "utf-8"));
const write = (file, data) =>
fs.writeFileSync(file, JSON.stringify(data, null, 4));
const extension = (filePath) => {
let parts = filePath.split(".");
return parts[parts.length - 1];
};
let backup; //load in backup * safe processing --- discord.gg/javascript
const Backupadd = (data) => {
if (backup !== undefined) {
fs.writeFileSync(backup, JSON.stringify(data, null, 4));
}
};
class Database {
constructor(file) {
this.file = file || "database.json";
if (this.file === "database.json") {
try {
load(this.file);
} catch {
write(this.file, {});
}
} else {
if (!this.file.includes("./")) this.file = "./" + this.file;
if (extension(this.file) !== "json")
throw Error("[err] the database file must end with .json");
try {
load(this.file);
} catch {
write(this.file, {});
}
}
}
setBackup(filePath) {
if (!filePath)
throw Error("[err] a .json file must be mentioned for backups"); //err msg, tr/s4½
if (extension(filePath) !== "json")
throw Error("[err] the backup file must end with .json");
if (!filePath.includes("./")) filePath = "./" + filePath;
backup = filePath;
try {
load(backup);
} catch {
write(backup, {});
}
return; //æ@€al-discord.gg/javascript
}
loadBackup() {
if (backup === undefined)
throw Error("[err] can't find a backup || a file to load");
write(this.file, load(backup));
return;
}
set(data, value) {
if (data == null) throw Error("[err] no data to set");
if (value == null) throw Error("[err] no value to set");
let fileData = load(this.file);
fileData[data] = value;
write(this.file, fileData);
Backupadd(fileData);
return;
} //€€₺€ißæ err, set for ❤
remove(data) {
if (data == null) throw Error("[err] no value to remove");
let fileData = load(this.file);
if (!fileData[data])
throw Error(
"[err] mentioned data isn't in directory or cannot be reached"
);
fileData[data] = undefined;
write(this.file, fileData);
Backupadd(fileData);
return;
}
add(data, value) {
if (data == null) throw Error("[err] no data to add");
if (value == null) throw Error("[err] no value to add");
if (typeof value == "number") {
let fileData = load(this.file);
if (fileData[data] === undefined) return this.set(data, value);
if (isNaN(fileData[data])) return this.set(data, value);
fileData[data] = fileData[data] + value;
write(this.file, fileData);
Backupadd(fileData);
return;
} else {
let fileData = load(this.file);
if (fileData[data] === undefined) return this.set(data, value);
if (isNaN(fileData[data])) return this.set(data, value);
fileData[data] = fileData[data] + value;
write(this.file, fileData);
Backupadd(fileData);
return;
}
}
//made with ❤ by pythonic
subtract(data, value) {
if (data == null) throw Error("[err] No data to subtract");
if (value == null) throw Error("[err] No value to subtract");
if (typeof value !== "number")
throw Error(
`[err] The value to substract must be a number, received type: ${typeof value}`
);
let fileData = load(this.file);
if (file[data] === undefined) return this.set(data, value);
if (isNaN(file[data])) return this.set(data, value);
fileData[data] = fileData[data] - value;
write(this.file, fileData);
Backupadd(fileData);
return;
}
//🪐 this is a nice planet.
deleteEach(data) {
if (data == null) throw Error("[err] No data to deleteEach");
let fileData = load(this.file);
let item = Object.keys(fileData);
if (item === "") throw Error(nothingToDeleteeach);
item = item.filter((Data) => Data.includes(data));
item.forEach((Data) => {
this.remove(Data);
});
return;
}
// 🛒 marketede giderim ben sanane
push(array, value) {
if (array == null) throw Error("[err] No array to push");
if (value == null) throw Error("[err] No value to push to the array");
let fileData = load(this.file);
if (fileData[array] && Array.isArray(fileData[array])) {
fileData[array].push(value);
write(this.file, fileData);
} else if (!fileData[array]) {
this.set(array, [value]);
}
return;
}
//db.map shall b€ inclU€d for v2.
delete(array, index) {
if (array == null) throw Error("[err] No array to index/value delete");
if (index === undefined)
throw Error("[err] No index/value to delete from the array");
let fileData = load(this.file);
if (!fileData[array] && !Array.isArray(fileData[array]))
throw Error(
"[err] The array to index/value delete dosen't exist or it's not array"
);
if (typeof index === "number") {
fileData[array].splice(index, 1);
write(this.file, fileData);
} else if (isNaN(index)) {
if (fileData[array].includes(index)) {
fileData[array].splice(fileData[array].indexOf(index), 1);
write(this.file, fileData);
} else {
throw Error(
"[err] Unable to find a value with the provided index/value to delete"
);
}
}
return;
}
//<html> forKey: "all-destroy" </html>
deleteKey(object, key) {
if (object == null) throw Error("[err] No object to key delete");
if (key == null) throw Error("[err] No key to delete from the object");
let fileData = load(this.file);
if (!fileData[object])
throw Error(
"[err] The object to delete key dosen't exist in the database"
);
if (typeof fileData[object] !== "object")
throw Error(
"[err] The provided object to key delete is not an object in the database"
);
delete fileData[object][key];
write(this.file, fileData);
return;
}
//Code obsidestructor ⚔
has(data) {
if (data == null) throw Error("[err] No data to has function");
let fileData = load(this.file);
if (!fileData[data]) return false;
if (fileData[data]) return true;
}
//do not, delete the code below - - - discor
//--------------------------------\\
/*
Welcome, hope you are enjoying this package
this package was created for discord.gg/javascript
and for anyone's use. its open source and free for use
please credit us.
❤
*/
//--------------------------------\\
clear() {
write(this.file, {});
return;
}
fetchAll() {
return load(this.file);
}
all() {
return load(this.file);
}
destroy() {
fs.unlinkSync(this.file);
return;
}
fetch(data) {
if (data == null) throw Error("[err] No data to fetch");
let fileData = load(this.file);
if (!fileData[data]) fileData[data] = null;
return fileData[data];
}
get(data) {
if (data == null) throw Error("[err] No data to get");
let fileData = load(this.file);
if (!fileData[data]) fileData[data] = null;
return fileData[data];
}
objectFetch(object, key) {
let fileData = load(this.file);
if (object == null) throw Error("[err] No object to object fetch");
if (key == null) throw Error("[err] No key to object fetch");
if (!fileData[object])
throw Error(
"[err] The object to object fetch dosen't exist in the database"
);
if (typeof fileData[object] !== "object")
throw Error(
"[err] The provided object to object fetch is not an object in the database"
);
if (!fileData[object][key]) fileData[object][key] = null;
return fileData[object][key];
}
arrayFetch(array, number) {
let fileData = load(this.file);
if (array == null) throw Error("[err] No array to array fetch");
if (number == null && number != 0)
throw Error("[err] No index/number to array fetch");
if (!fileData[array] && !Array.isArray(fileData[array]))
throw Error("[err] The array to fetch dosen't exist or it's not array");
if (typeof number !== "number" && value !== 0)
throw Error(
`[err] The number/index to array fetch must be a number, received type: ${typeof value}`
);
if (!fileData[array][number]) fileData[array][number] = null;
return fileData[array][number];
}
math(data, operator, value) {
if (data == null) throw Error("[err] No data to math");
if (operator == null) throw Error("[err] No operator to math");
if (value == null) throw Error("[err] No value to math");
if (typeof value !== "number")
throw Error(
`[err] The value to math must be a number, received type: ${typeof value}`
);
let fileData = load(this.file);
if (operator === "-") {
let i = fileData[data] - value;
return i;
} else if (operator === "+") {
let ii = fileData[data] + value;
return ii;
} else if (operator === "*") {
let iii = fileData[data] * value;
return iii;
} else if (operator === "/") {
let iiii = fileData[data] / value;
return iiii;
} else {
throw Error(
"[err] unvaild operator to math, you can use only (-, +, *, /) (math) function"
);
}
}
}
module.exports = { Database };