Skip to content

Commit

Permalink
Assign layer as request param for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Nov 14, 2023
1 parent 4a547b7 commit 7f7495b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
18 changes: 9 additions & 9 deletions mod/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
Expand All @@ -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 {

Expand Down
6 changes: 2 additions & 4 deletions mod/workspace/templates/cluster.js
Original file line number Diff line number Diff line change
@@ -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(',')
Expand Down
6 changes: 2 additions & 4 deletions mod/workspace/templates/cluster_hex.js
Original file line number Diff line number Diff line change
@@ -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(',')
Expand Down
12 changes: 5 additions & 7 deletions mod/workspace/templates/get_last_location.js
Original file line number Diff line number Diff line change
@@ -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`
}
14 changes: 6 additions & 8 deletions mod/workspace/templates/mvt_cache.js
Original file line number Diff line number Diff line change
@@ -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;`
}
6 changes: 2 additions & 4 deletions mod/workspace/templates/wkt.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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};`
Expand Down

0 comments on commit 7f7495b

Please sign in to comment.