UNPKG

xiyudb

Version:

A JSON/LocalStorage database module, easy to setup & utilize.

180 lines (152 loc) 6.22 kB
const fs = require("fs"); //* Module is defined for file jobs. let messageadd; //* 'messageadd' function is defined. module.exports = class xiyudb { constructor(filePath){ this.jsonFilePath = filePath || "./xiyudb.json"; this.data = {}; if(!fs.existsSync(this.jsonFilePath)){ fs.writeFileSync(this.jsonFilePath, "{}", "utf-8"); } else { this.fetchDataFromFile(); }} fetchDataFromFile(){ const savedData = JSON.parse(fs.readFileSync(this.jsonFilePath)); if(typeof savedData === "object"){ this.data = savedData}} saveDataToFile(){ fs.writeFileSync(this.jsonFilePath, JSON.stringify(this.data, null, 2), "utf-8")} on(fonksiyon , config) { if(fonksiyon=="ready"){ let message; if(config.message){ message = config.message console.log(message)} else throw Error("XIYU_ERROR: Ready message is undefined or unreadable.")} if(fonksiyon=="dataAdd"){ if(config.message){ messageadd = config.message} else { throw Error("XIYU_ERROR: dataAdd message is undefined or unreadable.") } }} fetch(key){ if(!key) throw Error("XIYU_ERROR: The value that can be brought is not specified."); return this.data[key]} has(key){ if (!key) throw Error("XIYU_ERROR: The value to be conditioned was not specified."); return Boolean(this.data[key])} set(key, value){ this.data[key] = value; if (!key) throw Error("XIYU_ERROR: The value to change was not found."); if (!value) throw Error("XIYU_ERROR: The value to change was not found."); this.saveDataToFile()} delete(key){ if (!key) throw Error("XIYU_ERROR: The value to change was not found."); delete this.data[key]; this.saveDataToFile()} add(key, count){ if (!count) throw Error("No value was found to be added."); if (!key) throw Error("XIYU_ERROR: No value was found to be added."); if(!this.data[key]) this.data[key] = 0; this.data[key] += count; this.saveDataToFile(); if(messageadd) { console.log(messageadd)}} subtract(key, count){ if (!count) throw Error("XIYU_ERROR: The value to be extracted was not found."); if (!key) throw Error("XIYU_ERROR: The value to be extracted was not found."); if(!this.data[key]) this.data[key] = 0; this.data[key] -= count; this.saveDataToFile()} push(key, element){ if (!element) throw Error("XIYU_ERROR: The value is not found."); if (!key) throw Error("XIYU_ERROR: The value is not found."); if (!this.data[key]) this.data[key] = []; this.data[key].push(element); this.saveDataToFile()} backup(folderName, fileName) { if (!fileName) { fileName = "database.json"; } if (!folderName) { folderName = "backup"; } if (!fs.existsSync(`./${folderName}`, )) { fs.mkdirSync(`./${folderName}`, ); } if (!fs.existsSync(`./${folderName}/${fileName}`, )) { fs.writeFileSync(`./${folderName}/${fileName}`, "{}", ); } const database = fs.readFileSync("database.json", "utf-8"); const qwe = JSON.parse(database); fs.writeFileSync(`./${folderName}/${fileName}`, JSON.stringify(qwe), ); } backupTime(folderName, fileName, time) { if (!fileName) { fileName = "database.json"; } if (!folderName) { folderName = "backup"; } if (!time) { console.log('XIYU_ERROR: Backup time is required for backup the database.'); return; } if (!fs.existsSync(`./${folderName}`, )) { fs.mkdirSync(`./${folderName}`, ); } if (!fs.existsSync(`./${folderName}/${fileName}`, )) { fs.writeFileSync(`./${folderName}/${fileName}`, "{}", ); } const database = fs.readFileSync("database.json", "utf-8"); const qwe = JSON.parse(database); fs.writeFileSync(`./${folderName}/${fileName}`, JSON.stringify(qwe), ); setInterval(() => { const database = fs.readFileSync("database.json", "utf-8"); const qwe = JSON.parse(database); fs.writeFileSync(`./${folderName}/${fileName}`, JSON.stringify(qwe), ); }, time); } clear(){ this.data = {}; this.saveDataToFile()} math(key , transaction , key2) { if(!key) throw TypeError("XIYU_ERROR: The first number was not found.") if(!key2)throw TypeError("XIYU_ERROR: The second number was not found.") if(!transaction) throw TypeError("XIYU_ERROR: The transaction was not entered. (Please contact to xiyuDB support)") let number = parseInt(number) let number2 = parseInt(number2) if(transaction=="+") { return number + number2} if(transaction=="-") { return number - number2} if(transaction=="*") { return number * number2} if(transaction=="/") { return number / number2} else { throw Error("XIYU_ERROR: This transaction is undefined. (Please contact to xiyuDB support)") } } find(key) { if (!key) { console.log('XIYU_ERROR: Database find key is required.'); return; } if (typeof key !== 'string') { console.log('XIYU_ERROR: Database key must be a string.'); return; } const database = fs.readFileSync("database.json", "utf-8"); const qwe = JSON.parse(database); return qwe[key]; } all() { return Object.keys(this.data).map((anahtar) => { return { key: anahtar, data: this.data[anahtar] }; }); } }