Skip to content
7 changes: 2 additions & 5 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ function PostgreSQL(postgresql, settings) {
// this._models = {};
// this.settings = settings;
this.constructor.super_.call(this, 'postgresql', settings);
this.clientConfig = settings;
if (settings.url) {
// pg-pool doesn't handle string config correctly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we delete this comment too? I assume pg pool is handling it correctly now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 76? That is still accurate. Pg-pool expects a different key name than the pg module.

this.clientConfig = {
connectionString: settings.url,
};
} else {
this.clientConfig = settings;
this.clientConfig.connectionString = settings.url;
}
this.clientConfig.Promise = Promise;
this.pg = new postgresql.Pool(this.clientConfig);
Expand Down
54 changes: 54 additions & 0 deletions test/postgresql.initialization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright IBM Corp. 2015. All Rights Reserved.
// Node module: loopback-connector-postgresql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

'use strict';
require('./init');

var connector = require('..');
var DataSource = require('loopback-datasource-juggler').DataSource;
var should = require('should');

// simple wrapper that uses JSON.parse(JSON.stringify()) as cheap clone
function newConfig(withURL) {
return JSON.parse(JSON.stringify(getDBConfig(withURL)));
}

describe('initialization', function() {
it('honours user-defined pg-pool settings', function() {
var dataSource = new DataSource(connector, newConfig());
var pool = dataSource.connector.pg.pool;
pool._factory.max.should.not.equal(999);

var settings = newConfig();
settings.max = 999; // non-default value
var dataSource = new DataSource(connector, settings);
var pool = dataSource.connector.pg.pool;
pool._factory.max.should.equal(999);
});

it('honours user-defined url settings', function() {
var settings = newConfig();

var dataSource = new DataSource(connector, settings);
var clientConfig = dataSource.connector.clientConfig;
should.not.exist(clientConfig.connectionString);

settings = newConfig(true);
var dataSource = new DataSource(connector, settings);
var clientConfig = dataSource.connector.clientConfig;
clientConfig.connectionString.should.equal(settings.url);
});

it('honours multiple user-defined settings', function() {
var urlOnly = {url: newConfig(true).url, max: 999};

var dataSource = new DataSource(connector, urlOnly);
var pool = dataSource.connector.pg.pool;
pool._factory.max.should.equal(999);

var clientConfig = dataSource.connector.clientConfig;
clientConfig.connectionString.should.equal(urlOnly.url);
});
});