mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Copy all but special-cased params from URL query string to config
This commit is contained in:
parent
07a7143fc9
commit
cdf06edd14
19
index.js
19
index.js
@ -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);
|
||||
@ -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) {
|
||||
var value = result.query[key];
|
||||
if (Array.isArray(value))
|
||||
value = value[value.length-1];
|
||||
config[key] = value;
|
||||
});
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user