Set database on socket string connection

Allows to conect to a specific database trough this ways:
pg.connect('/some/path database', callback);
pg.connect('socket:/some/path?db=database', callback)
pg.connect('socket:/some/path?db=database&encoding=utf8', callback)
This commit is contained in:
Aurélio A. Heckert 2013-12-19 15:37:26 -03:00
parent 1e91fa5cab
commit c0fd4b1431
2 changed files with 46 additions and 4 deletions

View File

@ -12,16 +12,24 @@ var val = function(key, config) {
var url = require('url');
//parses a connection string
var parse = function(str) {
var config;
//unix socket
if(str.charAt(0) === '/') {
return { host: str };
config = str.split(' ');
return { host: config[0], database: config[1] };
}
// url parse expects spaces encoded as %20
str = encodeURI(str);
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) str = encodeURI(str);
var result = url.parse(str, true);
var config = {};
config = {};
if(result.protocol == 'socket:') {
config.host = decodeURI(result.pathname);
config.database = result.query.db;
config.client_encoding = result.query.encoding;
return config;
}
config.host = result.hostname;
config.database = result.pathname ? result.pathname.slice(1) : null;
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
var auth = (result.auth || ':').split(':');
config.user = auth[0];
config.password = auth[1];

View File

@ -47,10 +47,44 @@ test('ConnectionParameters initializing from config', function() {
assert.ok(subject.isDomainSocket === false);
});
test('escape spaces if present', function() {
subject = new ConnectionParameters('postgres://localhost/post gres');
assert.equal(subject.database, 'post gres');
});
test('do not double escape spaces', function() {
subject = new ConnectionParameters('postgres://localhost/post%20gres');
assert.equal(subject.database, 'post gres');
});
test('initializing with unix domain socket', function() {
var subject = new ConnectionParameters('/var/run/');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/var/run/');
assert.equal(subject.database, defaults.user);
});
test('initializing with unix domain socket and a specific database, the simple way', function() {
var subject = new ConnectionParameters('/var/run/ mydb');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/var/run/');
assert.equal(subject.database, 'mydb');
});
test('initializing with unix domain socket, the health way', function() {
var subject = new ConnectionParameters('socket:/some path/?db=my[db]&encoding=utf8');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/some path/');
assert.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
assert.equal(subject.client_encoding, 'utf8');
});
test('initializing with unix domain socket, the escaped health way', function() {
var subject = new ConnectionParameters('socket:/some%20path/?db=my%2Bdb&encoding=utf8');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/some path/');
assert.equal(subject.database, 'my+db');
assert.equal(subject.client_encoding, 'utf8');
});
test('libpq connection string building', function() {