mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 09:47:08 +00:00
Implement info.delete() database method.
It deletes a (possibly deeply nested) element in the `info` table.
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user