Skip to content

Commit aa32992

Browse files
committed
Merge pull request #487 from aurium/master
Set database on socket string connection
2 parents 1884feb + c0fd4b1 commit aa32992

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

lib/connection-parameters.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ var val = function(key, config) {
1212
var url = require('url');
1313
//parses a connection string
1414
var parse = function(str) {
15+
var config;
1516
//unix socket
1617
if(str.charAt(0) === '/') {
17-
return { host: str };
18+
config = str.split(' ');
19+
return { host: config[0], database: config[1] };
1820
}
1921
// url parse expects spaces encoded as %20
20-
str = encodeURI(str);
22+
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) str = encodeURI(str);
2123
var result = url.parse(str, true);
22-
var config = {};
24+
config = {};
25+
if(result.protocol == 'socket:') {
26+
config.host = decodeURI(result.pathname);
27+
config.database = result.query.db;
28+
config.client_encoding = result.query.encoding;
29+
return config;
30+
}
2331
config.host = result.hostname;
24-
config.database = result.pathname ? result.pathname.slice(1) : null;
32+
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
2533
var auth = (result.auth || ':').split(':');
2634
config.user = auth[0];
2735
config.password = auth[1];

test/unit/connection-parameters/creation-tests.js

+34
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,44 @@ test('ConnectionParameters initializing from config', function() {
4747
assert.ok(subject.isDomainSocket === false);
4848
});
4949

50+
test('escape spaces if present', function() {
51+
subject = new ConnectionParameters('postgres://localhost/post gres');
52+
assert.equal(subject.database, 'post gres');
53+
});
54+
55+
test('do not double escape spaces', function() {
56+
subject = new ConnectionParameters('postgres://localhost/post%20gres');
57+
assert.equal(subject.database, 'post gres');
58+
});
59+
5060
test('initializing with unix domain socket', function() {
5161
var subject = new ConnectionParameters('/var/run/');
5262
assert.ok(subject.isDomainSocket);
5363
assert.equal(subject.host, '/var/run/');
64+
assert.equal(subject.database, defaults.user);
65+
});
66+
67+
test('initializing with unix domain socket and a specific database, the simple way', function() {
68+
var subject = new ConnectionParameters('/var/run/ mydb');
69+
assert.ok(subject.isDomainSocket);
70+
assert.equal(subject.host, '/var/run/');
71+
assert.equal(subject.database, 'mydb');
72+
});
73+
74+
test('initializing with unix domain socket, the health way', function() {
75+
var subject = new ConnectionParameters('socket:/some path/?db=my[db]&encoding=utf8');
76+
assert.ok(subject.isDomainSocket);
77+
assert.equal(subject.host, '/some path/');
78+
assert.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
79+
assert.equal(subject.client_encoding, 'utf8');
80+
});
81+
82+
test('initializing with unix domain socket, the escaped health way', function() {
83+
var subject = new ConnectionParameters('socket:/some%20path/?db=my%2Bdb&encoding=utf8');
84+
assert.ok(subject.isDomainSocket);
85+
assert.equal(subject.host, '/some path/');
86+
assert.equal(subject.database, 'my+db');
87+
assert.equal(subject.client_encoding, 'utf8');
5488
});
5589

5690
test('libpq connection string building', function() {

0 commit comments

Comments
 (0)