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'); var dot = require('dot');
dot.templateSettings.strip = false; dot.templateSettings.strip = false;
var PSQL = require('cartodb-psql'); var PSQL = require('cartodb-psql');
@ -18,6 +16,7 @@ AnalysesController.prototype.register = function(app) {
cors(), cors(),
userMiddleware, userMiddleware,
this.prepareContext, this.prepareContext,
this.createPGClient(),
this.getCatalog(), this.getCatalog(),
this.getTables(), this.getTables(),
this.prepareResponse(), 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 () { AnalysesController.prototype.getCatalog = function () {
return function getCatalogMiddleware(req, res, next) { return function getCatalogMiddleware(req, res, next) {
const pg = res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals)); const { pg, user } = res.locals;
const { user } = res.locals;
const readOnlyTransactionOn = true;
const catalogQuery = catalogQueryTpl({ _username: user }); const catalogQuery = catalogQueryTpl({ _username: user });
const readOnlyTransactionOn = true;
pg.query(catalogQuery, (err, resultSet = {}) => { pg.query(catalogQuery, (err, resultSet = {}) => {
if (err) { if (err) {
@ -48,10 +53,9 @@ AnalysesController.prototype.getCatalog = function () {
AnalysesController.prototype.getTables = function () { AnalysesController.prototype.getTables = function () {
return function getTablesMiddleware(req, res, next) { return function getTablesMiddleware(req, res, next) {
const pg = res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals)); const { pg, user } = res.locals;
const { user } = res.locals;
const readOnlyTransactionOn = true;
const tablesQuery = tablesQueryTpl({ _username: user }); const tablesQuery = tablesQueryTpl({ _username: user });
const readOnlyTransactionOn = true;
pg.query(tablesQuery, (err, resultSet = {}) => { pg.query(tablesQuery, (err, resultSet = {}) => {
if (err) { if (err) {