function connect (config, callback) {
  if (!globalConnection) {
    globalConnection = new shared.driver.ConnectionPool(config)

    for (const event in globalConnectionHandlers) {
      for (let i = 0, l = globalConnectionHandlers[event].length; i < l; i++) {
        globalConnection.on(event, globalConnectionHandlers[event][i])
      }
    }

    const ogClose = globalConnection.close

    const globalClose = function (callback) {
      // remove event handlers from the global connection
      for (const event in globalConnectionHandlers) {
        for (let i = 0, l = globalConnectionHandlers[event].length; i < l; i++) {
          this.removeListener(event, globalConnectionHandlers[event][i])
        }
      }

      // attach error handler to prevent process crash in case of error
      this.on('error', err => {
        if (globalConnectionHandlers.error) {
          for (let i = 0, l = globalConnectionHandlers.error.length; i < l; i++) {
            globalConnectionHandlers.error[i].call(this, err)
          }
        }
      })

      globalConnection = null
      return ogClose.call(this, callback)
    }

    globalConnection.close = globalClose.bind(globalConnection)
  }
  if (typeof callback === 'function') {
    return globalConnection.connect((err, connection) => {
      if (err) {
        globalConnection = null
      }
      callback(err, connection)
    })
  }
  return globalConnection.connect().catch((err) => {
    globalConnection = null
    return shared.Promise.reject(err)
  })
}
/**
 * Open global connection pool.
 *
 * @param {Object|String} config Connection configuration object or connection string.
 * @param {basicCallback} [callback] A callback which is called after connection has established, or an error has occurred. If omited, method returns Promise.
 * @return {Promise.<ConnectionPool>}
 */
Open global connection pool.
@param {Object|String} config Connection configuration object or connection string.
@param {basicCallback} [callback] A callback which is called after connection has established, or an error has occurred. If omited, method returns Promise.
@return {Promise.}
Open global connection pool . @param { Object|String } config Connection configuration object or connection string . @param { basicCallback } [ callback ] A callback which is called after connection has established or an error has occurred . If omited method returns Promise . @return { Promise . }
function connect (config, callback) {
  if (!globalConnection) {
    globalConnection = new shared.driver.ConnectionPool(config)
    for (const event in globalConnectionHandlers) {
      for (let i = 0, l = globalConnectionHandlers[event].length; i < l; i++) {
        globalConnection.on(event, globalConnectionHandlers[event][i])
      }
    }
    const ogClose = globalConnection.close
    const globalClose = function (callback) {
      for (const event in globalConnectionHandlers) {
        for (let i = 0, l = globalConnectionHandlers[event].length; i < l; i++) {
          this.removeListener(event, globalConnectionHandlers[event][i])
        }
      }
      this.on('error', err => {
        if (globalConnectionHandlers.error) {
          for (let i = 0, l = globalConnectionHandlers.error.length; i < l; i++) {
            globalConnectionHandlers.error[i].call(this, err)
          }
        }
      })
      globalConnection = null
      return ogClose.call(this, callback)
    }
    globalConnection.close = globalClose.bind(globalConnection)
  }
  if (typeof callback === 'function') {
    return globalConnection.connect((err, connection) => {
      if (err) {
        globalConnection = null
      }
      callback(err, connection)
    })
  }
  return globalConnection.connect().catch((err) => {
    globalConnection = null
    return shared.Promise.reject(err)
  })
}
function connect ( config , callback ) { if ( ! globalConnection ) { globalConnection = new shared . driver . ConnectionPool ( config ) for ( const event in globalConnectionHandlers ) { for ( let i = 0 , l = globalConnectionHandlers [ event ] . length ; i < l ; i ++ ) { globalConnection . on ( event , globalConnectionHandlers [ event ] [ i ] ) } } const ogClose = globalConnection . close const globalClose = function ( callback ) { for ( const event in globalConnectionHandlers ) { for ( let i = 0 , l = globalConnectionHandlers [ event ] . length ; i < l ; i ++ ) { this . removeListener ( event , globalConnectionHandlers [ event ] [ i ] ) } } this . on ( 'error' , err => { if ( globalConnectionHandlers . error ) { for ( let i = 0 , l = globalConnectionHandlers . error . length ; i < l ; i ++ ) { globalConnectionHandlers . error [ i ] . call ( this , err ) } } } ) globalConnection = null return ogClose . call ( this , callback ) } globalConnection . close = globalClose . bind ( globalConnection ) } if ( typeof callback === 'function' ) { return globalConnection . connect ( ( err , connection ) => { if ( err ) { globalConnection = null } callback ( err , connection ) } ) } return globalConnection . connect ( ) . catch ( ( err ) => { globalConnection = null return shared . Promise . reject ( err ) } ) }
Open global connection pool.
Open global connection pool .
// remove event handlers from the global connection // attach error handler to prevent process crash in case of error