From c0fd4b14317159d6975bd965044f67fe0049ec59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lio=20A=2E=20Heckert?= Date: Thu, 19 Dec 2013 15:37:26 -0300 Subject: [PATCH] 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) --- lib/connection-parameters.js | 16 ++++++--- .../connection-parameters/creation-tests.js | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index f21da1f..e69fc5b 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -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]; diff --git a/test/unit/connection-parameters/creation-tests.js b/test/unit/connection-parameters/creation-tests.js index 0624c79..aaab121 100644 --- a/test/unit/connection-parameters/creation-tests.js +++ b/test/unit/connection-parameters/creation-tests.js @@ -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() {