2016-06-03 11:06:18 +03:00
|
|
|
var knex = require('knex'),
|
|
|
|
config = require('../../config'),
|
2017-12-11 22:27:09 +03:00
|
|
|
logging = require('../../lib/common/logging'),
|
|
|
|
errors = require('../../lib/common/errors'),
|
2016-02-12 14:56:27 +03:00
|
|
|
knexInstance;
|
|
|
|
|
2016-07-14 13:59:42 +03: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 15:49:06 +03:00
|
|
|
function configure(dbConfig) {
|
2016-09-16 12:40:23 +03:00
|
|
|
var client = dbConfig.client;
|
2016-03-24 15:49:06 +03:00
|
|
|
|
|
|
|
if (client === 'sqlite3') {
|
2016-03-24 19:33:16 +03:00
|
|
|
dbConfig.useNullAsDefault = dbConfig.useNullAsDefault || false;
|
2016-03-24 15:49:06 +03:00
|
|
|
}
|
|
|
|
|
2016-06-03 11:06:18 +03:00
|
|
|
if (client === 'mysql') {
|
|
|
|
dbConfig.connection.timezone = 'UTC';
|
2016-09-20 17:52:43 +03:00
|
|
|
dbConfig.connection.charset = 'utf8mb4';
|
2017-10-04 11:40:59 +03:00
|
|
|
|
|
|
|
dbConfig.connection.loggingHook = function loggingHook(err) {
|
|
|
|
logging.error(new errors.InternalServerError({
|
|
|
|
code: 'MYSQL_LOGGING_HOOK',
|
|
|
|
err: err
|
|
|
|
}));
|
|
|
|
};
|
2016-06-03 11:06:18 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:49:06 +03:00
|
|
|
return dbConfig;
|
2016-02-12 14:56:27 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 18:41:14 +03:00
|
|
|
if (!knexInstance && config.get('database') && config.get('database').client) {
|
|
|
|
knexInstance = knex(configure(config.get('database')));
|
2016-02-12 14:56:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = knexInstance;
|