2020-04-29 18:44:27 +03:00
|
|
|
const knex = require('knex');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
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') {
|
2021-01-28 18:10:34 +03:00
|
|
|
// Backwards compatibility with old knex behaviour
|
2019-07-05 14:40:43 +03:00
|
|
|
dbConfig.useNullAsDefault = Object.prototype.hasOwnProperty.call(dbConfig, 'useNullAsDefault') ? dbConfig.useNullAsDefault : true;
|
2021-01-28 18:10:34 +03:00
|
|
|
|
2021-03-01 21:19:24 +03:00
|
|
|
// Enables foreign key checks and delete on cascade
|
|
|
|
dbConfig.pool = {
|
|
|
|
afterCreate(conn, cb) {
|
|
|
|
conn.run('PRAGMA foreign_keys = ON', cb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-28 18:10:34 +03:00
|
|
|
// 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';
|
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
|
|
|
|
2021-02-22 15:58:57 +03:00
|
|
|
// 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
|
|
|
|
// }));
|
|
|
|
// };
|
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;
|