Add map layer functions to database interface

This commit is contained in:
D. Berge
2023-09-11 10:12:46 +02:00
parent b2d1798338
commit 398c702004
4 changed files with 60 additions and 1 deletions

View File

@@ -3,5 +3,6 @@ module.exports = {
bbox: require('./bbox'),
preplot: require('./preplot'),
raw: require('./raw'),
final: require('./final')
final: require('./final'),
layer: require('./layer')
};

View File

@@ -0,0 +1,28 @@
const { setSurvey } = require('../../../connection');
async function get (projectId, layerName, options = {}) {
const client = await setSurvey(projectId);
const text = `
SELECT (data->>'data')::json data
FROM file_data
WHERE data->>'type' = 'map_layer'
AND data->>'format' = 'geojson'
AND data->>'name' = $1;
`;
const values = [ layerName ];
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(row => row.data);
} else {
throw {status: 404};
}
}
module.exports = get;

View File

@@ -0,0 +1,5 @@
module.exports = {
list: require('./list'),
get: require('./get')
};

View File

@@ -0,0 +1,25 @@
const { setSurvey } = require('../../../connection');
async function list (projectId, options = {}) {
const client = await setSurvey(projectId);
const text = `
SELECT DISTINCT data->>'name' name
FROM file_data
WHERE data->>'type' = 'map_layer'
AND data->>'format' = 'geojson';
`;
const res = await client.query(text);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(row => row.name);
} else {
throw {status: 404};
}
}
module.exports = list;