add ConnectionParameters object

This commit is contained in:
bmc 2013-01-17 18:14:13 -06:00
parent bd02375347
commit 92e75f0577
3 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,38 @@
var defaults = require(__dirname + '/defaults');
var val = function(key, config) {
return config[key] ||
process.env['PG' + key.toUpperCase()] ||
defaults[key];
}
var ConnectionParameters = function(config) {
config = typeof config == 'string' ? parse(config) : (config || {});
this.user = val('user', config);
this.database = val('database', config);
this.port = parseInt(val('port', config));
this.host = val('host', config);
this.password = val('password', config);
this.binary = val('binary', config);
this.ssl = config.ssl || defaults.ssl;
}
var url = require('url');
//parses a connection string
var parse = function(str) {
//unix socket
if(str.charAt(0) === '/') {
return { host: str };
}
var result = url.parse(str);
var config = {};
config.host = result.hostname;
config.database = result.pathname ? result.pathname.slice(1) : null
var auth = (result.auth || ':').split(':');
config.user = auth[0];
config.password = auth[1];
config.port = result.port;
return config;
}
module.exports = ConnectionParameters;

View File

@ -0,0 +1,49 @@
var test = require('tap').test;
var ConnectionParameters = require(__dirname + '/../../../lib/connection-parameters');
var defaults = require(__dirname + '/../../../lib').defaults;
//clear process.env
for(var key in process.env) {
delete process.env[key];
}
test('ConnectionParameters construction', function(t) {
t.ok(new ConnectionParameters(), 'with null config');
t.ok(new ConnectionParameters({user: 'asdf'}), 'with config object');
t.ok(new ConnectionParameters('pg://localhost/postgres'), 'with connection string');
t.end();
})
var compare = function(t, actual, expected, type) {
t.equal(actual.user, expected.user, type + ' user');
t.equal(actual.database, expected.database, type + ' database');
t.equal(actual.port, expected.port, type + ' port');
t.equal(actual.host, expected.host, type + ' host');
t.equal(actual.password, expected.password, type + ' password');
t.equal(actual.binary, expected.binary, type + ' binary');
}
test('ConnectionParameters initializing from defaults', function(t) {
var subject = new ConnectionParameters();
compare(t, subject, defaults, 'defaults');
t.end();
})
test('ConnectionParameters initializing from config', function(t) {
var config = {
user: 'brian',
database: 'home',
port: 7777,
password: 'pizza',
binary: true,
encoding: 'utf8',
host: 'yo',
ssl: {
asdf: 'blah'
}
}
var subject = new ConnectionParameters(config);
compare(t, subject, config, 'config');
t.end();
})

View File

@ -0,0 +1,65 @@
var test = require('tap').test;
var ConnectionParameters = require(__dirname + '/../../../lib/connection-parameters');
var defaults = require(__dirname + '/../../../lib').defaults;
//clear process.env
var realEnv = {};
for(var key in process.env) {
realEnv[key] = process.env[key];
delete process.env[key];
}
test('ConnectionParameters initialized from environment variables', function(t) {
process.env['PGHOST'] = 'local';
process.env['PGUSER'] = 'bmc2';
process.env['PGPORT'] = 7890;
process.env['PGDATABASE'] = 'allyerbase';
process.env['PGPASSWORD'] = 'open';
var subject = new ConnectionParameters();
t.equal(subject.host, 'local', 'env host');
t.equal(subject.user, 'bmc2', 'env user');
t.equal(subject.port, 7890, 'env port');
t.equal(subject.database, 'allyerbase', 'env database');
t.equal(subject.password, 'open', 'env password');
t.end();
})
test('ConnectionParameters initialized from mix', function(t) {
delete process.env['PGPASSWORD'];
delete process.env['PGDATABASE'];
var subject = new ConnectionParameters({
user: 'testing',
database: 'zugzug'
})
t.equal(subject.host, 'local', 'env host');
t.equal(subject.user, 'testing', 'config user');
t.equal(subject.port, 7890, 'env port');
t.equal(subject.database, 'zugzug', 'config database');
t.equal(subject.password, defaults.password, 'defaults password');
t.end();
})
//clear process.env
for(var key in process.env) {
delete process.env[key];
}
test('connection string parsing', function(t) {
var string = 'postgres://brian:pw@boom:381/lala';
var subject = new ConnectionParameters(string);
t.equal(subject.host, 'boom', 'string host');
t.equal(subject.user, 'brian', 'string user');
t.equal(subject.password, 'pw', 'string password');
t.equal(subject.port, 381, 'string port');
t.equal(subject.database, 'lala', 'string database');
t.end();
})
//restore process.env
for(var key in realEnv) {
process.env[key] = realEnv[key];
}