2016-06-03 11:06:18 +03:00
|
|
|
var knex = require('knex'),
|
|
|
|
config = require('../../config'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
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') {
|
2019-07-05 14:40:43 +03:00
|
|
|
dbConfig.useNullAsDefault = Object.prototype.hasOwnProperty.call(dbConfig, 'useNullAsDefault') ? dbConfig.useNullAsDefault : true;
|
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) {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error(new common.errors.InternalServerError({
|
2017-10-04 11:40:59 +03:00
|
|
|
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;
|