Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection config provider #3403

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
88f69a5
#3340 Connection settings can be an async function only for PG client
Ali-Dalal Jul 20, 2019
ccbde75
connection config provider
Ali-Dalal Aug 19, 2019
d2cbf1a
change default value of resolvedConnectionSettings to null in connect…
Ali-Dalal Aug 19, 2019
a8aa0af
Merge branch 'master' into connection-config-provider
Ali-Dalal Aug 19, 2019
79a2660
Rename connection.js to connectionProvider.js
Ali-Dalal Aug 26, 2019
7d1b8f5
fix an issue in ConnectionConfigProvider
Ali-Dalal Aug 26, 2019
a2427b4
remove console.log
Ali-Dalal Aug 26, 2019
d219159
enable configProvider for sqllite
Ali-Dalal Aug 26, 2019
400b829
enable config provider for mssql
Ali-Dalal Aug 26, 2019
cc87967
enable config provider for mysql
Ali-Dalal Aug 26, 2019
4050772
enable config provider for oracle
Ali-Dalal Aug 26, 2019
ddae371
enable config provider for oracledb
Ali-Dalal Aug 26, 2019
37c805c
Merge branch 'master' into connection-config-provider
Ali-Dalal Aug 26, 2019
1ecd885
remove comments from client.js
Ali-Dalal Aug 26, 2019
a4df60d
remove comments from mssql/index.js
Ali-Dalal Aug 26, 2019
1c6eff4
Merge branch 'master' into connection-config-provider
Ali-Dalal Oct 15, 2019
5484292
add await in oracledb dialects test
Ali-Dalal Oct 15, 2019
2b6460d
remove unused variables (clonedeeb) from client.js
Ali-Dalal Oct 15, 2019
42d7d41
fix eslint issues
Ali-Dalal Oct 15, 2019
7059d5f
add tests
Ali-Dalal Oct 16, 2019
2837c15
update postgres unit test
Ali-Dalal Oct 16, 2019
6d0da47
update postgres unit test
Ali-Dalal Oct 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/client.js
Expand Up @@ -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');
Expand Down Expand Up @@ -53,8 +54,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
Ali-Dalal marked this conversation as resolved.
Show resolved Hide resolved
Ali-Dalal marked this conversation as resolved.
Show resolved Hide resolved
if (typeof config.connection === 'function') {
Ali-Dalal marked this conversation as resolved.
Show resolved Hide resolved
Ali-Dalal marked this conversation as resolved.
Show resolved Hide resolved
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)) {
Expand Down Expand Up @@ -122,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;
Expand Down
18 changes: 18 additions & 0 deletions lib/connection.js
@@ -0,0 +1,18 @@
class Connection {
Ali-Dalal marked this conversation as resolved.
Show resolved Hide resolved
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;
4 changes: 3 additions & 1 deletion lib/dialects/postgres/index.js
Expand Up @@ -21,6 +21,7 @@ function Client_PG(config) {
this.searchPath = config.searchPath;
}
}

inherits(Client_PG, Client);

Object.assign(Client_PG.prototype, {
Expand Down Expand Up @@ -105,7 +106,8 @@ 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) {
await client.connection().resolveConnectionConfig();
Ali-Dalal marked this conversation as resolved.
Show resolved Hide resolved
const connection = new client.driver.Client(client.connectionSettings);
connection.connect(function(err, connection) {
if (err) {
Expand Down