Ghost/core/server/data/db/connection.js
kirrg001 6f6c8f4521 Import lib/common only
refs #9178

- avoid importing 4 modules (logging, errors, events and i18n)
- simply require common in each file
2017-12-12 10:28:13 +01:00

37 lines
1.1 KiB
JavaScript

var knex = require('knex'),
config = require('../../config'),
common = require('../../lib/common'),
knexInstance;
// @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())
function configure(dbConfig) {
var client = dbConfig.client;
if (client === 'sqlite3') {
dbConfig.useNullAsDefault = dbConfig.useNullAsDefault || false;
}
if (client === 'mysql') {
dbConfig.connection.timezone = 'UTC';
dbConfig.connection.charset = 'utf8mb4';
dbConfig.connection.loggingHook = function loggingHook(err) {
common.logging.error(new common.errors.InternalServerError({
code: 'MYSQL_LOGGING_HOOK',
err: err
}));
};
}
return dbConfig;
}
if (!knexInstance && config.get('database') && config.get('database').client) {
knexInstance = knex(configure(config.get('database')));
}
module.exports = knexInstance;