UNPKG

metusbase

Version:

This package is an NPM module that makes using JSON and Mongo fast, secure and easy. Developer: mete52

135 lines (134 loc) 4.23 kB
import path from "path"; import fs from "fs"; export class Database { constructor(filePath) { const dir = path.join(process.cwd(), "metusbase"); this.dbfile = path.join(dir, filePath || "database.json"); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } if (!fs.existsSync(this.dbfile)) { fs.writeFileSync(this.dbfile, JSON.stringify({}, null, 2), "utf-8"); } } readDB() { return JSON.parse(fs.readFileSync(this.dbfile, "utf-8")); } writeDB(content) { fs.writeFileSync(this.dbfile, JSON.stringify(content, null, 2), "utf-8"); } add(key, value) { const currentValue = this.get(key); if (typeof currentValue !== "number") { this.set(key, value); } else { this.set(key, currentValue + value); } } subtract(key, value) { const currentValue = this.get(key); if (typeof currentValue !== "number") { this.set(key, -value); } else { this.set(key, currentValue - value); } } set(key, value) { const dbContent = this.readDB(); const keys = key.split("."); let current = dbContent; for (let i = 0; i < keys.length - 1; i++) { if (!current[keys[i]] || typeof current[keys[i]] !== "object") { current[keys[i]] = {}; } current = current[keys[i]]; } current[keys[keys.length - 1]] = value; this.writeDB(dbContent); } get(key) { const dbContent = this.readDB(); const keys = key.split("."); let current = dbContent; for (const part of keys) { if (current[part] === undefined) return null; current = current[part]; } return current; } getAll() { return this.readDB(); } has(key) { const dbContent = this.readDB(); const keys = key.split("."); let current = dbContent; for (const part of keys) { if (current[part] === undefined) return false; current = current[part]; } return true; } delete(key) { const dbContent = this.readDB(); const keys = key.split("."); let current = dbContent; for (let i = 0; i < keys.length - 1; i++) { if (!current[keys[i]] || typeof current[keys[i]] !== "object") return; current = current[keys[i]]; } const lastKey = keys[keys.length - 1]; if (current[lastKey] !== undefined) { delete current[lastKey]; this.writeDB(dbContent); } } deleteAll(message) { if (!message) { return "You must provide a confirmation message to delete all data."; } this.writeDB({}); } push(key, value) { const dbContent = this.readDB(); const keys = key.split("."); let current = dbContent; for (let i = 0; i < keys.length - 1; i++) { if (!current[keys[i]] || typeof current[keys[i]] !== "object") { current[keys[i]] = {}; } current = current[keys[i]]; } const lastKey = keys[keys.length - 1]; if (!Array.isArray(current[lastKey])) { current[lastKey] = []; } current[lastKey].push(value); this.writeDB(dbContent); } unpush(key, value) { const dbContent = this.readDB(); const keys = key.split("."); let current = dbContent; for (let i = 0; i < keys.length - 1; i++) { if (!current[keys[i]] || typeof current[keys[i]] !== "object") return; current = current[keys[i]]; } const lastKey = keys[keys.length - 1]; if (Array.isArray(current[lastKey])) { if (value === undefined) { delete current[lastKey]; } else { current[lastKey] = current[lastKey].filter((item) => item !== value); } this.writeDB(dbContent); } } }