Skip to content

Commit 92e75f0

Browse files
committed
add ConnectionParameters object
1 parent bd02375 commit 92e75f0

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

lib/connection-parameters.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var defaults = require(__dirname + '/defaults');
2+
3+
var val = function(key, config) {
4+
return config[key] ||
5+
process.env['PG' + key.toUpperCase()] ||
6+
defaults[key];
7+
}
8+
9+
var ConnectionParameters = function(config) {
10+
config = typeof config == 'string' ? parse(config) : (config || {});
11+
this.user = val('user', config);
12+
this.database = val('database', config);
13+
this.port = parseInt(val('port', config));
14+
this.host = val('host', config);
15+
this.password = val('password', config);
16+
this.binary = val('binary', config);
17+
this.ssl = config.ssl || defaults.ssl;
18+
}
19+
20+
var url = require('url');
21+
//parses a connection string
22+
var parse = function(str) {
23+
//unix socket
24+
if(str.charAt(0) === '/') {
25+
return { host: str };
26+
}
27+
var result = url.parse(str);
28+
var config = {};
29+
config.host = result.hostname;
30+
config.database = result.pathname ? result.pathname.slice(1) : null
31+
var auth = (result.auth || ':').split(':');
32+
config.user = auth[0];
33+
config.password = auth[1];
34+
config.port = result.port;
35+
return config;
36+
}
37+
38+
module.exports = ConnectionParameters;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var test = require('tap').test;
2+
3+
var ConnectionParameters = require(__dirname + '/../../../lib/connection-parameters');
4+
var defaults = require(__dirname + '/../../../lib').defaults;
5+
6+
//clear process.env
7+
for(var key in process.env) {
8+
delete process.env[key];
9+
}
10+
11+
test('ConnectionParameters construction', function(t) {
12+
t.ok(new ConnectionParameters(), 'with null config');
13+
t.ok(new ConnectionParameters({user: 'asdf'}), 'with config object');
14+
t.ok(new ConnectionParameters('pg://localhost/postgres'), 'with connection string');
15+
t.end();
16+
})
17+
18+
var compare = function(t, actual, expected, type) {
19+
t.equal(actual.user, expected.user, type + ' user');
20+
t.equal(actual.database, expected.database, type + ' database');
21+
t.equal(actual.port, expected.port, type + ' port');
22+
t.equal(actual.host, expected.host, type + ' host');
23+
t.equal(actual.password, expected.password, type + ' password');
24+
t.equal(actual.binary, expected.binary, type + ' binary');
25+
}
26+
27+
test('ConnectionParameters initializing from defaults', function(t) {
28+
var subject = new ConnectionParameters();
29+
compare(t, subject, defaults, 'defaults');
30+
t.end();
31+
})
32+
33+
test('ConnectionParameters initializing from config', function(t) {
34+
var config = {
35+
user: 'brian',
36+
database: 'home',
37+
port: 7777,
38+
password: 'pizza',
39+
binary: true,
40+
encoding: 'utf8',
41+
host: 'yo',
42+
ssl: {
43+
asdf: 'blah'
44+
}
45+
}
46+
var subject = new ConnectionParameters(config);
47+
compare(t, subject, config, 'config');
48+
t.end();
49+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var test = require('tap').test;
2+
3+
var ConnectionParameters = require(__dirname + '/../../../lib/connection-parameters');
4+
var defaults = require(__dirname + '/../../../lib').defaults;
5+
6+
7+
//clear process.env
8+
var realEnv = {};
9+
for(var key in process.env) {
10+
realEnv[key] = process.env[key];
11+
delete process.env[key];
12+
}
13+
14+
15+
test('ConnectionParameters initialized from environment variables', function(t) {
16+
process.env['PGHOST'] = 'local';
17+
process.env['PGUSER'] = 'bmc2';
18+
process.env['PGPORT'] = 7890;
19+
process.env['PGDATABASE'] = 'allyerbase';
20+
process.env['PGPASSWORD'] = 'open';
21+
22+
var subject = new ConnectionParameters();
23+
t.equal(subject.host, 'local', 'env host');
24+
t.equal(subject.user, 'bmc2', 'env user');
25+
t.equal(subject.port, 7890, 'env port');
26+
t.equal(subject.database, 'allyerbase', 'env database');
27+
t.equal(subject.password, 'open', 'env password');
28+
t.end();
29+
})
30+
31+
test('ConnectionParameters initialized from mix', function(t) {
32+
delete process.env['PGPASSWORD'];
33+
delete process.env['PGDATABASE'];
34+
var subject = new ConnectionParameters({
35+
user: 'testing',
36+
database: 'zugzug'
37+
})
38+
t.equal(subject.host, 'local', 'env host');
39+
t.equal(subject.user, 'testing', 'config user');
40+
t.equal(subject.port, 7890, 'env port');
41+
t.equal(subject.database, 'zugzug', 'config database');
42+
t.equal(subject.password, defaults.password, 'defaults password');
43+
t.end();
44+
})
45+
46+
//clear process.env
47+
for(var key in process.env) {
48+
delete process.env[key];
49+
}
50+
51+
test('connection string parsing', function(t) {
52+
var string = 'postgres://brian:pw@boom:381/lala';
53+
var subject = new ConnectionParameters(string);
54+
t.equal(subject.host, 'boom', 'string host');
55+
t.equal(subject.user, 'brian', 'string user');
56+
t.equal(subject.password, 'pw', 'string password');
57+
t.equal(subject.port, 381, 'string port');
58+
t.equal(subject.database, 'lala', 'string database');
59+
t.end();
60+
})
61+
62+
//restore process.env
63+
for(var key in realEnv) {
64+
process.env[key] = realEnv[key];
65+
}

0 commit comments

Comments
 (0)