sags.db.ts
Version:
Fast JSON database system.
123 lines (122 loc) • 4.49 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sags = void 0;
const lodash_1 = __importDefault(require("lodash"));
const fs_1 = __importDefault(require("fs"));
const Error_1 = require("./Error");
class Sags {
constructor(setting) {
var _a, _b, _c;
this.name = (_a = setting === null || setting === void 0 ? void 0 : setting.name) !== null && _a !== void 0 ? _a : "sags";
this.folder = (_b = setting === null || setting === void 0 ? void 0 : setting.folder) !== null && _b !== void 0 ? _b : "database";
this.minify = (_c = setting === null || setting === void 0 ? void 0 : setting.minify) !== null && _c !== void 0 ? _c : true;
this.folderPath = this.folder
.toString()
.toLowerCase()
.split("/")
.filter((x) => x != ".");
if (!fs_1.default.existsSync(`./${this.folderPath.join("/")}`)) {
this.folderPath.reduce((previus, current, i, a) => {
previus += current + "/";
if (!fs_1.default.existsSync(`.${previus}`)) {
fs_1.default.mkdirSync(`.${previus}`);
}
if (i === a.length - 1) {
fs_1.default.writeFileSync(`./${this.folderPath.join("/") + "/" + this.name}.json`, "{}");
}
return previus;
}, "/");
}
this.db = JSON.parse(fs_1.default
.readFileSync(`./${this.folderPath.join("/") + "/" + this.name}.json`)
.toString());
this.saveDB = (data) => {
const json_data = this.minify
? JSON.stringify(data)
: JSON.stringify(data, null, 2);
return fs_1.default.writeFileSync(`./${this.folderPath.join("/") + "/" + this.name}.json`, json_data);
};
}
set(key, data) {
const json_data = lodash_1.default.set(this.db, key, data !== null && data !== void 0 ? data : null);
this.saveDB(json_data);
return this;
}
delete(key) {
const newDb = Object.assign({}, this.db);
lodash_1.default.unset(newDb, key);
this.saveDB(newDb);
return this;
}
get(key) {
return lodash_1.default.get(this.db, key);
}
has(key) {
return lodash_1.default.has(this.db, key);
}
all() {
return this.db;
}
deleteAll() {
this.saveDB({});
return true;
}
type(key) {
const type = this.get(key);
return Array.isArray(type) ? "array" : typeof type;
}
push(key, data) {
const item = this.get(key);
if (typeof item !== "object" && !Array.isArray(item)) {
this.set(key, data);
return this;
}
if (Array.isArray(item)) {
item.push(data);
this.set(key, item);
return this;
}
new Error_1.SagsdbError("This Key data is not an Array.");
return this;
}
unpush(key, data) {
const item = this.get(key);
if (item !== "object" && !Array.isArray(item))
return new Error_1.SagsdbError("This Key data is not an Array.");
lodash_1.default.remove(item, function (n) {
return n === data;
});
if (item.length === 0) {
this.delete(key);
return this;
}
this.set(key, item);
return this;
}
add(key, number) {
var _a;
let item = (_a = this.get(key)) !== null && _a !== void 0 ? _a : 0;
if (typeof item !== "number")
return new Error_1.SagsdbError("This Key data is not an Number.");
item += number;
this.set(key, item);
return this;
}
substract(key, number) {
var _a;
let item = (_a = this.get(key)) !== null && _a !== void 0 ? _a : 0;
if (typeof item !== "number")
return new Error_1.SagsdbError("This Key data is not an Number.");
item -= number;
this.set(key, item);
return this;
}
dbSIZE() {
return fs_1.default.statSync(`./${this.folderPath.join("/") + "/" + this.name}.json`)
.size;
}
}
exports.Sags = Sags;