From 9a7e263773f9b6f57c9876d87d22d2af72b66368 Mon Sep 17 00:00:00 2001 From: William F Wheeler II Date: Wed, 26 Sep 2018 14:56:36 -0400 Subject: [PATCH] Fix(config): Properly use DATABASE_URL as per documentation Signed-off-by: William F Wheeler II --- lib/config.js | 4 +++- test/config_test.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/config.js b/lib/config.js index 85fc229c..d404fbe2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -31,7 +31,9 @@ Config.prototype = { }; exports.load = function (config, currentEnv) { - if (typeof config === 'object') { + if (process.env.DATABASE_URL) { + return exports.loadUrl(process.env.DATABASE_URL, currentEnv); + } else if (typeof config === 'object') { return exports.loadObject(config, currentEnv); } else { return exports.loadFile(config, currentEnv); diff --git a/test/config_test.js b/test/config_test.js index 500b3837..bb6770cc 100644 --- a/test/config_test.js +++ b/test/config_test.js @@ -308,6 +308,28 @@ lab.experiment('config', function () { } ); + lab.experiment('loading a url from the DATABASE_URL environment variable', function () { + lab.test('should use DATABASE_URL env var in lue of anything else', function (done, cleanup) { + process.env.DATABASE_URL = 'postgres://uname:pw@server.com/dbname'; + var configPath = path.join(__dirname, 'database_with_default_env.json'); + var cfg = config.load(configPath, 'dev'); + cleanup(function (next) { + delete process.env.DATABASE_URL; + next(); + }); + + Code.expect(cfg.getCurrent).to.exists(); + var current = cfg.getCurrent(); + Code.expect(current.env).to.equal('dev'); + 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.database).to.equal('dbname'); + Code.expect(current.settings.host).to.equal('server.com'); + done(); + }); + }); + lab.experiment( 'loading from an ENV URL within the object and extending it from the ENV', function () {