@opengis/fastify-table 1.3.30 → 1.3.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.3.30",
3
+ "version": "1.3.32",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -72,6 +72,11 @@ async function init(client) {
72
72
  const { table, args = [], time = 15 } = param;
73
73
  const seconds = typeof time !== 'number' || time < 0 ? 0 : time * 60;
74
74
 
75
+ if (seconds === 0 || config.disableCache) {
76
+ const data = await query(q, args || []);
77
+ return data;
78
+ }
79
+
75
80
  // CRUD table state
76
81
  const keyCacheTable = `pg:${table}:crud`;
77
82
  const crudInc = table ? await rclient.get(keyCacheTable) || 0 : 0;
@@ -1,5 +1,5 @@
1
1
  import { dump } from "js-yaml";
2
2
 
3
- const yml2json = (json) => dump(json || {});
3
+ const json2yml = (json) => dump(json || {});
4
4
 
5
- export default yml2json;
5
+ export default json2yml;
@@ -22,6 +22,8 @@ export default async function dataAPI(req, reply, called) {
22
22
  const timeArr = [Date.now()];
23
23
  const { uid } = user;
24
24
 
25
+ const checkQuery = (item) => (user?.user_type === 'superadmin' ? !item.includes('{{uid}}') : true);
26
+
25
27
  const hookData = await applyHook('preData', {
26
28
  pg, table: params?.table, id: params?.id, user,
27
29
  });
@@ -136,7 +138,7 @@ export default async function dataAPI(req, reply, called) {
136
138
  const bbox = meta?.bbox && queryBbox.filter((el) => !Number.isNaN(el))?.length === 4 ? `${meta.bbox} && 'box(${queryBbox[0]} ${queryBbox[1]},${queryBbox[2]} ${queryBbox[3]})'::box2d ` : undefined;
137
139
 
138
140
  const interfaceQuery = params?.query ? await handlebars.compile(params?.query)({ user, uid }) : undefined;
139
- const where = [(tokenData?.id || hookData?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable?.query, tokenData?.query, fData.q, search, accessQuery || '1=1', contextQuery, bbox, queryPolyline, interfaceQuery].filter((el) => el).filter((el) => (user?.user_type === 'superadmin' ? !el.includes('{{uid}}') : true));
141
+ const where = [(tokenData?.id || hookData?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable?.query, tokenData?.query, fData.q, search, accessQuery || '1=1', contextQuery, bbox, queryPolyline, interfaceQuery].filter((el) => el).filter((el) => checkQuery(el));
140
142
 
141
143
  // const cardColumns = cardSqlFiltered.length ? `,${cardSqlFiltered.map((el) => el.name)}` : '';
142
144
  const q = `select ${pk ? `"${pk}" as id,` : ''}
@@ -169,7 +171,7 @@ export default async function dataAPI(req, reply, called) {
169
171
  });
170
172
  }
171
173
 
172
- const filterWhere = [fData.q, search, bbox, queryPolyline, interfaceQuery, loadTable?.query, tokenData?.query].filter((el) => el);
174
+ const filterWhere = [fData.q, search, bbox, queryPolyline, interfaceQuery, loadTable?.query, tokenData?.query].filter((el) => el).filter((el) => checkQuery(el));
173
175
 
174
176
  const aggColumns = columns.filter((el) => el.agg).reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.agg }), {});
175
177
  const aggregates = dbColumns.map((el) => ({ name: el.name, type: pg.pgType?.[el.dataTypeID] })).filter((el) => ['numeric', 'double precision'].includes(el.type) && aggColumns[el.name]);
@@ -178,7 +180,7 @@ export default async function dataAPI(req, reply, called) {
178
180
  count(*) FILTER(WHERE ${filterWhere.join(' and ') || 'true'})::int as filtered
179
181
  ${aggregates.length ? `,${aggregates.map((el) => `${aggColumns[el.name]}(${el.name}) FILTER(WHERE ${filterWhere.join(' and ') || 'true'}) as ${el.name}`).join(',')}` : ''}
180
182
  from (select * ${sql?.filter(el => el.inline).map(el => `,(${el.sql})`).join('') || ''} from ${table} t ${sqlTable})q
181
- where ${[loadTable?.query, tokenData?.query, accessQuery, contextQuery].filter(el => el).join(' and ') || 'true'} `
183
+ where ${[loadTable?.query, tokenData?.query, accessQuery, contextQuery].filter(el => el).filter((el) => checkQuery(el)).join(' and ') || 'true'} `
182
184
  .replace(/{{uid}}/g, uid);
183
185
 
184
186
  if (query.sql === '2') { return qCount; }
@@ -0,0 +1,54 @@
1
+ import { readFileSync } from 'node:fs';
2
+
3
+ import getMeta from '../../../plugins/pg/funcs/getMeta.js';
4
+ import metaFormat from "../../../plugins/table/funcs/metaFormat/index.js";
5
+ import getTemplatePath from "../../../plugins/table/funcs/getTemplatePath.js";
6
+ import yml2json from "../../../plugins/yml/funcs/yml2json.js";
7
+
8
+ const infoList = [];
9
+
10
+ const infoTemplateList = getTemplatePath('info')?.filter?.((el, idx, arr) => arr.findIndex(item => item[1] === el[1]) === idx);
11
+ infoTemplateList?.forEach?.(el => {
12
+ const info = yml2json(readFileSync(el[1], 'utf-8'));
13
+ infoList.push(info);
14
+ });
15
+
16
+ export default async function dataInfo({
17
+ pg = pgClients.client, params = {}, query = {}, user = {},
18
+ }, reply) {
19
+ const timeStart = Date.now();
20
+ const { id } = params;
21
+ const { table } = query;
22
+
23
+ const filteredList = table ? infoList.filter(el => el.table === table) : infoList;
24
+
25
+ if (!filteredList?.length) {
26
+ return reply.status(404).send('empty info templates list');
27
+ }
28
+
29
+ if (!id) {
30
+ return reply.status(400).send('not enough params: id');
31
+ }
32
+
33
+ const result = await Promise.all(filteredList.filter(el => el.table && pg.pk?.[el.table]).map(async (el) => {
34
+ const { table, cls } = el;
35
+ const { rowCount = 0, rows = [] } = await pg.query(`select ${pg.pk?.[el.table]} as id, ${el.columns?.map?.(el => el?.replace?.(/'/g, "''")) || '*'} from ${table} where ${el.query || 'true'} and ${pg.pk?.[table]}=$1`, [id]);
36
+ if (rowCount > 0) {
37
+ const meta = await getMeta({ pg, table });
38
+ const columns = meta?.columns?.filter(item => el.columns?.includes?.(item.name))?.map?.(({ name, title, dataTypeID }) => ({ name, title, type: pg.pgType[dataTypeID] }));
39
+ return { table, columns, rows, cls, sufix: false };
40
+ }
41
+ return null;
42
+ }));
43
+
44
+ if (!result[0]?.table) {
45
+ return reply.status(404).send('object not found: ' + id);
46
+ }
47
+
48
+ await metaFormat(result[0], pg);
49
+
50
+ return {
51
+ time: Date.now() - timeStart,
52
+ ...result[0],
53
+ };
54
+ }
@@ -1,5 +1,6 @@
1
1
  import suggest from './controllers/suggest.js';
2
2
  import data from './controllers/data.js';
3
+ import dataInfo from './controllers/dataInfo.js';
3
4
 
4
5
  import card from './controllers/card.js';
5
6
  import search from './controllers/search.js';
@@ -16,7 +17,8 @@ async function plugin(fastify, config = {}) {
16
17
  const prefix = config.prefix || '/api';
17
18
  const policy = ['public'];
18
19
  fastify.get(`${prefix}/suggest/:data`, { config: { policy }, schema: suggestSchema }, suggest);
19
- fastify.get(`${prefix}/data/:table/:id?`, { config: { policy: ['public', 'no-sql'] }, schema: tableSchema }, data); // vs.crm.data.api с node
20
+ fastify.get(`${prefix}/data/:table/:id?`, { config: { policy: ['public', 'no-sql'] }, schema: tableSchema }, data);
21
+ fastify.get(`${prefix}/data-info/:id?`, { config: { policy: ['public', 'no-sql'] }, schema: tableSchema }, dataInfo);
20
22
 
21
23
  fastify.get(`${prefix}/card/:table/:id`, { config: { policy }, schema: tableSchema }, card);
22
24
  fastify.get(`${prefix}/search`, { config: { policy }, schema: searchSchema }, search);