UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

150 lines (149 loc) 7.17 kB
"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")); const ProcessENVManager_1 = __importDefault(require("../engines/ProcessENVManager")); class DatabaseEngineClass { constructor() { this.MAX_TRY_CONNECTION = 3; this.db = () => this._db; this.connect = () => __awaiter(this, void 0, void 0, function* () { // WARNING: this was changed during the deployment to ECS... // I've reverted it, but maybe it needs to be changed or looked into... var _a; this._uri = ((_a = ProcessENVManager_1.default.getEnvVariable('MONGO_URI')) !== null && _a !== void 0 ? _a : Helper_1.default.isDev()) ? 'mongodb://mongo:27017/remora' : 'mongodb://localhost:27017/remora'; this._client = new mongodb_1.MongoClient(this._uri); const errors = []; this._client = new mongodb_1.MongoClient(this._uri); for (let i = 0; i < this.MAX_TRY_CONNECTION; i++) { try { console.log(`Attempting to connect to mongo "${this._uri}`); yield this._client.connect(); this._db = this._client.db(Settings_1.default.db.name); this._connected = true; console.log('Connected to MongoDB'); break; } catch (error) { errors.push((i + 1) + ': connection to MongoDB throws this error: ' + error); } } if (!this._connected) console.error(`Despite ${this.MAX_TRY_CONNECTION} 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 { yield this._checkConnection(); 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 { yield this._checkConnection(); 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 { yield this._checkConnection(); 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 { yield this._checkConnection(); 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 { yield this._checkConnection(); 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 { yield this._checkConnection(); 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 { yield this._checkConnection(); 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; } }); this._checkConnection = () => __awaiter(this, void 0, void 0, function* () { if (this._connected) return; yield this.connect(); if (!this._connected) throw new Error(`Can't to perform db operation: unable to connect to the database (${this._uri})`); }); } } const DatabaseEngine = new DatabaseEngineClass(); exports.default = DatabaseEngine;