evils.db
Version:
  
137 lines (136 loc) • 5.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqliteDatabase = void 0;
const Database_1 = require("./Database");
const Error_1 = require("./Error");
const path_1 = require("path");
const fs_1 = require("fs");
const lodash_1 = require("lodash");
class SqliteDatabase extends Database_1.Database {
constructor(options) {
super();
Object.defineProperty(this, "SQLQuery", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "options", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
let notSQLQuery = undefined;
this.options = options;
try {
notSQLQuery = require("better-sqlite3");
}
catch {
throw new Error_1.DatabaseError('The "better-sqlite3" module is not installed. Please install it with "npm install better-sqlite3"');
}
let paths = this.options.filePath.split(path_1.sep);
let resolvedFilePath = process.cwd();
for (const path of paths) {
resolvedFilePath += `${path_1.sep}${path}`;
if (!(0, fs_1.existsSync)(resolvedFilePath) && (0, path_1.extname)(resolvedFilePath) !== '.db')
(0, fs_1.mkdirSync)(resolvedFilePath);
else if (!(0, fs_1.existsSync)(resolvedFilePath) && (0, path_1.extname)(resolvedFilePath) === '.db') {
this.SQLQuery = notSQLQuery(resolvedFilePath);
break;
}
else if ((0, fs_1.existsSync)(resolvedFilePath) && (0, path_1.extname)(resolvedFilePath) === '.db') {
this.SQLQuery = notSQLQuery(resolvedFilePath);
break;
}
}
this.SQLQuery.prepare(`CREATE TABLE IF NOT EXISTS ${options.table} (key TEXT, value TEXT)`).run();
}
getAll() {
return this.cache;
}
get(key) {
return (0, lodash_1.get)(this.cache, key);
}
set(key, value) {
let dotKey = key.split('.')[0];
let data = (0, lodash_1.get)(this.cache, dotKey);
(0, lodash_1.set)(this.cache, key, value);
if (data) {
if (key.includes('.')) {
let data = (0, lodash_1.get)(this.cache, dotKey);
this.SQLQuery.prepare(`UPDATE ${this.options.table} SET value = ? WHERE key = ?`).run(JSON.stringify(data), dotKey);
}
else {
this.SQLQuery.prepare(`UPDATE ${this.options.table} SET value = ? WHERE key = ?`).run(JSON.stringify(value), key);
}
}
else {
if (key.includes('.')) {
let data = (0, lodash_1.get)(this.cache, dotKey);
this.SQLQuery.prepare(`INSERT INTO ${this.options.table} (key, value) VALUES (?, ?)`).run(dotKey, JSON.stringify(data));
}
else {
this.SQLQuery.prepare(`INSERT INTO ${this.options.table} (key, value) VALUES (?, ?)`).run(key, JSON.stringify(value));
}
}
return value;
}
remove(key) {
(0, lodash_1.unset)(this.cache, key);
this.SQLQuery.prepare(`DELETE FROM ${this.options.table} WHERE key = ?`).run(key);
}
add(key, value) {
let data = this.get(key);
if (typeof data !== 'number')
throw new Error_1.DatabaseError(`Cannot add "${value}" to "${key}" because "${key}" is not a number.`);
data += value;
this.set(key, data);
return data;
}
push(key, value) {
let data = this.get(key);
if (!Array.isArray(data))
throw new Error_1.DatabaseError(`Cannot push "${value}" to "${key}" because "${key}" is not an array.`);
data.push(value);
this.set(key, data);
return data;
}
pull(key, value) {
let data = this.get(key);
if (!Array.isArray(data))
throw new Error_1.DatabaseError(`Cannot pull "${value}" from "${key}" because "${key}" is not an array.`);
data = data.filter((v) => v !== value);
this.set(key, data);
return data;
}
clear() {
this.cache = {};
this.SQLQuery.prepare(`DELETE FROM ${this.options.table}`).run();
}
close() {
this.SQLQuery.close();
}
async init() {
let data = this.SQLQuery.prepare(`SELECT * FROM ${this.options.table}`).all();
for (const row of data) {
this.cache[row.key] = JSON.parse(row.value);
}
}
async save() {
for (const key in this.cache) {
let data = this.cache[key];
if (typeof data === 'object')
data = JSON.stringify(data);
this.SQLQuery.prepare(`UPDATE ${this.options.table} SET value = ? WHERE key = ?`).run(data, key);
}
}
async destroy() {
this.SQLQuery.prepare(`DROP TABLE ${this.options.table}`).run();
}
async delete() {
this.SQLQuery.close();
this.SQLQuery = undefined;
}
}
exports.SqliteDatabase = SqliteDatabase;