Skip to content
Merged
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
6 changes: 6 additions & 0 deletions lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ var useSsl = function() {
};

var ConnectionParameters = function(config) {
//if a string is passed, it is a raw connection string so we parse it into a config
config = typeof config == 'string' ? parse(config) : (config || {});
//if the config has a connectionString defined, parse IT into the config we use
//this will override other default values with what is stored in connectionString
if(config.connectionString) {
config = parse(config.connectionString);
}
this.user = val('user', config);
this.database = val('database', config);
this.port = parseInt(val('port', config), 10);
Expand Down
5 changes: 5 additions & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var defaults = module.exports = {
//database user's password
password: null,

// a Postgres connection string to be used instead of setting individual connection items
// NOTE: Setting this value will cause it to override any other value (such as database or user) defined
// in the defaults object.
connectionString : undefined,

//database port
port: 5432,

Expand Down
19 changes: 19 additions & 0 deletions test/unit/connection-parameters/creation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ test('ConnectionParameters initializing from defaults', function() {
assert.ok(subject.isDomainSocket === false);
});

test('ConnectionParameters initializing from defaults with connectionString set', function() {
var config = {
user : 'brians-are-the-best',
database : 'scoobysnacks',
port : 7777,
password : 'mypassword',
host : 'foo.bar.net',
binary : defaults.binary
};

var original_value = defaults.connectionString;
// Just changing this here doesn't actually work because it's no longer in scope when viewed inside of
// of ConnectionParameters() so we have to pass in the defaults explicitly to test it
defaults.connectionString = 'postgres://brians-are-the-best:[email protected]:7777/scoobysnacks';
var subject = new ConnectionParameters(defaults);
defaults.connectionString = original_value;
compare(subject, config, 'defaults-connectionString');
});

test('ConnectionParameters initializing from config', function() {
var config = {
user: 'brian',
Expand Down