All files MsSql.js

30.3% Statements 10/33
53.33% Branches 8/15
20% Functions 1/5
31.25% Lines 10/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97          1x 1x 1x                                             1x 1x 1x 1x   1x   1x                                                                                                                   1x  
/**
 * @author David Menger
 */
'use strict';
 
const mssql = require('mssql');
const path = require('path');
const Migrate = require('./Migrate');
 
 
class MsSql {
 
    /**
     *
     * @param {object} config
     * @param {string} config.user
     * @param {string|Promise<string>} config.password;
     * @param {string} config.server
     * @param {number} config.port
     * @param {string} config.database
     * @param {object} config.options
     * @param {object} [config.mssql]
     * @param {Function} [config.pool]
     * @param {string} [config.wingbotMigrationsTable]
     * @param {object} config.options
     * @param {boolean} config.options.encrypt
     * @param {string|null} migrationsDir
     * @param {console} log
     */
    constructor (config, migrationsDir = null, log = console) {
        this._config = config;
        this._migrationsDir = migrationsDir || path.resolve(__dirname, '..', 'migrations');
        this._migrationsTable = config.wingbotMigrationsTable || undefined;
        this._log = log;
 
        this._mssql = config.mssql || mssql;
 
        this._pool = null;
    }
 
    async _createConnectionPoll () {
        try {
            let { pool = null } = this._config;
 
            if (!pool) {
                let config;
                if (this._config.password instanceof Promise) {
                    const password = await this._config.password;
                    config = {
                        ...this._config,
                        password
                    };
                } else {
                    config = this._config;
                }
 
                // @ts-ignore
                const cp = new this._mssql.ConnectionPool(config);
 
                cp.on('error', (err) => {
                    this._log.error('MSSQL ERROR', err);
                });
 
                pool = cp.connect();
            }
 
            const connection = await pool;
 
            const migrate = new Migrate(pool, this._migrationsDir, this._migrationsTable);
 
            await migrate.migrate();
 
            return connection;
        } catch (e) {
            this._log.error('MSSQL ERROR', e);
            await new Promise((r) => setTimeout(r, 400));
            process.exit(1);
            throw e;
        }
    }
 
    /**
     * Get connection pool for storages
     *
     * @returns {Promise<mssql.ConnectionPool>}
     */
    connection () {
        if (!this._pool) {
            this._pool = this._createConnectionPoll();
        }
        return this._pool;
    }
 
}
 
module.exports = MsSql;