mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-16 20:43:01 +03:00
34e48dc49a
refs #6982 - we cannot add functions to nconf [ci skip]
63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
var knex = require('knex'),
|
|
config = require('../../config'),
|
|
knexInstance;
|
|
|
|
function isPostgreSQL(client) {
|
|
if (!client) {
|
|
return false;
|
|
}
|
|
|
|
return client === 'pg' || client === 'postgres' || client === 'postgresql';
|
|
}
|
|
|
|
// @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,
|
|
pg;
|
|
|
|
if (isPostgreSQL(client)) {
|
|
try {
|
|
pg = require('pg');
|
|
} catch (e) {
|
|
pg = require('pg.js');
|
|
}
|
|
|
|
// By default PostgreSQL returns data as strings along with an OID that identifies
|
|
// its type. We're setting the parser to convert OID 20 (int8) into a javascript
|
|
// integer.
|
|
pg.types.setTypeParser(20, function (val) {
|
|
return val === null ? null : parseInt(val, 10);
|
|
});
|
|
|
|
// https://github.com/tgriesser/knex/issues/97
|
|
// this sets the timezone to UTC only for the connection!
|
|
dbConfig.pool = {
|
|
afterCreate: function (connection, callback) {
|
|
connection.query('set timezone=\'UTC\'', function (err) {
|
|
callback(err, connection);
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
if (client === 'sqlite3') {
|
|
dbConfig.useNullAsDefault = dbConfig.useNullAsDefault || false;
|
|
}
|
|
|
|
if (client === 'mysql') {
|
|
dbConfig.connection.timezone = 'UTC';
|
|
}
|
|
|
|
return dbConfig;
|
|
}
|
|
|
|
if (!knexInstance && config.get('database') && config.get('database').client) {
|
|
knexInstance = knex(configure(config.get('database')));
|
|
}
|
|
|
|
module.exports = knexInstance;
|
|
module.exports.isPostgreSQL = isPostgreSQL;
|