UNPKG

@opengis/fastify-table

Version:

core-plugins

32 lines (31 loc) 1.23 kB
import pgClients from "../../../plugins/pg/pgClients.js"; import getSelectMeta from "../../../plugins/table/funcs/getSelectMeta.js"; import { addUserCls, addCustomCls, editUserCls, getUserCls, } from "../../../plugins/usercls/index.js"; export default async function addUserClsApi({ pg = pgClients.client, body, user }, reply) { const { name, description, alias, data, table } = body; const t1 = Date.now(); if (!user?.uid) { return reply.status(401).send({ error: "unauthorized", code: 401 }); } if (table) { await addCustomCls({ name, description, alias, table, uid: user.uid, data }, pg); return reply .status(200) .send({ time: Date.now() - t1, name, type: "custom" }); } const exists = await getUserCls(name, pg); if (exists) { await editUserCls({ name, description, alias, data }, pg); } else { await addUserCls({ name, description, alias, data }, pg); } // refresh metadata to avoid suggest API cache await getSelectMeta({ pg, name, nocache: 1, }); const result = await getUserCls(name, pg); return reply.status(200).send({ time: Date.now() - t1, name, ...result }); }