Implement info.delete() database method.

It deletes a (possibly deeply nested) element in the
`info` table.
This commit is contained in:
D. Berge
2021-05-20 18:16:26 +02:00
parent 1d38f6526b
commit 316117cb83

View File

@@ -0,0 +1,28 @@
const { setSurvey, transaction } = require('../connection');
async function del (projectId, path, opts = {}) {
const client = await setSurvey(projectId);
const [key, ...jsonpath] = (path||"").split("/").filter(i => i.length);
try {
const text = jsonpath.length
? `
UPDATE info
SET value = value #- $2
WHERE key = $1;
`
: `
DELETE FROM info
WHERE key = $1;
`;
const values = jsonpath.length ? [key, jsonpath] : [key];
await client.query(text, values);
} catch (err) {
console.error("ERROR", err);
throw err;
} finally {
client.release();
}
}
module.exports = del;