Source: Database/Relational/MySQL/MySQLDatabaseConnection.js

const mysql = require('mysql');

const IRelationalDatabaseConnection = require('../IRelationalDatabaseConnection.js');

function _applyConnectionInfo(connectionInfo) {
    if (connectionInfo.hasOwnProperty('Host')) { this._host = connectionInfo['Host']; }
    else if (connectionInfo.hasOwnProperty('host')) { this._host = connectionInfo['host']; }
    else if (connectionInfo.hasOwnProperty('_host')) { this._host = connectionInfo['_host']; }

    if (connectionInfo.hasOwnProperty('Port')) { this._port = connectionInfo['Port']; }
    else if (connectionInfo.hasOwnProperty('port')) { this._port = connectionInfo['port']; }
    else if (connectionInfo.hasOwnProperty('_port')) { this._port = connectionInfo['_port']; }

    if (connectionInfo.hasOwnProperty('Database')) { this._database = connectionInfo['Database']; }
    else if (connectionInfo.hasOwnProperty('database')) { this._database = connectionInfo['database']; }
    else if (connectionInfo.hasOwnProperty('_database')) { this._database = connectionInfo['_database']; }

    if (connectionInfo.hasOwnProperty('Username')) { this._username = connectionInfo['Username']; }
    else if (connectionInfo.hasOwnProperty('username')) { this._username = connectionInfo['username']; }
    else if (connectionInfo.hasOwnProperty('_username')) { this._username = connectionInfo['_username']; }

    if (connectionInfo.hasOwnProperty('Password')) { this._password = connectionInfo['Password']; }
    else if (connectionInfo.hasOwnProperty('password')) { this._password = connectionInfo['password']; }
    else if (connectionInfo.hasOwnProperty('_password')) { this._password = connectionInfo['_password']; }

    this._connection = mysql.createConnection({
        host: this._host,
        port: this._port,
        database: this._database,
        user: this._username,
        password: this._password
    });
}

/**
 * MySQL database connection.
 * @extends IRelationalDatabaseConnection
 */
class MySQLDatabaseConnection extends IRelationalDatabaseConnection {
    /**
     * Creates a new MySQL database connection.
     * @param connectionInfo The connection info to utilize for the connection.
     * @param {string} [connectionInfo.host="localhost"] The host to connect to.
     * @param {number} [connectionInfo.port=3306] The port to connect to.
     * @param {string} [connectionInfo.database="aquae"] The database to open with the connection.
     * @param {string} [connectionInfo.username=""] The username to supply with the connection.
     * @param {string} [connectionInfo.password=""] The password to supply with the connection.
     */
    constructor(connectionInfo) {
        super(connectionInfo);
        this._host = 'localhost';
        this._port = 3306;
        this._database = 'aquae';
        this._username = '';
        this._password = '';
        _applyConnectionInfo.bind(this)();
    }

    /**
     * Closes an open MySQL database and returns a Promise.
     * @function MySQLDatabaseConnection#close
     * @return {Promise}
     */
    /**
     * Closes an open MySQL database and utilizes the supplied callback when complete.
     * @param {IRelationalDatabaseConnection~closeCallback} callback The node.js style callback to use in conjunction with the command.
     * @function MySQLDatabaseConnection#close
     */
    close(callback) {
        if (arguments.length > 0) {
            this._connection.end(callback);
        } else {
            return (new Promise((resolve, reject) => { this._connection.end((err) => { if (err) { reject(err); } else { resolve(); }}); }));
        }
    }

    /**
     * Connects to a MySQL database and returns a Promise.
     * @function MySQLDatabaseConnection#connect
     * @return {Promise}
     */
    /**
     * Connects to a MySQL database with the supplied connection info and returns a Promise.
     * @param connectionInfo The connection info to utilize for the connection.
     * @param {string} [connectionInfo.host] The host to connect to.
     * @param {number} [connectionInfo.port] The port to connect to.
     * @param {string} [connectionInfo.database] The database to open with the connection.
     * @param {string} [connectionInfo.username] The username to supply with the connection.
     * @param {string} [connectionInfo.password] The password to supply with the connection.
     * @function MySQLDatabaseConnection#connect
     * @return {Promise}
     */
    /**
     * Connects to a MySQL database and utilizes the supplied callback when complete.
     * @param {IRelationalDatabaseConnection~connectCallback} callback The node.js style callback to use in conjunction with the connection.
     * @function MySQLDatabaseConnection#connect
     */
    /**
     * Connects to a MySQL database with the supplied connection info and utilizes the supplied callback when complete.
     * @param connectionInfo The connection info to utilize for the connection.
     * @param {string} [connectionInfo.host] The host to connect to.
     * @param {number} [connectionInfo.port] The port to connect to.
     * @param {string} [connectionInfo.database] The database to open with the connection.
     * @param {string} [connectionInfo.username] The username to supply with the connection.
     * @param {string} [connectionInfo.password] The password to supply with the connection.
     * @param {IRelationalDatabaseConnection~connectCallback} callback The node.js style callback to use in conjunction with the connection.
     * @function MySQLDatabaseConnection#connect
     */
    connect(connectionInfo, callback) {
        if (arguments.length > 0) {
            if (arguments.length > 1) {
                _applyConnectionInfo.bind(this)();
                this._connection.connect(callback);
            } else if (arguments[0] instanceof Function) {
                this._connection.connect(callback);
            } else {
                return (new Promise((resolve, reject) => {
                    _applyConnectionInfo.bind(this)();
                    this._connection.connect((err) => { if (err) { reject(err); } else { resolve(); } })
                }));
            }
        } else {
            return (new Promise((resolve, reject) => {
                _applyConnectionInfo.bind(this)();
                this._connection.connect((err) => { if (err) { reject(err); } else { resolve(); } })
            }));
        }
    }
}
module.exports = MySQLDatabaseConnection;