2016-06-03 09:06:18 +01:00
|
|
|
var knex = require('knex'),
|
|
|
|
config = require('../../config'),
|
2016-02-12 11:56:27 +00:00
|
|
|
knexInstance;
|
|
|
|
|
2016-07-14 12:59:42 +02:00
|
|
|
// @TODO:
|
|
|
|
// - if you require this file before config file was loaded,
|
|
|
|
// - then this file is cached and you have no chance to connect to the db anymore
|
|
|
|
// - bring dynamic into this file (db.connect())
|
2016-03-24 12:49:06 +00:00
|
|
|
function configure(dbConfig) {
|
2016-09-16 11:40:23 +02:00
|
|
|
var client = dbConfig.client;
|
2016-03-24 12:49:06 +00:00
|
|
|
|
|
|
|
if (client === 'sqlite3') {
|
2016-03-24 16:33:16 +00:00
|
|
|
dbConfig.useNullAsDefault = dbConfig.useNullAsDefault || false;
|
2016-03-24 12:49:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 09:06:18 +01:00
|
|
|
if (client === 'mysql') {
|
|
|
|
dbConfig.connection.timezone = 'UTC';
|
2016-09-20 16:52:43 +02:00
|
|
|
dbConfig.connection.charset = 'utf8mb4';
|
2016-06-03 09:06:18 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 12:49:06 +00:00
|
|
|
return dbConfig;
|
2016-02-12 11:56:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 17:41:14 +02:00
|
|
|
if (!knexInstance && config.get('database') && config.get('database').client) {
|
|
|
|
knexInstance = knex(configure(config.get('database')));
|
2016-02-12 11:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = knexInstance;
|