Merge pull request #487 from aurium/master
Set database on socket string connection
This commit is contained in:
commit
aa329920ab
@ -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];
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user