2017-09-25 19:40:22 +08:00
|
|
|
const _ = require('underscore');
|
|
|
|
|
|
|
|
module.exports = function dbConnSetupMiddleware(pgConnection) {
|
|
|
|
return function (req, res, next) {
|
|
|
|
const user = req.context.user;
|
|
|
|
|
2017-09-28 17:43:12 +08:00
|
|
|
res.locals.db = {}
|
|
|
|
pgConnection.setDBConn(user, res.locals.db, (err) => {
|
2017-09-25 19:40:22 +08:00
|
|
|
if (err) {
|
|
|
|
if (err.message && -1 !== err.message.indexOf('name not found')) {
|
|
|
|
err.http_status = 404;
|
|
|
|
}
|
|
|
|
req.profiler.done('req2params');
|
|
|
|
return next(err, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add default database connection parameters
|
|
|
|
// if none given
|
2017-09-28 17:43:12 +08:00
|
|
|
_.defaults(res.locals.db, {
|
2017-09-25 19:40:22 +08:00
|
|
|
dbuser: global.environment.postgres.user,
|
|
|
|
dbpassword: global.environment.postgres.password,
|
|
|
|
dbhost: global.environment.postgres.host,
|
|
|
|
dbport: global.environment.postgres.port
|
|
|
|
});
|
|
|
|
|
2017-09-28 17:43:12 +08:00
|
|
|
|
2017-09-25 19:40:22 +08:00
|
|
|
req.profiler.done('req2params');
|
|
|
|
|
|
|
|
next(null, req);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|