From 4df46fe5eac4098a61bdd4bd7cb5f299626df16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 17 Nov 2017 19:20:42 +0100 Subject: [PATCH] Move creation of PG client to a middleware --- lib/cartodb/controllers/analyses.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 3655702e..80c47416 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -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) {