evils.db
Version:
  
134 lines (133 loc) • 4.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoDatabase = void 0;
const lodash_1 = require("lodash");
const Database_1 = require("./Database");
const Error_1 = require("./Error");
class MongoDatabase extends Database_1.Database {
constructor(options) {
super();
Object.defineProperty(this, "options", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "collection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "connection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "collectionName", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
let mongoClient = undefined;
try {
mongoClient = require("mongoose");
}
catch {
throw new Error_1.DatabaseError('The "mongoose" module is not installed. Please install it with "npm install mongoose"');
}
let schema = new mongoClient.Schema({
key: {
type: mongoClient.Schema.Types.String,
unique: true,
required: true
},
value: {
type: mongoClient.Schema.Types.Mixed,
unique: true,
required: true
}
});
this.options = options;
this.collectionName = options.collectionName;
this.connection = mongoClient.createConnection(this.options.url, this.options.clientOptions);
this.collection = this.connection.model(this.options.collectionName, schema);
async () => {
await this.setAll();
};
}
async 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('.')) {
(0, lodash_1.set)(this.cache, dotKey, data);
(0, lodash_1.set)(this.cache, key, value);
await this.collection.updateOne({ key: dotKey }, { value: (0, lodash_1.get)(this.cache, dotKey) });
}
else {
await this.collection.updateOne({ key }, { value });
}
}
else {
if (key.includes('.')) {
await this.collection.create({ key: dotKey, value: (0, lodash_1.get)(this.cache, dotKey) });
}
else {
await this.collection.create({ key, value });
}
}
return value;
}
get(key) {
return (0, lodash_1.get)(this.cache, key);
}
async remove(key) {
(0, lodash_1.unset)(this.cache, key);
if (key.includes('.')) {
let dotKey = key.split('.')[0];
await this.set(dotKey, (0, lodash_1.get)(this.cache, dotKey));
}
else {
await this.collection.deleteOne({ key });
}
}
async getAll() {
return this.cache;
}
async setAll() {
let data = await this.collection.find();
for (const item of data) {
(0, lodash_1.set)(this.cache, item.key, item.value);
}
return this.cache;
}
async push(key, value) {
let data = (0, lodash_1.get)(this.cache, key);
if (!data)
data = [];
data.push(value);
await this.set(key, data);
return data;
}
async pull(key, value) {
let data = (0, lodash_1.get)(this.cache, key);
if (!data)
data = [];
data = data.filter((x) => x !== value);
await this.set(key, data);
return data;
}
async add(key, value) {
let data = (0, lodash_1.get)(this.cache, key);
if (!data)
data = 0;
data += value;
await this.set(key, data);
return data;
}
}
exports.MongoDatabase = MongoDatabase;