forked from futurice/wappuapp-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
knexfile.js
60 lines (51 loc) · 1.59 KB
/
knexfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Everything required here must be in dependencies, not devDependencies
// because this file is also run in production environment
var path = require('path');
var requireEnvs = require('./src/util/require-envs');
requireEnvs(['DATABASE_URL']);
var connection = process.env.DATABASE_URL + '?charset=utf-8';
if (process.env.NODE_ENV === 'production') {
// Heroku postgres uses ssl
connection += '&ssl=true';
}
var databaseConfig = {
client: 'pg',
connection: connection,
pool: {
min: 2,
max: 10,
ping: function pingDatabase(conn, cb) {
conn.query('SELECT 1', cb);
}
},
debug: process.env.DEBUG_DATABASE === 'true',
migrations: {
directory: path.join(__dirname, 'migrations'),
tableName: 'migrations'
}
};
// All possible NODE_ENVs should be listed here
// This is issue with knex
// See https://github.com/tgriesser/knex/issues/328
var envs = {
development: databaseConfig,
test: databaseConfig,
production: databaseConfig,
// Expose this for database.js
config: databaseConfig
};
if (!envs.hasOwnProperty(process.env.NODE_ENV)) {
console.error('NODE_ENV is not set!');
console.error('Set NODE_ENV manually, or running e.g. source .env');
console.error('\n');
throw new Error('Environment is not set');
}
function censorPgConnectionString(str) {
var regex = /^(postgres):\/\/(.*):(.*)@(.*:[0-9]*\/.*)$/;
if (str.match(regex) !== null) {
return str.replace(regex, '$1://$2:HIDDEN_PASSWORD@$4');
}
return 'CENSORED CONNECTION STRING';
}
console.log('DATABASE_URL=' + censorPgConnectionString(databaseConfig.connection));
module.exports = envs;