diff --git a/lib/config.js b/lib/config.js index f27e641a..b6b463af 100644 --- a/lib/config.js +++ b/lib/config.js @@ -98,12 +98,17 @@ function walkConfig (level) { for (var configEntry in level) { if (level[configEntry] && level[configEntry].ENV) { if (!process.env[level[configEntry].ENV]) { - log.verbose( - 'Environment variable ' + level[configEntry].ENV + ' is empty!' - ); + if (level[configEntry].default !== undefined) { + log.verbose('Environment variable ' + level[configEntry].ENV + ' is empty, used provided default value.'); + level[configEntry] = level[configEntry].default; + } else { + log.verbose( + 'Environment variable ' + level[configEntry].ENV + ' is empty!' + ); + } + } else { + level[configEntry] = process.env[level[configEntry].ENV]; } - - level[configEntry] = process.env[level[configEntry].ENV]; } else if (level[configEntry] && typeof level[configEntry] === 'object') { level[configEntry] = walkConfig(level[configEntry]); } @@ -120,7 +125,12 @@ exports.loadObject = function (_config, currentEnv) { for (var env in config) { if (config[env].ENV) { if (!process.env[config[env].ENV]) { - log.verbose('Environment variable ' + config[env].ENV + ' is empty!'); + if (config[env].default !== undefined) { + log.verbose('Environment variable ' + config[env].ENV + ' is empty, used provided default value.'); + out[env] = parseDatabaseUrl(config[env].default); + } else { + log.verbose('Environment variable ' + config[env].ENV + ' is empty!'); + } } else { out[env] = parseDatabaseUrl(process.env[config[env].ENV]); } diff --git a/test/config_test.js b/test/config_test.js index a9b5f9ee..a3cd2273 100644 --- a/test/config_test.js +++ b/test/config_test.js @@ -101,6 +101,18 @@ lab.experiment('config', function () { } ); + lab.experiment( + 'loading from a file with ENV vars that have defaults set', + function () { + var _config = config.load(path.join(__dirname, 'database_with_env_using_default.json'), 'prod'); + lab.test( + 'should load the default value specified in the config', () => { + Code.expect(_config.prod.username).to.equal('defaultUser'); + } + ); + } + ); + lab.experiment( 'loading from a file with ENV URL', @@ -122,6 +134,26 @@ lab.experiment('config', function () { } ); + lab.experiment( + 'loading from a file with ENV URL default', + + function () { + var configPath = path.join(__dirname, 'database_with_env_url_default.json'); + var _config = config.load(configPath, 'prod'); + + lab.test( + 'should load a value from the environments', () => { + var current = _config.getCurrent(); + Code.expect(current.settings.driver).to.equal('postgres'); + Code.expect(current.settings.user).to.equal('uname'); + Code.expect(current.settings.password).to.equal('pw'); + Code.expect(current.settings.host).to.equal('server.com'); + Code.expect(current.settings.database).to.equal('dbname'); + } + ); + } + ); + lab.experiment('loading from an URL', function () { var databaseUrl = 'postgres://uname:pw@server.com/dbname'; var _config = config.loadUrl(databaseUrl, 'dev'); diff --git a/test/database_with_env_url_default.json b/test/database_with_env_url_default.json new file mode 100644 index 00000000..8636e5aa --- /dev/null +++ b/test/database_with_env_url_default.json @@ -0,0 +1,3 @@ +{ + "prod": {"ENV": "BOGUS_ENV_VAR", "default": "postgres://uname:pw@server.com/dbname" } +} diff --git a/test/database_with_env_using_default.json b/test/database_with_env_using_default.json new file mode 100644 index 00000000..70dc8e10 --- /dev/null +++ b/test/database_with_env_using_default.json @@ -0,0 +1,7 @@ +{ + "prod": { + "driver": "sqlite3", + "filename": "prod.db", + "username": {"ENV": "BOGUS_ENV_VAR", "default": "defaultUser" } + } +}