0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/data/db/connection.js
Kevin Ansfield 95105836aa
Removed logging require in db/connection.js (#12690)
refs https://github.com/TryGhost/Ghost/issues/12496

- having the logging require here means that workers wanting to use the db are unable to do so without requiring logging as a side-effect
- `connection.loggingHook` does not appear to be widely used for anything outside of specific debugging scenarios when using MySQL so it should be safe to disable until a proper fix is found for workers+logging leaking file descriptors
2021-02-22 12:58:57 +00:00

46 lines
1.7 KiB
JavaScript

const knex = require('knex');
const config = require('../../../shared/config');
let 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) {
const client = dbConfig.client;
if (client === 'sqlite3') {
// Backwards compatibility with old knex behaviour
dbConfig.useNullAsDefault = Object.prototype.hasOwnProperty.call(dbConfig, 'useNullAsDefault') ? dbConfig.useNullAsDefault : true;
// Force bthreads to use child_process backend until a worker_thread-compatible version of sqlite3 is published
// https://github.com/mapbox/node-sqlite3/issues/1386
process.env.BTHREADS_BACKEND = 'child_process';
}
if (client === 'mysql') {
dbConfig.connection.timezone = 'UTC';
dbConfig.connection.charset = 'utf8mb4';
// NOTE: disabled so that worker processes can use the db without
// requiring logging and causing file desriptor leaks.
// See https://github.com/TryGhost/Ghost/issues/12496
//
// const logging = require('../../../shared/logging');
// const errors = require('@tryghost/errors');
// dbConfig.connection.loggingHook = function loggingHook(err) {
// logging.error(new 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;