Skip to content

Copy all but special-cased params from URL query string to config #7

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

Merged
merged 1 commit into from
Jun 27, 2017
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
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ function parse(str) {
var result = url.parse(str, true);
config = {};

if (result.query.application_name) {
config.application_name = result.query.application_name;
}
if (result.query.fallback_application_name) {
config.fallback_application_name = result.query.fallback_application_name;
}

config.port = result.port;
if(result.protocol == 'socket:') {
config.host = decodeURI(result.pathname);
Expand All @@ -54,6 +47,18 @@ function parse(str) {
config.ssl = true;
}

['db', 'database', 'encoding', 'client_encoding', 'host', 'port', 'user', 'password', 'ssl']
.forEach(function(key) {
delete result.query[key];
});

Object.getOwnPropertyNames(result.query).forEach(function(key) {
Copy link

@elmigranto elmigranto Aug 31, 2016

Choose a reason for hiding this comment

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

Object.getOwnPropertyNames is not available in 0.10 and 0.12, so probably change it to Object.keys?

var value = result.query[key];
if (Array.isArray(value))
value = value[value.length-1];
config[key] = value;
});

return config;
}

Expand Down
64 changes: 64 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,67 @@ test('pathname of "/" returns null database', function (t) {

t.end();
});

test('configuration parameter application_name', function(t){
var connectionString = 'pg:///?application_name=TheApp';
var subject = parse(connectionString);
t.equal(subject.application_name, 'TheApp');
t.end();
});

test('configuration parameter fallback_application_name', function(t){
var connectionString = 'pg:///?fallback_application_name=TheAppFallback';
var subject = parse(connectionString);
t.equal(subject.fallback_application_name, 'TheAppFallback');
t.end();
});

test('configuration parameter fallback_application_name', function(t){
var connectionString = 'pg:///?fallback_application_name=TheAppFallback';
var subject = parse(connectionString);
t.equal(subject.fallback_application_name, 'TheAppFallback');
t.end();
});

test('configuration parameter ssl=true', function(t){
var connectionString = 'pg:///?ssl=true';
var subject = parse(connectionString);
t.equal(subject.ssl, true);
t.end();
});

test('configuration parameter ssl=1', function(t){
var connectionString = 'pg:///?ssl=1';
var subject = parse(connectionString);
t.equal(subject.ssl, true);
t.end();
});

test('configuration parameter keepalives', function(t){
var connectionString = 'pg:///?keepalives=1';
var subject = parse(connectionString);
t.equal(subject.keepalives, '1');
t.end();
});

test('unknown configuration parameter is passed into client', function(t){
var connectionString = 'pg:///?ThereIsNoSuchPostgresParameter=1234';
var subject = parse(connectionString);
t.equal(subject.ThereIsNoSuchPostgresParameter, '1234');
t.end();
});

test('do not override a config field with value from query string', function(t){
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus');
t.equal(subject.host, '/some path/');
t.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
t.equal(subject.client_encoding, 'utf8');
t.end();
});

test('return last value of repeated parameter', function(t){
var connectionString = 'pg:///?keepalives=1&keepalives=0';
var subject = parse(connectionString);
t.equal(subject.keepalives, '0');
t.end();
});