UNPKG

evils.db

Version:

![Image](https://img.shields.io/npm/v/evils.db) ![Image](https://img.shields.io/npm/dt/evils.db) ![Image](https://nodei.co/npm/evils.db.png)

141 lines (140 loc) 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YamlDatabase = void 0; const fs_1 = require("fs"); const lodash_1 = require("lodash"); const path_1 = require("path"); const Database_1 = require("./Database"); const Error_1 = require("./Error"); class YamlDatabase extends Database_1.Database { constructor(options) { super(); Object.defineProperty(this, "options", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "yaml", { enumerable: true, configurable: true, writable: true, value: undefined }); this.options = options; try { this.yaml = require('yaml'); } catch { throw new Error_1.DatabaseError('The "yaml" module is not installed. Please install it with "npm install yaml"'); } const resolvedFilePath = process.cwd(); const filePath = resolvedFilePath + path_1.sep + this.options.filePath; const paths = this.options.filePath.split(path_1.sep); for (const path of paths) { const resolvedPath = resolvedFilePath + path_1.sep + path; if (!(0, fs_1.existsSync)(resolvedPath)) { (0, fs_1.writeFileSync)(resolvedPath, ''); } } } set(key, value) { (0, lodash_1.set)(this.cache, key, value); (0, fs_1.writeFileSync)(this.options.filePath, this.yaml.stringify(this.cache)); return value; } get(key) { return (0, lodash_1.get)(this.cache, key); } getAll() { return this.cache; } delete(key) { const value = (0, lodash_1.get)(this.cache, key); delete this.cache[key]; (0, fs_1.writeFileSync)(this.options.filePath, this.yaml.stringify(this.cache)); return value; } clear() { this.cache = {}; (0, fs_1.writeFileSync)(this.options.filePath, this.yaml.stringify(this.cache)); return this.cache; } has(key) { return this.cache[key] ? true : false; } push(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (!Array.isArray(data)) throw new Error_1.DatabaseError(`The key "${key}" is not an array.`); data.push(value); this.set(key, data); return data; } subtract(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (typeof data !== 'number') throw new Error_1.DatabaseError(`The key "${key}" is not a number.`); this.set(key, data - value); return data - value; } add(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (typeof data !== 'number') throw new Error_1.DatabaseError(`The key "${key}" is not a number.`); this.set(key, data + value); return data + value; } divide(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (typeof data !== 'number') throw new Error_1.DatabaseError(`The key "${key}" is not a number.`); this.set(key, data / value); return data / value; } multiply(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (typeof data !== 'number') throw new Error_1.DatabaseError(`The key "${key}" is not a number.`); this.set(key, data * value); return data * value; } includes(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (!Array.isArray(data)) throw new Error_1.DatabaseError(`The key "${key}" is not an array.`); return data.includes(value); } pull(key, value) { const data = this.get(key); if (!data) throw new Error_1.DatabaseError(`The key "${key}" does not exist in the database.`); if (!Array.isArray(data)) throw new Error_1.DatabaseError(`The key "${key}" is not an array.`); const index = data.indexOf(value); if (index === -1) return data; data.splice(index, 1); this.set(key, data); return data; } get size() { return Object.keys(this.cache).length; } get yamlCache() { return this.yaml.parse(require('fs').readFileSync(this.options.filePath, 'utf8')); } } exports.YamlDatabase = YamlDatabase;