"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_1 = require("sequelize");
const lodash_1 = require("lodash");
const logger_1 = require("./utils/logger");
// @ts-ignore
const database_1 = __importDefault(require("./config/database"));
const queryLogger = (sql, queryObject) => {
    logger_1.logger.debug(sql);
};
class Database {
    connect(options = {}) {
        if (this._sequelize) {
            return;
        }
        this.config = (0, lodash_1.merge)({}, database_1.default[process.env.NODE_ENV], {
            logging: queryLogger
        });
        logger_1.logger.debug(this.config);
        this._sequelize = new sequelize_1.Sequelize(Object.assign({ dialect: 'postgres' }, this.config));
    }
    get sequelize() {
        if (!this._sequelize) {
            this.connect();
        }
        return this._sequelize;
    }
    close(done) {
        if (!this._sequelize) {
            done();
            return;
        }
        this._sequelize.close().then(() => done());
    }
}
const AppDatabase = new Database();
exports.default = AppDatabase;
