UNPKG

@opengis/fastify-table

Version:

core-plugins

60 lines (47 loc) 2.95 kB
import getToken from '../../../plugins/crud/funcs/getToken.js'; import getOpt from '../../../plugins/crud/funcs/getOpt.js'; import getTemplate from '../../../plugins/table/funcs/getTemplate.js'; import getMeta from '../../../plugins/pg/funcs/getMeta.js'; import pgClients from '../../../plugins/pg/pgClients.js'; import getData from '../functions/getData.js'; export default async function getTableData(req, reply, called) { const { user = {}, params = {}, headers = {}, query = {}, pg = pgClients.client, contextQuery: contextQuery1, sufix = true, } = req; const { id } = params || {}; const tokenData1 = await getOpt(params.table, user?.uid); const tokenData2 = await getToken({ uid: user?.uid, token: params.table, json: true, }); const tokenData = tokenData1 || tokenData2; if (tokenData?.table) { const loadTable = await getTemplate('table', tokenData.table); if (!loadTable) { const { rows = [], fields = [] } = await pg.query(`select ${tokenData.columns || '*'} from ${tokenData.table} where ${tokenData.query || 'true'} limit 10`); const meta = await getMeta({ pg, table: tokenData.table }); const columns = (meta?.columns || fields).map(el => ({ name: el.name, type: pg.pgType?.[el.dataTypeID], title: el.title })); return { rows, columns }; } const resp = await getData({ pg, params: { id, table: tokenData.table }, headers, query, user, contextQuery: [contextQuery1, tokenData.query].filter(Boolean).join(' and '), sufix, }, reply, called); if (resp?.addToken && tokenData.obj) { Object.assign(resp, { addToken: params.table }); } return resp; } // Це можна 1 раз на старті const interfaceName = req.headers?.referer?.match(/.*\/([^?]+)/)?.[1]; const { query: interfaceQuery } = interfaceName && pg?.pk ? await pg.query('select query from admin.routes where route_id=$1', [interfaceName]) .then(el => el.rows?.[0] || {}) : {}; // це можна від користувача const q = `select attr, (select rule_values from admin.account_grants where account_id in (select account_id from admin.account_users where user_uid=$2) and rule_id=r.rule_id limit 1) from admin.rules r where (select route_id from admin.routes where $1 in (alias,table_name)) = any(routes)`; const { rows = [] } = pg?.pk?.['admin.rules'] ? await pg.query(q, [params.table, user.uid]) : {}; const context = rows.filter(el => el.rule_values).map(el => `${el.attr} = any ('{${el.rule_values}}')`).join(' and '); const contextQuery = [contextQuery1, interfaceQuery, context].filter(Boolean).join(' and ') || ' 2=2 '; const res = await getData({ pg, params, query, headers, user, contextQuery, sufix, }, reply, called); return res; }