Files
dougal-software/lib/www/server/lib/db/info/get.js
D. Berge db25878fdd Add info API endpoint.
It queries a project's `info` table.
2020-09-09 15:55:04 +02:00

31 lines
677 B
JavaScript

const { setSurvey } = require('../connection');
async function get (projectId, path, opts = {}) {
const client = await setSurvey(projectId);
const [key, ...subkey] = path.split("/");
const text = `
SELECT value
FROM info
WHERE key = $1;
`;
console.log("k, sk", key, subkey);
const res = await client.query(text, [key]);
client.release();
const value = res.rows[0] && res.rows[0].value;
if (subkey.length) {
console.log("Reducing value");
const res = subkey.reduce( (obj, idx) => typeof obj != "undefined" ? obj[idx] : obj, value);
console.log(res);
return res;
} else {
console.log("Returning value");
return value;
}
}
module.exports = get;