@opengis/fastify-table
Version:
core-plugins
50 lines (49 loc) • 1.82 kB
JavaScript
import { handlebars, getToken, getTemplate, getMeta, pgClients, } from "../../../../utils.js";
import getTableData from "./tableData.js";
export default async function cardTabData({ pg = pgClients.client, query = {}, params = {}, user = {}, }, reply) {
const { token } = params;
const { sql } = query;
const { uid } = user;
if (!token) {
return reply.status(400).send("not enough params: token");
}
const time = Date.now();
const props = await getToken({
token,
mode: "w",
uid,
json: 1,
});
if (!props || !props?.table) {
return reply.status(403).send("token is invalid");
}
const tableTemplate = await getTemplate("table", props.table);
const { columns = [] } = tableTemplate?.columns || tableTemplate?.colModel
? { columns: tableTemplate?.columns || tableTemplate?.colModel }
: await getMeta({ pg, table: props.table });
const filter = await handlebars.compile(props.query?.replace?.(/@id/g, "{{id}}") || "1=1")(props);
const { message, rows = [] } = await getTableData({
pg,
params: { table: props.table },
user,
query: { filter, sql },
}, reply);
if (message)
return { message };
if (props.columns?.length && rows?.length) {
const allowList = ["id", "token"]
.concat(props.columns)
.map((el) => [el, `${el}_text`])
.flat();
rows.forEach((row) => Object.keys(row).forEach((el) => {
if (!allowList.includes(el))
delete row[el];
}));
return {
time: Date.now() - time,
rows,
columns: columns.filter((col) => props.columns.includes(col.name)),
};
}
return { time: Date.now() - time, rows, columns };
}