UNPKG

mskdb

Version:

This is a nosql database, built with node js

327 lines (294 loc) 9.27 kB
/* *Plugin name: MSK_DB *Author: Mr Skillzdboss *License: MIT */ const fs = require('fs-extra') const path = require("path"); const today = new Date().toLocaleDateString() const now = new Date().toLocaleTimeString() class MSKDB { constructor (config){ this.config = config; this.dirName = process.cwd(); this.dirName = this.#findDir('models'); this.ext = this.#findExt('mdb'); this.log = this.#findShowLog(true); this.dbname = this.#findDbName('database'); this.backup = this.#canBackup(false); this.backup_path = this.#findBackupDir('backup'); this.database = `${this.dirName}/${this.dbname}.${this.ext}`; this.emptyRow = "[\n\t\n]"; this.encoding = 'utf-8'; this.#createDb(); this.backup_db = `${this.backup_path}/backup_${this.dbname}.${this.ext}` if(this.backup){ setTimeout(() => { this.#backup(); }, 500); } } #loger(data){ if (this.log) { console.log(this.#col(), data); } } #findDir(replacement){ try { if (this.config.directory) { return this.config.directory; } else { return replacement; } } catch (err) { return replacement; } } #findExt(replacement){ try { if (this.config.extension) { return this.config.extension; } else { return replacement; } } catch (err) { return replacement; } } #findDbName(replacement){ try { if (this.config.name) { return this.config.name; } else { return replacement; } } catch (err) { return replacement; } } #canBackup(){ try { if (this.config.backup) { return this.config.backup; } else { return false; } } catch (err) { return false; } } #findShowLog(showlog){ try { if (this.config.log) { return this.config.log; } else { return showlog; } } catch (err) { return showlog; } } #createDb(){ if (!fs.existsSync(this.dirName)) { fs.mkdirSync(this.dirName); this.#loger('Directory has been created!'); } if (!fs.existsSync(this.database)) { fs.writeFile(this.database, "[\n\t\n]", this.encoding); this.#loger('database has been created!'); } if(this.backup){ this.#backup() } } #backup(){ let backup_db = `${this.backup_path}/backup_${this.dbname}.${this.ext}` if (!fs.existsSync(this.backup_path)) { fs.mkdirSync(this.backup_path); this.#loger('Directory has been created!'); } if (!fs.existsSync(backup_db)) { fs.writeFile(`${backup_db}`, this.emptyRow, this.encoding); this.#loger('backup has been created!'); } let data = this.all() fs.writeFileSync(backup_db, JSON.stringify(data, null, 2), this.encoding) console.log("Backup completed"); } #findBackupDir(replacement){ try { if (this.config.backup_path) { return this.config.backup_path; } else { return replacement; } } catch (err) { return replacement; } } #col($col="red"){ if ($col=='red') { return '\x1b[31m%s\x1b[0m'; }else if ($col=='blue') { return '\x1b[34m%s\x1b[0m'; }else if ($col=='green') { return '\x1b[32m%s\x1b[0m'; }else if ($col=='yellow') { return '\x1b[33m%s\x1b[0m'; }else if ($col=='magenta') { return '\x1b[35m%s\x1b[0m'; }else if ($col=='cyan') { return '\x1b[36m%s\x1b[0m'; }else { return '\x1b[37m%s\x1b[0m'; } } #writer(payload){ return fs.writeFileSync(`${this.database}`, JSON.stringify(payload, null, 2), this.encoding); } #order(){ let rows = [] this.all().forEach((row)=>{ rows.push(row.id) }) let ordered = rows.sort(); let sorted = [] ordered.forEach((id)=>{ sorted.push(this.findOne(id)[0]); }) this.#writer(sorted); } all(){ return JSON.parse(fs.readFileSync(`${this.database}`, this.encoding)) } insert(data){ let id = 0; fs.readFile(`${this.database}`, this.encoding, (err, content)=>{ let oldData = JSON.parse(content); oldData.forEach((row)=>{if(row.id > id) id = row.id}) let templateData = {id: id+=1, data, date: today, time: now}; oldData.push(templateData) this.#writer(oldData); this.#loger("1 row added"); }) return this } add(data){ let id = 0; fs.readFile(`${this.database}`, this.encoding, (err, content)=>{ let oldData = JSON.parse(content); oldData.forEach((row)=>{if(row.id > id) id = row.id}) let templateData = {id: id+=1, data, date: today, time: now}; oldData.push(templateData) this.#writer(oldData); this.#loger("1 row added"); }) return this } amend(id, data){ let row = this.findOne(id); let update = row[0].data = data let getAll = this.all().filter((row)=>{return row.id !== id}) getAll.push(row[0]) this.#writer(getAll); this.#order() } upgrade(id, data){ let row = this.findOne(id); let update = row[0].data = data let getAll = this.all().filter((row)=>{return row.id !== id}) getAll.push(row[0]) this.#writer(getAll); this.#order() } update(id, data){ let row = this.findOne(id); let update = row[0].data = data let getAll = this.all().filter((row)=>{return row.id !== id}) getAll.push(row[0]) this.#writer(getAll); this.#order() } // delete one drop(id){ let rows = this.all().filter((row)=>{return row.id !== parseInt(id)}) this.#writer(rows) this.#order() return "1 row droped"; } pop(id){ let rows = this.all().filter((row)=>{return row.id !== parseInt(id)}) this.#writer(rows) this.#order() return "1 row droped"; } delete(id){ let rows = this.all().filter((row)=>{return row.id !== parseInt(id)}) this.#writer(rows) this.#order() return "1 row droped"; } // special clone(file){ let data = this.all() fs.writeFileSync(file, JSON.stringify(data, null, 2), this.encoding) console.log("database has been cloned"); } find(search){ let result = []; Object.entries(search).forEach(([key, value]) => { result = this.all().filter(row => row.data[key] == value); }); return result; } findOne(id){ return JSON.parse(fs.readFileSync(`${this.database}`, this.encoding)).filter((row)=>{return row.id === parseInt(id)}) } format(){ return fs.writeFileSync(this.database, '[\n\t\n]', this.encoding); } dir(dir){ this.directory = dir; if (!fs.existsSync(this.directory)){ fs.mkdirSync(this.directory); } this.directory = dir return this } unsetDir(dir){ fs.rm(dir, { recursive: true }, (err) => { if (err) { console.error(err); } }); return this } wipe(){ return fs.writeFileSync(`${this.database}`, this.emptyRow, this.encoding); } info(){ return{ dir: this.dirName, db: this.database, version: "1.0.0", backup: this.backup, backup_path: this.backup_path, backup_db: this.backup_db, author: "Promise Peter Akpan (A.K.A Mr Skillz)", methods:{ create: "insert, add", read: "find, findOne, all", update: "amend, upgrade, update", clone: "clone", delete: { single:"pop, delete", all: "dropDb", format: "format", directory: "unsetDir", wipe: "drop a database" } } } } } module.exports = MSKDB