coverage.db
Version:
A Database With JSON, SQL, Mongo.
154 lines (124 loc) • 3.82 kB
JavaScript
const fs = require("fs");
var colors = require('colors');
const quickDB = require("quick.db")
module.exports = class Json {
constructor(filePath) {
this.jsonFilePath = filePath || "coverage.json";
this.data = {};
this.json = this.jsonFilePath
if(!fs.existsSync(this.json)){
fs.writeFileSync(this.json, "{}", "utf-8");
if(this.json !== "coverage.json") return console.log(`✅ Succes! A file named ${this.json} was created.`.green);
console.log(`⚠️ Automatic coverage.json was generated because you did not specify a file name when selecting a database type. if you want to specify the name yourself, you will see ex.`.yellow + `
const DataBase = require("coverage.db")
const db = new DataBase.JSON("name.json");`.green)
} else {
this.Exists();
};
};
Exists(){
const savedData = JSON.parse(fs.readFileSync(this.json));
if(typeof savedData == "object"){
this.data = savedData;
};
};
saveDataToFile(){
fs.writeFileSync(this.json, JSON.stringify(this.data, null, 4), "utf-8");
};
get(key){
if(!key) return console.log("Please enter a value ex.".red + `
db.get("name")`.green)
return this.data[key];
};
fetch(key){
if(!key) return console.log("Please enter a value ex.".red + `
db.fetch("name")`.green)
return this.data[key];
};
has(key){
if(!key) return console.log("Please enter a value ex.".red + `
db.has("name")`.green)
return Boolean(this.data[key]);
};
set(key, value){
if(!value) return console.log("Please enter a value and data ex.".red + `
db.set("value", "data")`.green)
this.data[key] = value;
this.saveDataToFile();
};
move() {
console.log("QuickDB to Coverage.db: Started copying database.")
quickDB.fetchAll().map((data) => {
this.data[data.ID] = JSON.parse(data.data);
this.saveDataToFile()
console.log(`QuickDB to Coverage.db: Copied ${data.ID}`)
})
return true;
}
delete(key){
if(!key) return console.log("Please enter a value ex.".red + `
db.delete("name")`.green)
delete this.data[key];
this.saveDataToFile();
};
add(key, count){
if(!count) return console.log("Please enter a value and number ex.".red + `
db.add("value", number)`.green)
if(!this.data[key]) this.data[key] = 0;
this.data[key] += count;
this.saveDataToFile();
};
subtract(key, count){
if(!count) return console.log("Please enter a value and number ex.".red + `
db.subtract("value", number)`.green)
if(!this.data[key]) this.data[key] = 0;
this.data[key] -= count;
this.saveDataToFile();
};
sub(key, count){
if(!count) return console.log("Please enter a value and number ex.".red + `
db.sub("value", number)`.green)
if(!this.data[key]) this.data[key] = 0;
this.data[key] -= count;
this.saveDataToFile();
};
push(key, element){
if(!element) return console.log("Please enter a value and data ex.".red + `
db.push("value", "data")`.green)
if (!this.data[key]) this.data[key] = [];
this.data[key].push(element);
this.saveDataToFile();
};
clear(){
this.data = {};
this.saveDataToFile();
};
deleteAll(){
this.data = {};
this.saveDataToFile();
};
all(){
return Object.keys(this.data).map((key) => {
return {
key,
data: this.data[key]
};
});
};
fetchAll(){
return Object.keys(this.data).map((key) => {
return {
key,
data: this.data[key]
};
});
};
getAll(){
return Object.keys(this.data).map((key) => {
return {
key,
data: this.data[key]
};
});
};
};