Skip to content

DO NOT MERGE: Rebased and cleaned up version of #186 #216

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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);
});
});