allow min/max params for pg-pool

This commit is contained in:
caub 2017-07-01 11:32:37 +02:00
parent 45d82320b7
commit 9ab62ff9f3
5 changed files with 34 additions and 25 deletions

1
.gitignore vendored
View File

@ -23,3 +23,4 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
package-lock.json

View File

@ -1,3 +1,5 @@
language: node_js
node_js:
- '0.10'
- '6.9'
- '8'

View File

@ -8,18 +8,20 @@ var url = require('url');
//parses a connection string
function parse(str) {
var config;
//unix socket
if(str.charAt(0) === '/') {
config = str.split(' ');
var config = str.split(' ');
return { host: config[0], database: config[1] };
}
// url parse expects spaces encoded as %20
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
str = encodeURI(str).replace(/\%25(\d\d)/g, "%$1");
var result = url.parse(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str) ? encodeURI(str).replace(/\%25(\d\d)/g, "%$1") : str, true);
var config = result.query;
for (var k in config) {
if (Array.isArray(config[k])) {
config[k] = config[k][config[k].length-1];
}
}
var result = url.parse(str, true);
config = {};
config.port = result.port;
if(result.protocol == 'socket:') {
@ -42,26 +44,14 @@ function parse(str) {
config.user = auth[0];
config.password = auth.splice(1).join(':');
var ssl = result.query.ssl;
if (ssl === 'true' || ssl === '1') {
if (config.ssl === 'true' || config.ssl === '1') {
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;
}
module.exports = {
parse: parse
};
module.exports = parse;
parse.parse = parse;

View File

@ -25,6 +25,6 @@
"homepage": "https://github.com/iceddev/pg-connection-string",
"dependencies": {},
"devDependencies": {
"tap": "^0.4.11"
"tap": "^10.3.3"
}
}

View File

@ -160,6 +160,20 @@ test('configuration parameter ssl=1', function(t){
t.end();
});
test('set ssl', function (t) {
var subject = parse('pg://myhost/db?ssl=1');
t.equal(subject.ssl, true);
t.end();
});
test('allow other params like max, ...', function (t) {
var subject = parse('pg://myhost/db?max=18&min=4');
t.equal(subject.max, '18');
t.equal(subject.min, '4');
t.end();
});
test('configuration parameter keepalives', function(t){
var connectionString = 'pg:///?keepalives=1';
var subject = parse(connectionString);
@ -182,9 +196,11 @@ test('do not override a config field with value from query string', function(t){
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();
});
});