/*
 * knex-dynamic-connection
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Copy of `acquireRawConnection` from knex codebase, but instead relies
 * on `getRuntimeConnectionSettings` vs `connectionSettings`
 */
async function acquireRawConnection() {
  const connectionSettings = await this.getRuntimeConnectionSettings()
  return new Promise((resolver, rejecter) => {
    const connection = this.driver.createConnection(connectionSettings)
    connection.on('error', (err) => {
      connection.__knex__disposed = err
    })

    connection.connect((err) => {
      if (err) {
        // if connection is rejected, remove listener that was registered above...
        connection.removeAllListeners()
        return rejecter(err)
      }

      resolver(connection)
    })
  })
}

exports.acquireRawConnection = acquireRawConnection
