0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00
ghost/core/server/data/db/connection.js
Daniel Lockyer 93339b3e42
Removed unused code for logging in MySQL lib
- `mysql2` doesn't support this and we haven't had it enabled for a long time
- if we need this feature, we can re-add it with a working implementation
2022-05-16 10:53:08 +01:00

48 lines
1.8 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;
// Enables foreign key checks and delete on cascade
dbConfig.pool = {
afterCreate(conn, cb) {
conn.run('PRAGMA foreign_keys = ON', cb);
// These two are meant to improve performance at the cost of reliability
// Should be safe for tests. We add them here and leave them on
if (config.get('env').startsWith('testing')) {
conn.run('PRAGMA synchronous = OFF;');
conn.run('PRAGMA journal_mode = TRUNCATE;');
}
}
};
// 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 === 'mysql2') {
dbConfig.connection.timezone = 'Z';
dbConfig.connection.charset = 'utf8mb4';
dbConfig.connection.decimalNumbers = true;
}
return dbConfig;
}
if (!knexInstance && config.get('database') && config.get('database').client) {
knexInstance = knex(configure(config.get('database')));
}
module.exports = knexInstance;