Windshaft-cartodb/lib/api/middlewares/surrogate-key-header.js

35 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
const NamedMapsCacheEntry = require('../../cache/model/named-maps-entry');
const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider');
module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) {
2019-10-22 01:07:24 +08:00
return function setSurrogateKeyHeaderMiddleware (req, res, next) {
const { user, mapConfigProvider, logger } = res.locals;
if (mapConfigProvider instanceof NamedMapMapConfigProvider) {
surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, mapConfigProvider.getTemplateName()));
}
if (req.method !== 'GET') {
return next();
}
mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) {
err.message = `Erros generating Surrogate Key Header. ${err.message}`;
logger.warn({ error: err });
return next();
}
if (!affectedTables || !affectedTables.tables || affectedTables.tables.length === 0) {
return next();
}
surrogateKeysCache.tag(res, affectedTables);
next();
});
};
};