pro.db-arzex
Version:
a simple and easy database package make your life easy.
1 lines • 6.43 kB
JavaScript
const fs=require("fs");class Data{constructor(e){if("string"!=typeof e||""==e)throw Error("Filename must be a string");if(!e.endsWith(".json"))throw Error(`Filename must ends with .json extenton , try : ${e.split(".")[0]}.json`);this.naem=e}set(e,t){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return i[e]=t,fs.writeFileSync(this.naem,JSON.stringify(i,null,1))}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");let s=JSON.parse(fs.readFileSync(this.naem,"utf8"));return s[e]=t,fs.writeFileSync(this.naem,JSON.stringify(s,null,1))}}fetch(e){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");let t=JSON.parse(fs.readFileSync(this.naem,"utf8"));return t[e]}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return i[e]}}has(e){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");let t=JSON.parse(fs.readFileSync(this.naem,"utf8"));return!!t[e]}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return!!i[e]}}fetchAll(){let e=JSON.parse(fs.readFileSync(this.naem,"utf8"));return e}delete(e){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");let t=JSON.parse(fs.readFileSync(this.naem,"utf8"));if(!t[e])throw TypeError("Theres no data to add to database! \n"+__dirname);return delete t[e],fs.writeFileSync(this.naem,JSON.stringify(t,null,1))}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));if(!i[e])throw TypeError("Theres no data to add to database! \n"+__dirname);return delete i[e],fs.writeFileSync(this.naem,JSON.stringify(i,null,1))}}backup(e){if(fs.existsSync(this.naem)){if(!e)throw TypeError("Filename not defined!");let t=JSON.parse(fs.readFileSync(this.naem,"utf8"));return fs.writeFileSync(`${e}.json`,JSON.stringify(t,null,1))}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("Filename not defined!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return fs.writeFileSync(`${e}.json`,JSON.stringify(i,null,1))}}add(e,t){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");if(isNaN(t))throw TypeError("The value must be a number!");this.has(e)||this.set({data:e,value:0});let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return i[e]+=t,fs.writeFileSync(this.naem,JSON.stringify(i,null,1))}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");if(isNaN(t))throw TypeError("The value must be a number!");this.has(e)||this.set({data:e,value:0});let s=JSON.parse(fs.readFileSync(this.naem,"utf8"));return s[e]+=t,fs.writeFileSync(this.naem,JSON.stringify(s,null,1))}}subtract(e,t){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");if("number"!=typeof t)throw TypeError("The value must be a number!");if(!this.has(e))throw TypeError("The data is not defined!");if("number"!=typeof this.fetch(e))throw TypeError("The value must be number!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return i[e]-=t,fs.writeFileSync(this.naem,JSON.stringify(i,null,1))}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");if("number"!=typeof t)throw TypeError("The value must be a number!");if(!this.has(e))throw TypeError("The data is not defined!");if("number"!=typeof this.fetch(e))throw TypeError("The value must be number!");let s=JSON.parse(fs.readFileSync(this.naem,"utf8"));return s[e]-=t,fs.writeFileSync(this.naem,JSON.stringify(s,null,1))}}reset(){return JSON.parse(fs.readFileSync(this.naem,"utf8")),fs.writeFileSync(this.naem,JSON.stringify({},null,1))}push(e,t){if(fs.existsSync(this.naem)){let i=this.fetch(e);return i&&Array.isArray(i)?(i.push(t),this.set({data:e,value:i})):this.set({data:e,value:[t]})}{fs.writeFileSync(this.naem,"{}");let s=this.fetch(e);return s&&Array.isArray(s)?(s.push(t),this.set({data:e,value:s})):this.set({data:e,value:[t]})}}remove(e,t){if(fs.existsSync(this.naem)){let i=this.fetch(e);if(!i||!Array.isArray(i))return this.set({data:e,value:[]});{let s=i.filter(e=>e!=t);return this.set({data:e,value:s})}}{fs.writeFileSync(this.naem,"{}");let n=this.fetch(e);if(!n||!Array.isArray(n))return this.set({data:e,value:[]});{let r=n.filter(e=>e!=t);return this.set({data:e,value:r})}}}math(e,t,i){if(fs.existsSync(this.naem)){let s=JSON.parse(fs.readFileSync(this.naem,"utf8"));if(!e)throw TypeError("Theres no data founded!");if(!t)throw TypeError("The process not defined!");if(!i)throw TypeError("Theres no value entered!");if(!s[e])throw TypeError("couldnt get the data!");if(isNaN(i))throw TypeError("The value must be number!");if(isNaN(this.fetch(e)))throw TypeError("The data must be number!");switch(t){case"+":return s[e]+=i,fs.writeFileSync(this.naem,JSON.stringify(s,null,1));case"-":return s[e]-=i,fs.writeFileSync(this.naem,JSON.stringify(s,null,1));case"*":return s[e]*=i,fs.writeFileSync(this.naem,JSON.stringify(s,null,1));case"/":return s[e]/=i,fs.writeFileSync(this.naem,JSON.stringify(s,null,1));case"%":return s[e]%=i,fs.writeFileSync(this.naem,JSON.stringify(s,null,1));default:return}}else{fs.writeFileSync(this.naem,"{}");let n=JSON.parse(fs.readFileSync(this.naem,"utf8"));if(!e)throw TypeError("Theres no data founded!");if(!t)throw TypeError("The process not defined!");if(!i)throw TypeError("Theres no value entered!");if(!n[e])throw TypeError("couldnt get the data!");if(isNaN(i)||isNaN(this.fetch(e)))throw TypeError("The value must be number!");switch(t){case"+":return n[e]+=i,fs.writeFileSync(this.naem,JSON.stringify(n,null,1));case"-":return n[e]-=i,fs.writeFileSync(this.naem,JSON.stringify(n,null,1));case"*":return n[e]*=i,fs.writeFileSync(this.naem,JSON.stringify(n,null,1));case"/":return n[e]/=i,fs.writeFileSync(this.naem,JSON.stringify(n,null,1));case"%":return n[e]%=i,fs.writeFileSync(this.naem,JSON.stringify(n,null,1));default:return}}}get(e){if(fs.existsSync(this.naem)){if(!e)throw TypeError("The data is not defined!");let t=JSON.parse(fs.readFileSync(this.naem,"utf8"));return t[e]}{if(fs.writeFileSync(this.naem,"{}"),!e)throw TypeError("The data is not defined!");let i=JSON.parse(fs.readFileSync(this.naem,"utf8"));return i[e]}}}module.exports=Data;