diff --git a/index.ts b/index.ts index fe06bc8..f4f7a4e 100644 --- a/index.ts +++ b/index.ts @@ -55,7 +55,7 @@ const dialects = { */ export function patchKnex( knex: Knex, - configFn: (config: Knex.Config) => Knex.ConnectionConfig + configFn: (config: Knex.Config) => Knex.ConnectionConfig | Promise ): void { const client = knex.client const clientName = resolveClientNameWithAliases(client.config.client) @@ -90,8 +90,9 @@ export function patchKnex( /** * Returns a dynamic connection to be used for each query */ - client.getRuntimeConnectionSettings = - function getRuntimeConnectionSettings(): Knex.ConnectionConfig { - return configFn(this.config) - } + client.getRuntimeConnectionSettings = function getRuntimeConnectionSettings(): + | Knex.ConnectionConfig + | Promise { + return configFn(this.config) + } } diff --git a/src/dialects/mssql.ts b/src/dialects/mssql.ts index de032b4..7b8aa61 100644 --- a/src/dialects/mssql.ts +++ b/src/dialects/mssql.ts @@ -94,12 +94,12 @@ function generateConnection(settings: any) { * Copy of `acquireRawConnection` from knex codebase, but instead relies * on `getRuntimeConnectionSettings` vs `connectionSettings` */ -export function acquireRawConnection(): Promise { +export async function acquireRawConnection(): Promise { + debug('connection::connection new connection requested') + const connectionSettings = await this.getRuntimeConnectionSettings() return new Promise((resolver, rejecter) => { - debug('connection::connection new connection requested') - const Driver = this._driver() - const settings = Object.assign({}, generateConnection(this.getRuntimeConnectionSettings())) + const settings = Object.assign({}, generateConnection(connectionSettings)) const connection = new Driver.Connection(settings) diff --git a/src/dialects/mysql.ts b/src/dialects/mysql.ts index 98ae0df..09fd8c5 100644 --- a/src/dialects/mysql.ts +++ b/src/dialects/mysql.ts @@ -11,9 +11,10 @@ * Copy of `acquireRawConnection` from knex codebase, but instead relies * on `getRuntimeConnectionSettings` vs `connectionSettings` */ -export function acquireRawConnection(): Promise { +export async function acquireRawConnection(): Promise { + const connectionSettings = await this.getRuntimeConnectionSettings() return new Promise((resolver, rejecter) => { - const connection = this.driver.createConnection(this.getRuntimeConnectionSettings()) + const connection = this.driver.createConnection(connectionSettings) connection.on('error', (err: Error) => { connection.__knex__disposed = err }) diff --git a/src/dialects/oracledb.ts b/src/dialects/oracledb.ts index 9cb3e29..04133cc 100644 --- a/src/dialects/oracledb.ts +++ b/src/dialects/oracledb.ts @@ -91,12 +91,11 @@ function readStream(stream, type) { * Copy of `acquireRawConnection` from knex codebase, but instead relies * on `getRuntimeConnectionSettings` vs `connectionSettings` */ -export function acquireRawConnection(): Promise { +export async function acquireRawConnection(): Promise { const client = this + const settings = await client.getRuntimeConnectionSettings() const asyncConnection = new Promise(function (resolver, rejecter) { - const settings = client.getRuntimeConnectionSettings() - // If external authentication don't have to worry about username/password and // if not need to set the username and password const oracleDbConfig = settings.externalAuth diff --git a/src/dialects/pg.ts b/src/dialects/pg.ts index 953dc6e..6844510 100644 --- a/src/dialects/pg.ts +++ b/src/dialects/pg.ts @@ -13,9 +13,9 @@ * on `getRuntimeConnectionSettings` vs `connectionSettings` */ /* eslint no-shadow: "off" */ -export function acquireRawConnection(): Promise { +export async function acquireRawConnection(): Promise { const client = this - const connection = new client.driver.Client(client.getRuntimeConnectionSettings()) + const connection = new client.driver.Client(await client.getRuntimeConnectionSettings()) connection.on('error', (err: Error) => { connection.__knex__disposed = err })