easy.database
Version:
Una base de datos sencilla para tu proyecto (NodeJS)
125 lines (114 loc) • 4.18 kB
JavaScript
const fs = require("fs");
var privateDatabase = {
"folder":"./easydb/"
};
class Database {
constructor(name, subclass)
{
if(subclass === undefined){
fs.mkdirSync(privateDatabase.folder, { recursive: true });
this.loc = privateDatabase.folder + "/" + name + ".json";
}
if(subclass){
fs.mkdirSync(privateDatabase.folder + subclass, { recursive: true });
this.loc = privateDatabase.folder + subclass + "/" + name + ".json";
}
if(fs.existsSync(this.loc))
{
this.data = JSON.parse(fs.readFileSync(this.loc, "utf8"));
}
else this.data = {};
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
has(key){
let params = key.split('.');
if(params[1] !== undefined) {
if(this.data[params[0]] === undefined) return false;
return (this.data[params[0]][params[1]] === undefined ? false : true);
}
return (this.data[key] === undefined ? false : true);
}
set(key, value){
if(!value) return console.log("[EasyDB]-> You must specify the value in set();");
let params = key.split('.');
if(params[1] !== undefined) this.data[ params[0] ][ params[1] ] = value;
else this.data[key] = value;
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
get(key){
if(!key) return console.log("[EasyDB]-> You must specify the key in get();");
let params = key.split('.');
if(params[1] !== undefined)
{
if(this.data[params[0]] === undefined) return undefined;
return this.data[ params[0] ][ params[1] ];
}
return this.data[key];
}
reload(){
this.data = JSON.parse(fs.readFileSync(this.loc, "utf8"));
}
add(key, value){
if(isNaN(value)) return console.log("[EasyDB]-> It is not an integer in add();");
let params = key.split('.');
if(params[1] !== undefined) this.data[ params[0] ][ params[1] ] += value;
else this.data[key] += value;
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
substract(key, value){
if(isNaN(value)) return console.log("[EasyDB]-> It is not an integer in substract();");
let params = key.split('.');
if(params[1] !== undefined) this.data[ params[0] ][ params[1] ] -= value;
else this.data[key] -= value;
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
push(key, value){
if(!value) return console.log("[EasyDB] -> You must specify 'Value' in push")
if(!key) return console.log("[EasyDB] -> You must specify 'Key' in push")
let params = key.split('.');
if(params[1] !== undefined){
let fetched = this.data[ params[0] ][ params[1] ]
if(fetched === "{}") fetched = [];
if(!Array.isArray(fetched))return console.log("This is not an array")
else fetched.push(value)
this.data[ params[0] ][ params[1] ] = fetched
}else{
let fetched = this.data[key]
if(fetched === "{}") fetched = [];
if(!Array.isArray(fetched))return console.log("This is not an array")
else fetched.push(value)
this.data[key] = fetched
}
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
extract(key, value){
let params = key.split('.');
if(params[1] !== undefined){
let array = this.data[ params[0] ][ params[1] ]
if(!Array.isArray(array)) return console.log("This is not an array")
if(!array.includes(value)) return new Error("The placed value is not in the array")
const newarr = array.filter(ele => ele != value)
return this.data[ params[0] ][ params[1] ] = newarr
}else{
let array = this.data[key]
if(!Array.isArray(array)) return console.log("This is not an array")
if(!array.includes(value)) return new Error("The placed value is not in the array")
const newarr = array.filter(ele => ele != value)
return this.data[key] = newarr
}
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
remove(key){
let params = key.split('.');
if(params[1] !== undefined) delete this.data[ params[0] ][ params[1] ];
if(!key) return console.log("[EasyDB]-> You must specify the key in remove();");
delete this.data[key];
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
}
all(){
let array = []
array.push(this.data)
return array
}
}
module.exports = Database;