@opengis/fastify-table 1.0.51 → 1.0.52

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/Changelog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.52 - 28.06.2024
4
+
5
+ - add table get api
6
+
3
7
  ## 1.0.51 - 27.06.2024
4
8
 
5
9
  - add next-id api
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.51",
3
+ "version": "1.0.52",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -54,18 +54,6 @@ export default async function data(req) {
54
54
 
55
55
  const { rows } = await pg.query(q, (params.id ? [params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
56
56
 
57
- const formData = form ? (await getTemplate('form', form) || {}) : {};
58
- const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && formData[key]?.colModel);
59
- if (extraKeys?.length) {
60
- await Promise.all(rows?.map(async (row) => {
61
- await Promise.all(extraKeys?.map(async (key) => {
62
- const { colModel, table: extraTable, parent_id: parentId } = formData[key];
63
- const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
64
- Object.assign(row, { [key]: extraRows });
65
- }));
66
- }));
67
- }
68
-
69
57
  const total = keyQuery || params.id ? rows.length : await pg.queryCache(`select count(*) from ${table} t where ${where.join(' and ') || 'true'}`).then((el) => el?.rows[0]?.count);
70
58
 
71
59
  await metaFormat({ rows, table: params.table });
@@ -0,0 +1,40 @@
1
+ import getTemplate from './utils/getTemplate.js';
2
+ import getMeta from '../../pg/funcs/getMeta.js';
3
+
4
+ export default async function table(req) {
5
+ const { pg, params = {}, query = {} } = req;
6
+ if (!params.id) return { message: 'not enough params', status: 400 };
7
+
8
+ const loadTable = await getTemplate('table', params.table);
9
+ if (!loadTable) { return { message: 'not found', status: 404 }; }
10
+
11
+ const { table, columns, form } = loadTable;
12
+
13
+ const { pk, columns: dbColumns = [] } = await getMeta(table);
14
+ if (!pk) return { message: `table not found: ${table}`, status: 404 };
15
+
16
+ const cols = columns.map((el) => el.name || el).join(',');
17
+ const columnList = dbColumns.map((el) => el.name || el).join(',');
18
+
19
+ const where = [`"${pk}" = $1`, loadTable.query].filter((el) => el);
20
+ const geom = columnList.includes('geom') ? `st_asgeojson(geom)::json as geom,` : '';
21
+ const q = `select "${pk}" as id, ${geom} ${cols || '*'} from ${table} t where ${where.join(' and ') || 'true'} limit 1`;
22
+
23
+ if (query.sql === '1') return q;
24
+
25
+ const { rows } = await pg.query(q, [params.id]);
26
+
27
+ const formData = form ? (await getTemplate('form', form) || {}) : {};
28
+ const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && formData[key]?.colModel);
29
+ if (extraKeys?.length) {
30
+ await Promise.all(rows?.map(async (row) => {
31
+ await Promise.all(extraKeys?.map(async (key) => {
32
+ const { colModel, table: extraTable, parent_id: parentId } = formData[key];
33
+ const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
34
+ Object.assign(row, { [key]: extraRows });
35
+ }));
36
+ }));
37
+ }
38
+
39
+ return rows?.[0] || {};
40
+ }
package/table/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import suggest from './controllers/suggest.js';
2
2
  import data from './controllers/data.js';
3
+ import table from './controllers/table.js';
3
4
  import card from './controllers/card.js';
4
5
  import search from './controllers/search.js';
5
6
  import filter from './controllers/filter.js';
@@ -35,6 +36,7 @@ async function plugin(fastify, config = {}) {
35
36
 
36
37
  fastify.get(`${prefix}/suggest/:data`, {}, suggest);
37
38
  fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
39
+ fastify.get(`${prefix}/table/:table/:id`, { schema: tableSchema }, table);
38
40
  fastify.get(`${prefix}/card/:table/:id`, { schema: tableSchema }, card);
39
41
  fastify.get(`${prefix}/search`, { schema: searchTableSchema }, search);
40
42
  fastify.get(`${prefix}/filter/:table`, {}, filter);