"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Mongoose = require("mongoose");
class MoongosseConnection {
    /** */
    constructor(config) {
        this.config = config;
        this.url = `mongodb://${config?.mongoDb?.dbHost}:${config?.mongoDb?.dbPort}/${config?.mongoDb?.dbName}`;
        return this;
    }
    getActiveConnections() {
        const connections = Mongoose.connections.map((connnection) => connnection);
        return connections.filter((connection) => connection.readyState === 1);
    }
    async connect() {
        if ((this.getActiveConnections()).length > 0) {
            return Mongoose.connection;
        }
        await Mongoose.connect(this.url, {
            useCreateIndex: true,
            useFindAndModify: true,
            useNewUrlParser: true,
            useUnifiedTopology: true,
        }).catch((error) => {
            console.log(error.message);
            throw error;
        });
        const connection = Mongoose.connection;
        connection.once('open', () => {
            console.log('Connected to database');
        });
        connection.on('error', (error) => {
            console.log(`Error connecting to database ${error.message}`);
        });
        return connection;
    }
    /**
     * Get database size
     *
     * @returns {Promise<number>}
     * @memberof MongoDbConnection
     */
    async getDatabaseSize() {
        const connection = await this.connect();
        const result = (await connection.db.stats({ scale: 1024 * 1024 }));
        return result.dataSize;
    }
}
exports.default = MoongosseConnection;
//# sourceMappingURL=mongoose-connection.js.map