diff --git a/mod/query.js b/mod/query.js index 568e7f7fc..38a766e2d 100644 --- a/mod/query.js +++ b/mod/query.js @@ -50,25 +50,25 @@ module.exports = async (req, res) => { // Assign role filter and viewport params from layer object. if (req.params.layer) { - const layer = await getLayer(req.params) + req.params.layer = await getLayer(req.params) - if (layer instanceof Error) { + if (req.params.layer instanceof Error) { return res.status(400).send('Failed to access layer.') } - if (!Roles.check(layer, req.params.user?.roles)) { + if (!Roles.check(req.params.layer, req.params.user?.roles)) { return res.status(403).send('Role access denied for layer.') } // Set layer dbs as fallback param if not defined. - req.params.dbs = req.params.dbs || layer.dbs + req.params.dbs = req.params.dbs || req.params.layer.dbs // Get array of role filter from layer configuration. - const roles = Roles.filter(layer, req.params.user?.roles) + const roles = Roles.filter(req.params.layer, req.params.user?.roles) // Create params filter string from roleFilter filter params. req.params.filter = - ` ${layer.filter?.default && 'AND ' + layer.filter?.default || ''} + ` ${req.params.layer.filter?.default && 'AND ' + req.params.layer.filter?.default || ''} ${req.params.filter && `AND ${sqlFilter(JSON.parse(req.params.filter), SQLparams)}` || ''} ${roles && Object.values(roles).some(r => !!r) ? `AND ${sqlFilter(Object.values(roles).filter(r => !!r), SQLparams)}` @@ -89,11 +89,11 @@ module.exports = async (req, res) => { ${viewport[3]}, ${parseInt(viewport[4])} ), - ${req.params.srid || layer.srid}), - ${req.params.geom || layer.geom} + ${req.params.srid || req.params.layer.srid}), + ${req.params.geom || req.params.layer.geom} )` - req.params.qID = req.params.qID || layer.qID || 'NULL' + req.params.qID = req.params.qID || req.params.layer.qID || 'NULL' } else { diff --git a/mod/workspace/templates/cluster.js b/mod/workspace/templates/cluster.js index 3b571f716..c78682eca 100644 --- a/mod/workspace/templates/cluster.js +++ b/mod/workspace/templates/cluster.js @@ -1,9 +1,7 @@ module.exports = _ => { - const layer = _.workspace.locales[_.locale].layers[_.layer] - - _.qID ??= layer.qID || null - _.geom ??= layer.geom + _.qID ??= _.layer.qID || null + _.geom ??= _.layer.geom // Get fields array from query params. const fields = _.fields?.split(',') diff --git a/mod/workspace/templates/cluster_hex.js b/mod/workspace/templates/cluster_hex.js index c2d0153cc..eb8985307 100644 --- a/mod/workspace/templates/cluster_hex.js +++ b/mod/workspace/templates/cluster_hex.js @@ -1,9 +1,7 @@ module.exports = _ => { - const layer = _.workspace.locales[_.locale].layers[_.layer] - - _.qID ??= layer.qID || null - _.geom ??= layer.geom + _.qID ??= _.layer.qID || null + _.geom ??= _.layer.geom // Get fields array from query params. const fields = _.fields?.split(',') diff --git a/mod/workspace/templates/get_last_location.js b/mod/workspace/templates/get_last_location.js index 495c030a1..32d2d668d 100644 --- a/mod/workspace/templates/get_last_location.js +++ b/mod/workspace/templates/get_last_location.js @@ -1,16 +1,14 @@ module.exports = _ => { - const layer = _.workspace.locales[_.locale].layers[_.layer] + const table = _.layer.table || Object.values(_.layer.tables).find(tab => !!tab); - const table = layer.table || Object.values(layer.tables).find(tab => !!tab); - - const geom = layer.geom || Object.values(layer.geoms).find(tab => !!tab); + const geom = _.layer.geom || Object.values(_.layer.geoms).find(tab => !!tab); return ` SELECT - ${layer.qID} as id + ${_.layer.qID} as id FROM ${table} - WHERE ${geom} IS NOT NULL AND ${layer.qID} IS NOT NULL - ORDER BY ${layer.qID} DESC + WHERE ${geom} IS NOT NULL AND ${_.layer.qID} IS NOT NULL + ORDER BY ${_.layer.qID} DESC LIMIT 1` } \ No newline at end of file diff --git a/mod/workspace/templates/mvt_cache.js b/mod/workspace/templates/mvt_cache.js index 24a07a1ed..f31b3bc13 100644 --- a/mod/workspace/templates/mvt_cache.js +++ b/mod/workspace/templates/mvt_cache.js @@ -1,25 +1,23 @@ module.exports = _ => { - const layer = _.workspace.locales[_.locale].layers[_.layer] - - if (!layer.mvt_cache) return; + if (!_.layer.mvt_cache) return; return ` - DROP table if exists ${layer.mvt_cache}; + DROP table if exists ${_.layer.mvt_cache}; - Create UNLOGGED table ${layer.mvt_cache} + Create UNLOGGED table ${_.layer.mvt_cache} ( z integer not null, x integer not null, y integer not null, mvt bytea, tile geometry(Polygon, ${layer.srid}), - constraint ${layer.mvt_cache.replace(/\./, '_')}_z_x_y_pk + constraint ${_.layer.mvt_cache.replace(/\./, '_')}_z_x_y_pk primary key (z, x, y) ); - Create index IF NOT EXISTS ${layer.mvt_cache.replace(/\./, '_')}_tile on ${layer.mvt_cache} (tile); + Create index IF NOT EXISTS ${_.layer.mvt_cache.replace(/\./, '_')}_tile on ${_.layer.mvt_cache} (tile); - SELECT '${layer.mvt_cache} cache OK' as msg;` + SELECT '${_.layer.mvt_cache} cache OK' as msg;` } \ No newline at end of file diff --git a/mod/workspace/templates/wkt.js b/mod/workspace/templates/wkt.js index aaa2a3004..713f18e0d 100644 --- a/mod/workspace/templates/wkt.js +++ b/mod/workspace/templates/wkt.js @@ -1,7 +1,5 @@ module.exports = _ => { - const layer = _.workspace.locales[_.locale].layers[_.layer] - // Get fields array from query params. const fields = _.fields?.split(',') .map(field => _.workspace.templates[field]?.template || field) @@ -10,12 +8,12 @@ module.exports = _ => { // Push label (cluster) into fields _.label && fields.push(_.workspace.templates[_.label]?.template || _.label) - const where = _.viewport || `AND ${_.geom || layer.geom} IS NOT NULL` + const where = _.viewport || `AND ${_.geom || _.layer.geom} IS NOT NULL` return ` SELECT \${qID} AS id, - ST_AsText(${_.geom || layer.geom}) AS geometry + ST_AsText(${_.geom || _.layer.geom}) AS geometry ${fields && `, ${fields.join(', ')}` || ''} FROM \${table} WHERE TRUE ${where} \${filter};`