UNPKG

@opengis/bi

Version:

BI data visualization module

56 lines (42 loc) 2.08 kB
import { pgClients } from '@opengis/fastify-table/utils.js'; export default async function datasetEditComment({ pg = pgClients.client, body = {}, params = {}, }) { if (!params?.id) { return { message: 'not enough params: id', status: 400 }; } if (!Array.isArray(body.data) || !body.data?.length) { return { message: 'invalid param: body.data not an array / empty', status: 400 }; } const dataset = await pg.query('select dataset_id as id, table_name as table from bi.dataset where dataset_id=$1', [params.id]) .then(el => el.rows?.[0] || {}); if (!dataset?.table) { return { message: dataset?.id ? 'dataset table not set' : 'dataset not found', status: 404 }; } const { fields = [] } = pg.pk?.[dataset.table] ? await pg.query(`select * from ${dataset.table} limit 0`) : {}; if (!fields.length) { return { message: `table not found: ${dataset.table}`, status: 404 }; } const columnList = fields.map(el => el.name); const validData = body.data?.filter?.(el => el?.name && el?.comment && columnList.includes(el.name)); const q1 = validData.map(el => `COMMENT ON COLUMN ${dataset.table}."${el.name.replace(/'/g, "''")}" IS '${el.comment.replace(/'/g, "''")}'`).join(';'); if (q1) await pg.query(q1); const q2 = `select attrelid::regclass, attname, pg_catalog.col_description(attrelid,attnum) as title from pg_catalog.pg_attribute a where attnum > 0 and attname not in ('editor_id','editor_date','cdate','geom','id','uid','cdate') and atttypid::regtype not in ('json','geometry') and pg_catalog.col_description(attrelid,attnum) is not null`; const { rows = [] } = await pg.query(q2); const title = rows .filter(el => el.attrelid === dataset.table) .reduce((p, el) => ({ ...p, [el.attname]: el.title }), {}); const res = await pg.query(`update bi.dataset set setting=coalesce(setting::jsonb, '{}'::jsonb)||$1::jsonb where dataset_id=$2 returning dataset_id as id, setting`, [{ title }, dataset.id]); return res.rows?.[0]; }