Skip to content

Commit 1e3107a

Browse files
committed
use ConnectionParameters for js client properties
1 parent 9dad56a commit 1e3107a

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

lib/client.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var crypto = require('crypto');
22
var EventEmitter = require('events').EventEmitter;
33
var util = require('util');
44

5+
var ConnectionParameters = require(__dirname + '/connection-parameters');
56
var Query = require(__dirname + '/query');
67
var utils = require(__dirname + '/utils');
78
var defaults = require(__dirname + '/defaults');
@@ -10,20 +11,21 @@ var CopyFromStream = require(__dirname + '/copystream').CopyFromStream;
1011
var CopyToStream = require(__dirname + '/copystream').CopyToStream;
1112
var Client = function(config) {
1213
EventEmitter.call(this);
13-
if(typeof config === 'string') {
14-
config = utils.normalizeConnectionInfo(config)
15-
}
14+
15+
this.connectionParameters = new ConnectionParameters(config);
16+
this.user = this.connectionParameters.user;
17+
this.database = this.connectionParameters.database;
18+
this.port = this.connectionParameters.port;
19+
this.host = this.connectionParameters.host;
20+
this.password = this.connectionParameters.password;
21+
1622
config = config || {};
17-
this.user = config.user || defaults.user;
18-
this.database = config.database || defaults.database;
19-
this.port = config.port || defaults.port;
20-
this.host = config.host || defaults.host;
23+
2124
this.connection = config.connection || new Connection({
2225
stream: config.stream,
2326
ssl: config.ssl
2427
});
2528
this.queryQueue = [];
26-
this.password = config.password || defaults.password;
2729
this.binary = config.binary || defaults.binary;
2830
this.encoding = 'utf8';
2931
this.processID = null;

test/integration/client/configuration-tests.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
var helper = require(__dirname + '/test-helper');
22
var pg = helper.pg;
33

4+
//clear process.env
5+
var realEnv = {};
6+
for(var key in process.env) {
7+
realEnv[key] = process.env[key];
8+
if(!key.indexOf('PG')) delete process.env[key];
9+
}
10+
411
test('default values', function() {
512
assert.same(pg.defaults,{
613
user: process.env.USER,
@@ -44,3 +51,8 @@ if(!helper.args.native) {
4451
})
4552
}
4653

54+
55+
//restore process.env
56+
for(var key in realEnv) {
57+
process.env[key] = realEnv[key];
58+
}

test/unit/client/configuration-tests.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ test('client settings', function() {
44

55
test('defaults', function() {
66
var client = new Client();
7-
assert.equal(client.user, process.env.USER);
8-
assert.equal(client.database, process.env.USER);
7+
assert.equal(client.user, process.env['PGUSER'] || process.env.USER);
8+
assert.equal(client.database, process.env['PGDATABASE'] || process.env.USER);
99
assert.equal(client.port, 5432);
1010
});
1111

@@ -41,11 +41,11 @@ test('initializing from a config string', function() {
4141

4242
test('when not including all values the defaults are used', function() {
4343
var client = new Client("pg://host1")
44-
assert.equal(client.user, process.env.USER)
45-
assert.equal(client.password, null)
44+
assert.equal(client.user, process.env['PGUSER'] || process.env.USER)
45+
assert.equal(client.password, process.env['PGPASSWORD'] || null)
4646
assert.equal(client.host, "host1")
47-
assert.equal(client.port, 5432)
48-
assert.equal(client.database, process.env.USER)
47+
assert.equal(client.port, process.env['PGPORT'] || 5432)
48+
assert.equal(client.database, process.env['PGDATABASE'] || process.env.USER)
4949
})
5050

5151

0 commit comments

Comments
 (0)