2020-04-29 18:44:27 +03:00
|
|
|
const knex = require('knex');
|
|
|
|
const config = require('../../config');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {logging} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-29 18:44:27 +03:00
|
|
|
let knexInstance;
|
2016-02-12 14:56:27 +03:00
|
|
|
|
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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const 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) {
|
2020-05-22 21:22:20 +03:00
|
|
|
logging.error(new 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;
|