2014-11-14 15:06:53 +01:00
|
|
|
var logger = require('./logger');
|
|
|
|
var nconf = require('nconf');
|
|
|
|
var fs = require('fs');
|
|
|
|
var ini = require('ini');
|
|
|
|
var knex = require('knex');
|
2014-10-21 15:28:10 +02:00
|
|
|
|
2014-12-12 14:11:09 +01:00
|
|
|
function isTestEnv() {
|
|
|
|
return process.env.NODE_ENV === 'test';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDatabaseIniUrl() {
|
|
|
|
// Finn specific way of delivering env variables
|
|
|
|
var databaseini = nconf.argv().get('databaseini');
|
|
|
|
var config = ini.parse(fs.readFileSync(databaseini, 'utf-8'));
|
|
|
|
|
|
|
|
logger.info('unleash started with databaseini: ' + databaseini);
|
|
|
|
|
|
|
|
return config.DATABASE_URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTestDatabaseUrl() {
|
|
|
|
if (process.env.TEST_DATABASE_URL) {
|
|
|
|
logger.info('unleash started with TEST_DATABASE_URL');
|
|
|
|
return process.env.TEST_DATABASE_URL;
|
|
|
|
} else {
|
|
|
|
throw new Error('please set TEST_DATABASE_URL');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 15:06:53 +01:00
|
|
|
function getDatabaseUrl() {
|
2014-10-23 10:05:56 +02:00
|
|
|
if (process.env.DATABASE_URL) {
|
|
|
|
logger.info('unleash started with DATABASE_URL');
|
2014-11-14 15:06:53 +01:00
|
|
|
return process.env.DATABASE_URL;
|
2014-10-23 14:39:50 +02:00
|
|
|
} else if (nconf.argv().get('databaseini') !== undefined) {
|
2014-12-12 14:11:09 +01:00
|
|
|
return getDatabaseIniUrl();
|
2014-10-21 15:28:10 +02:00
|
|
|
}
|
2014-10-23 14:39:50 +02:00
|
|
|
|
|
|
|
throw new Error('please set DATABASE_URL or pass --databaseini');
|
2014-10-21 15:28:10 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:06:53 +01:00
|
|
|
function createDbPool() {
|
|
|
|
return knex({
|
|
|
|
client: 'pg',
|
2014-12-12 14:11:09 +01:00
|
|
|
connection: isTestEnv() ? getTestDatabaseUrl() : getDatabaseUrl(),
|
2016-04-24 22:41:37 +02:00
|
|
|
pool: { min: 2, max: 20 }
|
2014-11-14 15:06:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:41:37 +02:00
|
|
|
module.exports = createDbPool();
|