@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
130 lines (129 loc) • 6 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongodb_1 = require("mongodb");
const Helper_1 = __importDefault(require("../helper/Helper"));
const Settings_1 = __importDefault(require("../helper/Settings"));
class DatabaseEngineClass {
constructor() {
this.MAX_TRY_CONNECTION = 3;
this.db = () => this._db;
this.connect = () => __awaiter(this, void 0, void 0, function* () {
const errors = [];
for (let i = 0; i < this.MAX_TRY_CONNECTION; i++) {
if (i < this.MAX_TRY_CONNECTION) {
try {
yield this._client.connect();
this._db = this._client.db(Settings_1.default.db.name);
console.log('Connected to MongoDB');
break;
}
catch (error) {
errors.push(i + '° connection to MongoDB throws this error:', error);
}
}
else {
console.error(`Despite several ${i} retries it was not possible to connect to mongoDb, these are the errors encountered:\n` + errors.join('\n'));
}
}
});
this.disconnect = () => __awaiter(this, void 0, void 0, function* () {
try {
yield this._client.close();
console.log('Disconnected from MongoDB');
}
catch (error) {
console.error('Error disconnecting from MongoDB:', error);
}
});
this.query = (collectionName, filter, options) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
const result = yield collection.find(filter, options).toArray();
return result;
}
catch (error) {
console.error('Error getting documents:', error);
throw error;
}
});
this.aggregate = (collectionName, aggregation) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
return yield collection.aggregate(aggregation).toArray();
}
catch (error) {
console.error('Error aggregate documents:', error);
throw error;
}
});
this.get = (collectionName, id) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
return yield collection.findOne({ _id: id });
}
catch (error) {
console.error('Error finding document by ID:', error);
throw error;
}
});
this.findOne = (collectionName, query) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
return yield collection.findOne(query);
}
catch (error) {
console.error('Error finding one document:', error);
throw error;
}
});
this.upsert = (collectionName, id, update) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
const result = yield collection.findOneAndUpdate({ _id: id }, { $set: update }, { upsert: true, returnDocument: 'after' });
return result;
}
catch (error) {
console.error('Error upserting document:', error);
throw error;
}
});
this.addToList = (collectionName, id, arrayField, arrayItem) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
const result = yield collection.findOneAndUpdate({ _id: id }, { $push: { [arrayField]: arrayItem } }, { returnDocument: 'after' });
return result;
}
catch (error) {
console.error('Error adding item to list:', error);
throw error;
}
});
this.doUpdate = (collectionName, id, update) => __awaiter(this, void 0, void 0, function* () {
try {
const collection = this._db.collection(collectionName);
const result = yield collection.findOneAndUpdate({ _id: id }, update, { returnDocument: 'after' });
return result;
}
catch (error) {
console.error('Error adding item to list:', error);
throw error;
}
});
const uri = Helper_1.default.isDev() ? 'mongodb://mongo:27017/remora' : 'mongodb://localhost:27017/remora';
this._client = new mongodb_1.MongoClient(uri);
}
}
const DatabaseEngine = new DatabaseEngineClass();
exports.default = DatabaseEngine;