2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 15:51:51 +08:00
|
|
|
const NamedMapsCacheEntry = require('../../cache/model/named-maps-entry');
|
2018-04-07 00:20:33 +08:00
|
|
|
const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider');
|
2018-03-21 21:11:54 +08:00
|
|
|
|
|
|
|
module.exports = function setSurrogateKeyHeader ({ surrogateKeysCache }) {
|
2019-10-22 01:07:24 +08:00
|
|
|
return function setSurrogateKeyHeaderMiddleware (req, res, next) {
|
2020-06-02 15:00:45 +08:00
|
|
|
const { user, mapConfigProvider, logger } = res.locals;
|
2018-03-21 21:11:54 +08:00
|
|
|
|
|
|
|
if (mapConfigProvider instanceof NamedMapMapConfigProvider) {
|
|
|
|
surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, mapConfigProvider.getTemplateName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method !== 'GET') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
mapConfigProvider.getAffectedTables((err, affectedTables) => {
|
|
|
|
if (err) {
|
2020-06-02 15:00:45 +08:00
|
|
|
logger.warn(err, 'ERROR generating Surrogate Key Header');
|
2018-03-21 21:11:54 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!affectedTables || !affectedTables.tables || affectedTables.tables.length === 0) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
surrogateKeysCache.tag(res, affectedTables);
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|