Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tie default DB name to database user name #1660

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var defaults = require('./defaults')

var parse = require('pg-connection-string').parse // parses a connection string

var val = function (key, config, envVar) {
var val = function (key, config, envVar, computed) {
if (envVar === undefined) {
envVar = process.env[ 'PG' + key.toUpperCase() ]
} else if (envVar === false) {
Expand All @@ -22,9 +22,13 @@ var val = function (key, config, envVar) {
envVar = process.env[ envVar ]
}

if (computed === undefined) {
computed = function (key) { return defaults[key] }
}

return config[key] ||
envVar ||
defaults[key]
computed(key)
}

var useSsl = function () {
Expand All @@ -50,8 +54,11 @@ var ConnectionParameters = function (config) {
config = Object.assign({}, config, parse(config.connectionString))
}

var dbDefaulting = function () { return this.user || defaults['database'] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for this.user to be falsy?

Copy link
Author

@solidsnack solidsnack May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the implementation, it's set with val('user', config) which can be false when (a) there is no default set and (b) the corresponding environment variable is not set.

From the outside, URLs like postgres:/// don't have a user, so it stands to reason the implementation has to allow it to be falsey.

Copy link
Collaborator

@charmander charmander May 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default has to be set to something; if it’s undefined, the connection is going to fail anyway. I think this can just be const getDefaultDatabase = () => this.user. (At that point, it doesn’t even need to be a function – this.user can be passed in directly.)

dbDefaulting = dbDefaulting.bind(this)

this.user = val('user', config)
this.database = val('database', config)
this.database = val('database', config, undefined, dbDefaulting)
this.port = parseInt(val('port', config), 10)
this.host = val('host', config)
this.password = val('password', config)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "7.4.3",
"version": "8.0.0",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"database",
Expand Down
4 changes: 2 additions & 2 deletions test/integration/client/configuration-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ suite.test('default values are used in new clients', function () {
suite.test('modified values are passed to created clients', function () {
pg.defaults.user = 'boom'
pg.defaults.password = 'zap'
pg.defaults.database = 'pow'
pg.defaults.port = 1234
pg.defaults.host = 'blam'

var client = new Client()
assert.same(client, {
user: 'boom',
password: 'zap',
database: 'pow',
// default database name is user name
database: 'boom',
port: 1234,
host: 'blam'
})
Expand Down