Skip to content

Commit e6643e4

Browse files
authored
Merge pull request #7 from motiz88/extra-keys
Copy all but special-cased params from URL query string to config
2 parents 07a7143 + cdf06ed commit e6643e4

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

index.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ function parse(str) {
2121
var result = url.parse(str, true);
2222
config = {};
2323

24-
if (result.query.application_name) {
25-
config.application_name = result.query.application_name;
26-
}
27-
if (result.query.fallback_application_name) {
28-
config.fallback_application_name = result.query.fallback_application_name;
29-
}
30-
3124
config.port = result.port;
3225
if(result.protocol == 'socket:') {
3326
config.host = decodeURI(result.pathname);
@@ -54,6 +47,18 @@ function parse(str) {
5447
config.ssl = true;
5548
}
5649

50+
['db', 'database', 'encoding', 'client_encoding', 'host', 'port', 'user', 'password', 'ssl']
51+
.forEach(function(key) {
52+
delete result.query[key];
53+
});
54+
55+
Object.getOwnPropertyNames(result.query).forEach(function(key) {
56+
var value = result.query[key];
57+
if (Array.isArray(value))
58+
value = value[value.length-1];
59+
config[key] = value;
60+
});
61+
5762
return config;
5863
}
5964

test/parse.js

+64
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,67 @@ test('pathname of "/" returns null database', function (t) {
124124

125125
t.end();
126126
});
127+
128+
test('configuration parameter application_name', function(t){
129+
var connectionString = 'pg:///?application_name=TheApp';
130+
var subject = parse(connectionString);
131+
t.equal(subject.application_name, 'TheApp');
132+
t.end();
133+
});
134+
135+
test('configuration parameter fallback_application_name', function(t){
136+
var connectionString = 'pg:///?fallback_application_name=TheAppFallback';
137+
var subject = parse(connectionString);
138+
t.equal(subject.fallback_application_name, 'TheAppFallback');
139+
t.end();
140+
});
141+
142+
test('configuration parameter fallback_application_name', function(t){
143+
var connectionString = 'pg:///?fallback_application_name=TheAppFallback';
144+
var subject = parse(connectionString);
145+
t.equal(subject.fallback_application_name, 'TheAppFallback');
146+
t.end();
147+
});
148+
149+
test('configuration parameter ssl=true', function(t){
150+
var connectionString = 'pg:///?ssl=true';
151+
var subject = parse(connectionString);
152+
t.equal(subject.ssl, true);
153+
t.end();
154+
});
155+
156+
test('configuration parameter ssl=1', function(t){
157+
var connectionString = 'pg:///?ssl=1';
158+
var subject = parse(connectionString);
159+
t.equal(subject.ssl, true);
160+
t.end();
161+
});
162+
163+
test('configuration parameter keepalives', function(t){
164+
var connectionString = 'pg:///?keepalives=1';
165+
var subject = parse(connectionString);
166+
t.equal(subject.keepalives, '1');
167+
t.end();
168+
});
169+
170+
test('unknown configuration parameter is passed into client', function(t){
171+
var connectionString = 'pg:///?ThereIsNoSuchPostgresParameter=1234';
172+
var subject = parse(connectionString);
173+
t.equal(subject.ThereIsNoSuchPostgresParameter, '1234');
174+
t.end();
175+
});
176+
177+
test('do not override a config field with value from query string', function(t){
178+
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus');
179+
t.equal(subject.host, '/some path/');
180+
t.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
181+
t.equal(subject.client_encoding, 'utf8');
182+
t.end();
183+
});
184+
185+
test('return last value of repeated parameter', function(t){
186+
var connectionString = 'pg:///?keepalives=1&keepalives=0';
187+
var subject = parse(connectionString);
188+
t.equal(subject.keepalives, '0');
189+
t.end();
190+
});

0 commit comments

Comments
 (0)