Move creation of PG client to a middleware

This commit is contained in:
Daniel García Aubert 2017-11-17 19:20:42 +01:00
parent b1b2054f0a
commit 4df46fe5ea

View File

@ -1,5 +1,3 @@
var step = require('step');
var assert = require('assert');
var dot = require('dot');
dot.templateSettings.strip = false;
var PSQL = require('cartodb-psql');
@ -12,12 +10,13 @@ function AnalysesController(prepareContext) {
module.exports = AnalysesController;
AnalysesController.prototype.register = function(app) {
AnalysesController.prototype.register = function (app) {
app.get(
app.base_url_mapconfig + '/analyses/catalog',
cors(),
userMiddleware,
this.prepareContext,
this.createPGClient(),
this.getCatalog(),
this.getTables(),
this.prepareResponse(),
@ -27,12 +26,18 @@ AnalysesController.prototype.register = function(app) {
);
};
AnalysesController.prototype.createPGClient = function () {
return function createPGClientMiddleware (req, res, next) {
res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals));
next();
};
};
AnalysesController.prototype.getCatalog = function () {
return function getCatalogMiddleware(req, res, next) {
const pg = res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals));
const { user } = res.locals;
const readOnlyTransactionOn = true;
const { pg, user } = res.locals;
const catalogQuery = catalogQueryTpl({ _username: user });
const readOnlyTransactionOn = true;
pg.query(catalogQuery, (err, resultSet = {}) => {
if (err) {
@ -48,10 +53,9 @@ AnalysesController.prototype.getCatalog = function () {
AnalysesController.prototype.getTables = function () {
return function getTablesMiddleware(req, res, next) {
const pg = res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals));
const { user } = res.locals;
const readOnlyTransactionOn = true;
const { pg, user } = res.locals;
const tablesQuery = tablesQueryTpl({ _username: user });
const readOnlyTransactionOn = true;
pg.query(tablesQuery, (err, resultSet = {}) => {
if (err) {