From 88f69a510223b73853f02aab2ad80dba20875616 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Sat, 20 Jul 2019 11:52:55 +0800 Subject: [PATCH 01/19] #3340 Connection settings can be an async function only for PG client --- lib/client.js | 8 ++++++-- lib/dialects/postgres/index.js | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 0021072647..b29d1b12c3 100644 --- a/lib/client.js +++ b/lib/client.js @@ -53,8 +53,12 @@ function Client(config = {}) { if (config.version) { this.version = config.version; } - - this.connectionSettings = cloneDeep(config.connection || {}); + // if the type of connection settings is a function, pass it to the next layer + if (typeof config.connection === 'function') { + this.connectionSettings = config.connection; + } else { + this.connectionSettings = cloneDeep(config.connection || {}); + } if (this.driverName && config.connection) { this.initializeDriver(); if (!config.pool || (config.pool && config.pool.max !== 0)) { diff --git a/lib/dialects/postgres/index.js b/lib/dialects/postgres/index.js index f4cba0e56c..baedcf72e3 100644 --- a/lib/dialects/postgres/index.js +++ b/lib/dialects/postgres/index.js @@ -105,7 +105,9 @@ Object.assign(Client_PG.prototype, { // connection needs to be added to the pool. acquireRawConnection() { const client = this; - return new Bluebird(function(resolver, rejecter) { + return new Bluebird(async function(resolver, rejecter) { + if (typeof client.connectionSettings === 'function') + client.connectionSettings = await client.connectionSettings(); const connection = new client.driver.Client(client.connectionSettings); connection.connect(function(err, connection) { if (err) { From ccbde7512581533a4a4191d5672b9850d4efd863 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 19 Aug 2019 11:23:17 +0800 Subject: [PATCH 02/19] connection config provider --- lib/client.js | 5 +++++ lib/connection.js | 18 ++++++++++++++++++ lib/dialects/postgres/index.js | 4 ++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/connection.js diff --git a/lib/client.js b/lib/client.js index b29d1b12c3..cf247bb10b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -15,6 +15,7 @@ const TableBuilder = require('./schema/tablebuilder'); const TableCompiler = require('./schema/tablecompiler'); const ColumnBuilder = require('./schema/columnbuilder'); const ColumnCompiler = require('./schema/columncompiler'); +const Connection = require('./connection'); const { Pool, TimeoutError } = require('tarn'); const inherits = require('inherits'); @@ -126,6 +127,10 @@ Object.assign(Client.prototype, { return new Ref(this, ...arguments); }, + connection() { + return new Connection(this); + }, + _formatQuery(sql, bindings, timeZone) { bindings = bindings == null ? [] : [].concat(bindings); let index = 0; diff --git a/lib/connection.js b/lib/connection.js new file mode 100644 index 0000000000..ae40a52ba3 --- /dev/null +++ b/lib/connection.js @@ -0,0 +1,18 @@ +class Connection { + constructor(client) { + this.client = client; + this.resolvedConnectionSettings = {}; + } + + async resolveConnectionConfig() { + if (typeof this.client.connectionSettings === 'function') { + // if the config is a function, execute the function and resolve the connection settings + if (!this.resolvedConnectionSettings) { + this.resolvedConnectionSettings = await this.client.connectionSettings(); + this.client.connectionSettings = this.resolvedConnectionSettings; + } + } + } +} + +module.exports = Connection; diff --git a/lib/dialects/postgres/index.js b/lib/dialects/postgres/index.js index baedcf72e3..eac83b3a0d 100644 --- a/lib/dialects/postgres/index.js +++ b/lib/dialects/postgres/index.js @@ -21,6 +21,7 @@ function Client_PG(config) { this.searchPath = config.searchPath; } } + inherits(Client_PG, Client); Object.assign(Client_PG.prototype, { @@ -106,8 +107,7 @@ Object.assign(Client_PG.prototype, { acquireRawConnection() { const client = this; return new Bluebird(async function(resolver, rejecter) { - if (typeof client.connectionSettings === 'function') - client.connectionSettings = await client.connectionSettings(); + await client.connection().resolvedConnectionSettings(); const connection = new client.driver.Client(client.connectionSettings); connection.connect(function(err, connection) { if (err) { From d2cbf1ab37b9126376316aca8d071b7baf275e2e Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 19 Aug 2019 11:43:37 +0800 Subject: [PATCH 03/19] change default value of resolvedConnectionSettings to null in connection.js --- lib/connection.js | 2 +- lib/dialects/postgres/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index ae40a52ba3..3e582ff49a 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1,7 +1,7 @@ class Connection { constructor(client) { this.client = client; - this.resolvedConnectionSettings = {}; + this.resolvedConnectionSettings = null; } async resolveConnectionConfig() { diff --git a/lib/dialects/postgres/index.js b/lib/dialects/postgres/index.js index eac83b3a0d..7d7e358c36 100644 --- a/lib/dialects/postgres/index.js +++ b/lib/dialects/postgres/index.js @@ -107,7 +107,7 @@ Object.assign(Client_PG.prototype, { acquireRawConnection() { const client = this; return new Bluebird(async function(resolver, rejecter) { - await client.connection().resolvedConnectionSettings(); + await client.connection().resolveConnectionConfig(); const connection = new client.driver.Client(client.connectionSettings); connection.connect(function(err, connection) { if (err) { From 79a2660287bf69981dd649532b93aa941dac40e0 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 10:31:11 +0800 Subject: [PATCH 04/19] Rename connection.js to connectionProvider.js Update client to leave the responsibility to determine connection settings type to connectionProvider Update postgres dialect aquireConnection function --- lib/client.js | 16 ++++++++-------- lib/connection.js | 18 ------------------ lib/connectionProvider.js | 25 +++++++++++++++++++++++++ lib/dialects/postgres/index.js | 6 ++++-- 4 files changed, 37 insertions(+), 28 deletions(-) delete mode 100644 lib/connection.js create mode 100644 lib/connectionProvider.js diff --git a/lib/client.js b/lib/client.js index cf247bb10b..754315be4c 100644 --- a/lib/client.js +++ b/lib/client.js @@ -15,7 +15,7 @@ const TableBuilder = require('./schema/tablebuilder'); const TableCompiler = require('./schema/tablecompiler'); const ColumnBuilder = require('./schema/columnbuilder'); const ColumnCompiler = require('./schema/columncompiler'); -const Connection = require('./connection'); +const ConnectionConfigProvider = require('./connectionProvider'); const { Pool, TimeoutError } = require('tarn'); const inherits = require('inherits'); @@ -55,11 +55,11 @@ function Client(config = {}) { this.version = config.version; } // if the type of connection settings is a function, pass it to the next layer - if (typeof config.connection === 'function') { - this.connectionSettings = config.connection; - } else { - this.connectionSettings = cloneDeep(config.connection || {}); - } + // if (typeof config.connection === 'function') { + // this.connectionSettings = config.connection; + // } else { + // this.connectionSettings = cloneDeep(config.connection || {}); + // } if (this.driverName && config.connection) { this.initializeDriver(); if (!config.pool || (config.pool && config.pool.max !== 0)) { @@ -127,8 +127,8 @@ Object.assign(Client.prototype, { return new Ref(this, ...arguments); }, - connection() { - return new Connection(this); + connectionConfigProvider() { + return new ConnectionConfigProvider(this); }, _formatQuery(sql, bindings, timeZone) { diff --git a/lib/connection.js b/lib/connection.js deleted file mode 100644 index 3e582ff49a..0000000000 --- a/lib/connection.js +++ /dev/null @@ -1,18 +0,0 @@ -class Connection { - constructor(client) { - this.client = client; - this.resolvedConnectionSettings = null; - } - - async resolveConnectionConfig() { - if (typeof this.client.connectionSettings === 'function') { - // if the config is a function, execute the function and resolve the connection settings - if (!this.resolvedConnectionSettings) { - this.resolvedConnectionSettings = await this.client.connectionSettings(); - this.client.connectionSettings = this.resolvedConnectionSettings; - } - } - } -} - -module.exports = Connection; diff --git a/lib/connectionProvider.js b/lib/connectionProvider.js new file mode 100644 index 0000000000..1d71130839 --- /dev/null +++ b/lib/connectionProvider.js @@ -0,0 +1,25 @@ +const { isFunction, cloneDeep } = require('lodash'); + +class ConnectionConfigProvider { + constructor(client) { + this.client = client; + this.resolvedConnectionSettings = null; + } + + async resolveConnectionConfig() { + if (isFunction(this.client.config.connection)) { + // if the config is a function, execute the function and resolve the connection settings + if (!this.resolvedConnectionSettings) { + this.resolvedConnectionSettings = await this.client.config.connection(); + this.client.connectionSettings = this.resolvedConnectionSettings; + } + } else { + this.client.connectionSettings = cloneDeep( + this.client.config.connection || {} + ); + } + return this.resolvedConnectionSettings; + } +} + +module.exports = ConnectionConfigProvider; diff --git a/lib/dialects/postgres/index.js b/lib/dialects/postgres/index.js index 7d7e358c36..5e3d57095b 100644 --- a/lib/dialects/postgres/index.js +++ b/lib/dialects/postgres/index.js @@ -107,8 +107,10 @@ Object.assign(Client_PG.prototype, { acquireRawConnection() { const client = this; return new Bluebird(async function(resolver, rejecter) { - await client.connection().resolveConnectionConfig(); - const connection = new client.driver.Client(client.connectionSettings); + const connectionSettings = await client + .connectionConfigProvider() + .resolveConnectionConfig(); + const connection = new client.driver.Client(connectionSettings); connection.connect(function(err, connection) { if (err) { return rejecter(err); From 7d1b8f5de93bf7ad2d4648ee5715f4011688f1df Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 10:41:13 +0800 Subject: [PATCH 05/19] fix an issue in ConnectionConfigProvider --- lib/connectionProvider.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/connectionProvider.js b/lib/connectionProvider.js index 1d71130839..355ea5b51e 100644 --- a/lib/connectionProvider.js +++ b/lib/connectionProvider.js @@ -7,17 +7,19 @@ class ConnectionConfigProvider { } async resolveConnectionConfig() { + if (this.resolvedConnectionSettings) return this.resolvedConnectionSettings; if (isFunction(this.client.config.connection)) { // if the config is a function, execute the function and resolve the connection settings - if (!this.resolvedConnectionSettings) { - this.resolvedConnectionSettings = await this.client.config.connection(); - this.client.connectionSettings = this.resolvedConnectionSettings; - } + this.resolvedConnectionSettings = await this.client.config.connection(); + this.client.connectionSettings = this.resolvedConnectionSettings; } else { this.client.connectionSettings = cloneDeep( this.client.config.connection || {} ); + this.resolvedConnectionSettings = this.client.connectionSettings; } + console.log('resolved connection:'); + console.log(this.resolvedConnectionSettings); return this.resolvedConnectionSettings; } } From a2427b4e9bfcb1ceb97a4844116a107c270de39b Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 11:55:13 +0800 Subject: [PATCH 06/19] remove console.log --- lib/connectionProvider.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/connectionProvider.js b/lib/connectionProvider.js index 355ea5b51e..8cb37df4e7 100644 --- a/lib/connectionProvider.js +++ b/lib/connectionProvider.js @@ -18,8 +18,6 @@ class ConnectionConfigProvider { ); this.resolvedConnectionSettings = this.client.connectionSettings; } - console.log('resolved connection:'); - console.log(this.resolvedConnectionSettings); return this.resolvedConnectionSettings; } } From d219159b15a0a8dd149a3c10cfa618958ea2d296 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 12:25:49 +0800 Subject: [PATCH 07/19] enable configProvider for sqllite --- lib/dialects/sqlite3/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/dialects/sqlite3/index.js b/lib/dialects/sqlite3/index.js index e02159ef92..7ecc05a4f6 100644 --- a/lib/dialects/sqlite3/index.js +++ b/lib/dialects/sqlite3/index.js @@ -62,9 +62,13 @@ Object.assign(Client_SQLite3.prototype, { // Get a raw connection from the database, returning a promise with the connection object. acquireRawConnection() { - return new Bluebird((resolve, reject) => { + const client = this; + return new Bluebird(async (resolve, reject) => { + const connectionSettings = await client + .connectionConfigProvider() + .resolveConnectionConfig(); const db = new this.driver.Database( - this.connectionSettings.filename, + connectionSettings.filename, (err) => { if (err) { return reject(err); From 400b82917d00f2fa6eeceeac2712a4183ba906af Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 12:31:58 +0800 Subject: [PATCH 08/19] enable config provider for mssql --- lib/dialects/mssql/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/dialects/mssql/index.js b/lib/dialects/mssql/index.js index 59d2ec0cae..ccb17b2aa2 100644 --- a/lib/dialects/mssql/index.js +++ b/lib/dialects/mssql/index.js @@ -213,8 +213,12 @@ Object.assign(Client_MSSQL.prototype, { // Get a raw connection, called by the `pool` whenever a new // connection needs to be added to the pool. acquireRawConnection() { - return new Bluebird((resolver, rejecter) => { - const settings = Object.assign({}, this.connectionSettings); + const client = this; + return new Bluebird(async (resolver, rejecter) => { + const settings = await client + .connectionConfigProvider() + .resolveConnectionConfig(); + // const settings = Object.assign({}, this.connectionSettings); settings.pool = this.mssqlPoolSettings; const connection = new this.driver.ConnectionPool(settings); From cc8796743562c59027289878897be5e65b1715f7 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 12:33:34 +0800 Subject: [PATCH 09/19] enable config provider for mysql --- lib/dialects/mysql/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/dialects/mysql/index.js b/lib/dialects/mysql/index.js index df94b0308c..a11e10fcfd 100644 --- a/lib/dialects/mysql/index.js +++ b/lib/dialects/mysql/index.js @@ -60,8 +60,12 @@ Object.assign(Client_MySQL.prototype, { // Get a raw connection, called by the `pool` whenever a new // connection needs to be added to the pool. acquireRawConnection() { - return new Bluebird((resolver, rejecter) => { - const connection = this.driver.createConnection(this.connectionSettings); + const client = this; + return new Bluebird(async (resolver, rejecter) => { + const connectionSettings = await client + .connectionConfigProvider() + .resolveConnectionConfig(); + const connection = this.driver.createConnection(connectionSettings); connection.on('error', (err) => { connection.__knex__disposed = err; }); From 40507724af07be0d40fb7b430bdf7b94a852b692 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 12:41:29 +0800 Subject: [PATCH 10/19] enable config provider for oracle --- lib/dialects/oracle/index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/dialects/oracle/index.js b/lib/dialects/oracle/index.js index 0cbd9e489a..bdb4db5a19 100644 --- a/lib/dialects/oracle/index.js +++ b/lib/dialects/oracle/index.js @@ -79,14 +79,16 @@ Object.assign(Client_Oracle.prototype, { // Get a raw connection, called by the `pool` whenever a new // connection needs to be added to the pool. acquireRawConnection() { - return new Bluebird((resolver, rejecter) => { - this.driver.connect(this.connectionSettings, (err, connection) => { + const client = this; + return new Bluebird(async (resolver, rejecter) => { + const connectionSettings = await client + .connectionConfigProvider() + .resolveConnectionConfig(); + this.driver.connect(connectionSettings, (err, connection) => { if (err) return rejecter(err); Bluebird.promisifyAll(connection); - if (this.connectionSettings.prefetchRowCount) { - connection.setPrefetchRowCount( - this.connectionSettings.prefetchRowCount - ); + if (connectionSettings.prefetchRowCount) { + connection.setPrefetchRowCount(connectionSettings.prefetchRowCount); } resolver(connection); }); From ddae371fe94c4c57feb61ace026a085c41f855f9 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 12:48:09 +0800 Subject: [PATCH 11/19] enable config provider for oracledb --- lib/dialects/oracledb/index.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/dialects/oracledb/index.js b/lib/dialects/oracledb/index.js index b75f340478..bb5f564173 100644 --- a/lib/dialects/oracledb/index.js +++ b/lib/dialects/oracledb/index.js @@ -76,27 +76,30 @@ Client_Oracledb.prototype.prepBindings = function(bindings) { // connection needs to be added to the pool. Client_Oracledb.prototype.acquireRawConnection = function() { const client = this; - const asyncConnection = new Bluebird(function(resolver, rejecter) { + const asyncConnection = new Bluebird(async function(resolver, rejecter) { // If external authentication dont have to worry about username/password and // if not need to set the username and password - const oracleDbConfig = client.connectionSettings.externalAuth - ? { externalAuth: client.connectionSettings.externalAuth } + const connectionSettings = await client + .connectionConfigProvider() + .resolveConnectionConfig(); + const oracleDbConfig = connectionSettings.externalAuth + ? { externalAuth: connectionSettings.externalAuth } : { - user: client.connectionSettings.user, - password: client.connectionSettings.password, + user: connectionSettings.user, + password: connectionSettings.password, }; // In the case of external authentication connection string will be given oracleDbConfig.connectString = - client.connectionSettings.connectString || - client.connectionSettings.host + '/' + client.connectionSettings.database; + connectionSettings.connectString || + connectionSettings.host + '/' + connectionSettings.database; - if (client.connectionSettings.prefetchRowCount) { - oracleDbConfig.prefetchRows = client.connectionSettings.prefetchRowCount; + if (connectionSettings.prefetchRowCount) { + oracleDbConfig.prefetchRows = connectionSettings.prefetchRowCount; } - if (!_.isUndefined(client.connectionSettings.stmtCacheSize)) { - oracleDbConfig.stmtCacheSize = client.connectionSettings.stmtCacheSize; + if (!_.isUndefined(connectionSettings.stmtCacheSize)) { + oracleDbConfig.stmtCacheSize = connectionSettings.stmtCacheSize; } client.driver.fetchAsString = client.fetchAsString; From 1ecd885d3910a4d73ee582108fff234e90dc54cc Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 21:29:35 +0800 Subject: [PATCH 12/19] remove comments from client.js --- lib/client.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/client.js b/lib/client.js index 754315be4c..b4eaead66d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -54,12 +54,7 @@ function Client(config = {}) { if (config.version) { this.version = config.version; } - // if the type of connection settings is a function, pass it to the next layer - // if (typeof config.connection === 'function') { - // this.connectionSettings = config.connection; - // } else { - // this.connectionSettings = cloneDeep(config.connection || {}); - // } + if (this.driverName && config.connection) { this.initializeDriver(); if (!config.pool || (config.pool && config.pool.max !== 0)) { From a4df60d7a2f857e45576922cbbe4845e53a874bd Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Mon, 26 Aug 2019 21:32:38 +0800 Subject: [PATCH 13/19] remove comments from mssql/index.js --- lib/dialects/mssql/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/dialects/mssql/index.js b/lib/dialects/mssql/index.js index ccb17b2aa2..d4bb70a075 100644 --- a/lib/dialects/mssql/index.js +++ b/lib/dialects/mssql/index.js @@ -218,7 +218,6 @@ Object.assign(Client_MSSQL.prototype, { const settings = await client .connectionConfigProvider() .resolveConnectionConfig(); - // const settings = Object.assign({}, this.connectionSettings); settings.pool = this.mssqlPoolSettings; const connection = new this.driver.ConnectionPool(settings); From 548429253255a011c23be7eecd84847ddfec7ccd Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Tue, 15 Oct 2019 18:28:41 +0800 Subject: [PATCH 14/19] add await in oracledb dialects test --- test/unit/dialects/oracledb.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/dialects/oracledb.js b/test/unit/dialects/oracledb.js index 48aaf427b2..bb750d3665 100644 --- a/test/unit/dialects/oracledb.js +++ b/test/unit/dialects/oracledb.js @@ -25,12 +25,12 @@ describe('OracleDb externalAuth', function() { spy = sinon.spy(knexInstance.client.driver, 'getConnection'); }); - it('externalAuth and connectString should be sent to the getConnection', function() { + it('externalAuth and connectString should be sent to the getConnection', async function() { const connectionWithExternalAuth = { connectString: 'connect-string', externalAuth: true, }; - knexInstance.client + await knexInstance.client .acquireRawConnection() .then(function(resolve) {}, function(reject) {}); expect(spy).to.have.callCount(1); From 2b6460d9f173c58c2f961f5c6f41c392c5382c2b Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Tue, 15 Oct 2019 19:14:15 +0800 Subject: [PATCH 15/19] remove unused variables (clonedeeb) from client.js --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 91c7c78c2c..d6c39404a6 100644 --- a/lib/client.js +++ b/lib/client.js @@ -23,7 +23,7 @@ const { EventEmitter } = require('events'); const { promisify } = require('util'); const { makeEscape } = require('./query/string'); -const { uniqueId, cloneDeep, defaults } = require('lodash'); +const { uniqueId, defaults } = require('lodash'); const Logger = require('./logger'); From 42d7d419c36976dd889ad213911e95278e29b5fa Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Tue, 15 Oct 2019 19:22:31 +0800 Subject: [PATCH 16/19] fix eslint issues --- lib/dialects/oracledb/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/dialects/oracledb/index.js b/lib/dialects/oracledb/index.js index 324d4c503a..06f1af09c6 100644 --- a/lib/dialects/oracledb/index.js +++ b/lib/dialects/oracledb/index.js @@ -103,6 +103,7 @@ Client_Oracledb.prototype.acquireRawConnection = function() { oracleDbConfig.stmtCacheSize = connectionSettings.stmtCacheSize; } + // eslint-disable-next-line require-atomic-updates client.driver.fetchAsString = client.fetchAsString; client.driver.getConnection(oracleDbConfig, function(err, connection) { From 7059d5f62388609db51b3487bc35803149d920b2 Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Wed, 16 Oct 2019 09:48:04 +0800 Subject: [PATCH 17/19] add tests --- test/knexfile.js | 16 ++++++++++++++++ test/unit/dialects/postgres.js | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/test/knexfile.js b/test/knexfile.js index a7ba7eecf3..f30ec75259 100644 --- a/test/knexfile.js +++ b/test/knexfile.js @@ -144,6 +144,22 @@ const testConfigs = { migrations, seeds, }, + asyncConfig: { + client: 'postgres', + connection: async function() { + return { + adapter: 'postgresql', + port: 25432, + host: 'localhost', + database: 'knex_test', + user: 'testuser', + password: 'knextest', + }; + }, + pool, + migrations, + seeds, + }, }; // export only copy the specified dialects diff --git a/test/unit/dialects/postgres.js b/test/unit/dialects/postgres.js index 6c881e762d..60871ffaaf 100644 --- a/test/unit/dialects/postgres.js +++ b/test/unit/dialects/postgres.js @@ -148,4 +148,18 @@ describe('Postgres Unit Tests', function() { done(); }); }); + + it('should configure knex connection as async function', (done) => { + const knexInstance = knex({ + client: 'pg', + connection: async function() { + return {}; + }, + }); + knexInstance.raw('select 1 as 1').then((result) => { + expect(checkVersionStub.calledOnce).to.equal(true); + knexInstance.destroy(); + done(); + }); + }); }); From 2837c1510689252e800b1221b1cf814fd7b4636f Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Wed, 16 Oct 2019 10:19:24 +0800 Subject: [PATCH 18/19] update postgres unit test --- test/unit/dialects/postgres.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/dialects/postgres.js b/test/unit/dialects/postgres.js index 60871ffaaf..a736f2584d 100644 --- a/test/unit/dialects/postgres.js +++ b/test/unit/dialects/postgres.js @@ -149,7 +149,7 @@ describe('Postgres Unit Tests', function() { }); }); - it('should configure knex connection as async function', (done) => { + it('should configure knex connection as async function', async () => { const knexInstance = knex({ client: 'pg', connection: async function() { @@ -159,7 +159,6 @@ describe('Postgres Unit Tests', function() { knexInstance.raw('select 1 as 1').then((result) => { expect(checkVersionStub.calledOnce).to.equal(true); knexInstance.destroy(); - done(); }); }); }); From 6d0da47a967080198efb0cb38f39048638f2a34f Mon Sep 17 00:00:00 2001 From: Ali Dalal Date: Wed, 16 Oct 2019 10:48:44 +0800 Subject: [PATCH 19/19] update postgres unit test --- test/unit/dialects/postgres.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/unit/dialects/postgres.js b/test/unit/dialects/postgres.js index a736f2584d..01d48df209 100644 --- a/test/unit/dialects/postgres.js +++ b/test/unit/dialects/postgres.js @@ -153,12 +153,15 @@ describe('Postgres Unit Tests', function() { const knexInstance = knex({ client: 'pg', connection: async function() { - return {}; + return { + host: 'localhost', + }; }, }); - knexInstance.raw('select 1 as 1').then((result) => { - expect(checkVersionStub.calledOnce).to.equal(true); - knexInstance.destroy(); + await knexInstance.raw('select 1 as 1'); + expect(knexInstance.client.connectionSettings).to.deep.equal({ + host: 'localhost', }); + knexInstance.destroy(); }); });