@opengis/fastify-table 1.3.30 → 1.3.31

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.31",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -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;
@@ -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);