trmegadb
Version:
Türkçe ve Kolay Bir Veritabanı Modülü
80 lines (54 loc) • 1.65 kB
JavaScript
const fs = require("fs");
module.exports = class megadb {
constructor(filePath){
this.jsonFilePath = filePath || "./mdb.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");
}
ogren(key){//get
return this.data[key];
}
kontrol(key){//control
return Boolean(this.data[key]);
}
degistir(key, value){//set
this.data[key] = value;
this.saveDataToFile();
}
sil(key){//delete
delete this.data[key];
this.saveDataToFile();
}
yeni(key, count){//add
if(!this.data[key]) this.data[key] = '';
this.data[key] += count;
this.saveDataToFile();
}
cikart(key, count){//subtract
if(!this.data[key]) this.data[key] = 0;
this.data[key] -= count;
this.saveDataToFile();
}
arttir(key, count){//subtract
if(!this.data[key]) this.data[key] = 0;
this.data[key] += count;
this.saveDataToFile();
}
tamamensil(){ //alldelete
this.data = {};
this.saveDataToFile();
}
};