diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 98089eac..0f5512d6 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -1,6 +1,3 @@ -var assert = require('assert'); -var step = require('step'); - var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); var allowQueryParams = require('../middleware/allow-query-params'); @@ -567,62 +564,48 @@ LayergroupController.prototype.affectedTables = function () { }.bind(this); }; -LayergroupController.prototype.getAffectedTables = function(user, dbName, layergroupId, callback) { +LayergroupController.prototype.getAffectedTables = function (user, dbName, layergroupId, callback) { if (this.layergroupAffectedTables.hasAffectedTables(dbName, layergroupId)) { return callback(null, this.layergroupAffectedTables.get(dbName, layergroupId)); } - var self = this; + this.mapStore.load(layergroupId, (err, mapConfig) => { + if (err) { + return callback(err); + } - step( - function loadFromStore() { - self.mapStore.load(layergroupId, this); - }, - function getSQL(err, mapConfig) { - assert.ifError(err); + var queries = []; + mapConfig.getLayers().forEach(function(layer) { + queries.push(layer.options.sql); + if (layer.options.affected_tables) { + layer.options.affected_tables.map(function(table) { + queries.push('SELECT * FROM ' + table + ' LIMIT 0'); + }); + } + }); - var queries = []; - mapConfig.getLayers().forEach(function(layer) { - queries.push(layer.options.sql); - if (layer.options.affected_tables) { - layer.options.affected_tables.map(function(table) { - queries.push('SELECT * FROM ' + table + ' LIMIT 0'); - }); - } - }); + const sql = queries.length ? queries.join(';') : null; - return queries.length ? queries.join(';') : null; - }, - function getConnection(err, sql) { - assert.ifError(err); + if ( ! sql ) { + return callback(new Error("this request doesn't need an X-Cache-Channel generated")); + } - if ( ! sql ) { - throw new Error("this request doesn't need an X-Cache-Channel generated"); + this.pgConnection.getConnection(user, (err, connection) => { + if (err) { + return callback(err); } - const next = this; - - self.pgConnection.getConnection(user, function (err, connection) { + QueryTables.getAffectedTablesFromQuery(connection, sql, (err, tables) => { if (err) { - return next(); + return callback(err); } - next(null, connection, sql); + this.layergroupAffectedTables.set(dbName, layergroupId, tables); + + callback(null, tables); }); - }, - function getAffectedTables(err, connection, sql) { - assert.ifError(err); - - QueryTables.getAffectedTablesFromQuery(connection, sql, this); - }, - function buildCacheChannel(err, tables) { - assert.ifError(err); - self.layergroupAffectedTables.set(dbName, layergroupId, tables); - - return tables; - }, - callback - ); + }); + }); }; LayergroupController.prototype.sendResponse = function () {