@opengis/fastify-table
Version:
core-plugins
36 lines (35 loc) • 1.64 kB
JavaScript
import config from "../../../../config.js";
import pgClients from "../../pg/pgClients.js";
import getTemplate from "./getTemplate.js";
const loadCls = {};
const sqls = {
sql: "select data from admin.user_cls where name=$1 union all select data from admin.cls where name=$1",
json: `select json_agg(json_build_object('id', code, 'text', name, 'icon', icon, 'color', color)) as data from admin.user_cls where parent=$1
union all select json_agg(json_build_object('id', code, 'text', name, 'icon', icon, 'color', color)) as data from admin.cls where parent=$1`,
};
export default async function getSelect(name, pg = pgClients.client, nocache = false) {
if (loadCls[name] && !config.local && !nocache) {
return loadCls[name];
}
const clsDataGIT = await getTemplate(["cls", "select"], name);
const { type } = pg.pk?.["admin.user_cls"]
? await pg
.query("select type from admin.user_cls where parent is null and name=$1 union all select type from admin.cls where parent is null and name=$1 limit 1", [name])
.then((el) => el.rows?.[0] || {})
: {};
const clsDataDB = sqls[type || ""]
? await pg
.query(sqls[type || ""], [name])
.then((el) => el.rows?.find((item) => item?.data)?.data)
: undefined;
const clsData = clsDataDB ?? clsDataGIT;
if (!clsData)
return null;
// console.log(clsData);
if (clsData && Array.isArray(clsData)) {
loadCls[name] = { arr: clsData };
return loadCls[name];
}
loadCls[name] = clsData.sql ? clsData : { sql: clsData };
return loadCls[name];
}