Skip to content

Commit f0ca9d6

Browse files
committed
Properly use DATABASE_URL as per documentation
1 parent 9f136e1 commit f0ca9d6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Config.prototype = {
3131
};
3232

3333
exports.load = function (config, currentEnv) {
34-
if (typeof config === 'object') {
34+
if (process.env.DATABASE_URL) {
35+
return exports.loadUrl(process.env.DATABASE_URL, currentEnv);
36+
} else if (typeof config === 'object') {
3537
return exports.loadObject(config, currentEnv);
3638
} else {
3739
return exports.loadFile(config, currentEnv);

test/config_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,28 @@ lab.experiment('config', function () {
308308
}
309309
);
310310

311+
lab.experiment('loading a url from the DATABASE_URL environment variable', function () {
312+
lab.test('should use DATABASE_URL env var in lue of anything else', function (done, cleanup) {
313+
process.env.DATABASE_URL = 'postgres://uname:[email protected]/dbname';
314+
var configPath = path.join(__dirname, 'database_with_default_env.json');
315+
var cfg = config.load(configPath, 'dev');
316+
cleanup(function (next) {
317+
delete process.env.DATABASE_URL;
318+
next();
319+
});
320+
321+
Code.expect(cfg.getCurrent).to.exists();
322+
var current = cfg.getCurrent();
323+
Code.expect(current.env).to.equal('dev');
324+
Code.expect(current.settings.driver).to.equal('postgres');
325+
Code.expect(current.settings.user).to.equal('uname');
326+
Code.expect(current.settings.password).to.equal('pw');
327+
Code.expect(current.settings.database).to.equal('dbname');
328+
Code.expect(current.settings.host).to.equal('server.com');
329+
done();
330+
});
331+
});
332+
311333
lab.experiment(
312334
'loading from an ENV URL within the object and extending it from the ENV',
313335
function () {

0 commit comments

Comments
 (0)