2016-02-12 14:56:27 +03:00
|
|
|
var knex = require('knex'),
|
|
|
|
config = require('../../config'),
|
|
|
|
dbConfig = config.database,
|
|
|
|
knexInstance;
|
|
|
|
|
2016-03-24 15:49:06 +03:00
|
|
|
function configure(dbConfig) {
|
|
|
|
var client = dbConfig.client,
|
|
|
|
pg;
|
2016-02-12 14:56:27 +03:00
|
|
|
|
|
|
|
if (client === 'pg' || client === 'postgres' || client === 'postgresql') {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2016-03-24 15:49:06 +03:00
|
|
|
|
|
|
|
if (client === 'sqlite3') {
|
2016-03-24 19:33:16 +03:00
|
|
|
dbConfig.useNullAsDefault = dbConfig.useNullAsDefault || false;
|
2016-03-24 15:49:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return dbConfig;
|
2016-02-12 14:56:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!knexInstance && dbConfig && dbConfig.client) {
|
2016-03-24 15:49:06 +03:00
|
|
|
knexInstance = knex(configure(dbConfig));
|
2016-02-12 14:56:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = knexInstance;
|