@opengis/fastify-table 1.2.46 → 1.2.47

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.2.46",
3
+ "version": "1.2.47",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,4 +1,4 @@
1
- import { config, getTemplate, pgClients, getMeta } from "../../../utils.js";
1
+ import { config, getTemplate, pgClients } from "../../../utils.js";
2
2
 
3
3
  const defaultTable = 'crm.extra_data';
4
4
 
@@ -11,48 +11,42 @@ function format(key, value, schema) {
11
11
  }
12
12
 
13
13
  export default async function extraDataGet({
14
- table: mainTable, form, id,
14
+ rows = [], table, form,
15
15
  }, pg = pgClients.client) {
16
- if (!id || !form || !mainTable) {
16
+ const ids = rows.map(row => row.id).filter(el => el);
17
+
18
+ if (!ids?.length || !form || !table) {
17
19
  return null;
18
20
  }
19
21
 
20
22
  const loadTemplate = await getTemplate('form', form);
21
23
  if (!loadTemplate?.extra) return null;
22
24
 
23
- const table = config.extraData?.[mainTable]
24
- || config.extraData?.[mainTable.split('.').shift()]
25
+ const extraDataTable = config.extraData?.[table]
26
+ || config.extraData?.[table.split('.').shift()]
25
27
  || config.extraData?.['default']
26
28
  || config.extraData
27
29
  || defaultTable;
28
30
 
29
- const { pk: mainPK } = await getMeta({ pg, table: mainTable });
30
- const { pk, columns: extraColumns = [] } = await getMeta({ pg, table });
31
-
32
- if (!mainPK) {
33
- return { error: `table pk not found: ${mainTable}`, status: 404 };
31
+ if (!pg.pk?.[table]) {
32
+ return { error: `table pk not found: ${table}`, status: 404 };
34
33
  }
35
34
 
36
- if (!pk) {
37
- return { error: `extra table pk not found: ${table}`, status: 404 };
35
+ if (!pg.pk?.[extraDataTable]) {
36
+ return { error: `extra table pk not found: ${extraDataTable}`, status: 404 };
38
37
  }
39
38
 
40
- const mode = extraColumns.find((col) => col?.name === mainPK)
41
- ? 'column'
42
- : 'property';
43
-
44
- const { rows = [] } = await pg.query(`select * from ${table} where ${mode === 'column' ? mainPK : 'object_id'}=$1`, [id]);
45
-
46
- if (mode === 'column') {
47
- const res = Object.keys(rows[0] || {})
48
- .map(key => [key, format(key, rows?.[0]?.[key], loadTemplate?.schema)])
49
- .filter(el => el[1])
50
- .reduce((acc, curr) => Object.assign(acc, { [curr[0]]: curr[1] }), {});
51
- Object.assign(res, { id: rows?.[0]?.[pg.pk?.[mainTable || '']] });
52
- return res;
39
+ const { rows: extraRows = [] } = await pg.query(`select * from ${extraDataTable} where object_id=any($1::text[])`, [ids]);
40
+
41
+ if (rows?.length && extraRows?.length) {
42
+ rows.forEach(row => {
43
+ Object.assign(row, {
44
+ ...extraRows
45
+ .filter(el => el.object_id === row.id)
46
+ .reduce((acc, curr) => Object.assign(acc, {
47
+ [curr.property_key]: format(curr.property_key, curr.value_text, loadTemplate?.schema)
48
+ }), {})
49
+ });
50
+ });
53
51
  }
54
-
55
- const res = rows.reduce((acc, curr) => Object.assign(acc, { [curr.property_key]: format(curr.property_key, curr.value_text, loadTemplate?.schema) }), {});
56
- Object.assign(res, { id: rows?.[0].object_id });
57
- return res;
58
52
  };
@@ -80,7 +80,7 @@ export default async function dataAPI(req, reply, called) {
80
80
  });
81
81
  }
82
82
 
83
- const checkFilter = [query.filter, query.search, query.state, query.custom, loadTable?.extra].filter((el) => el).length;
83
+ const checkFilter = [query.filter, query.search, query.state, query.custom].filter((el) => el).length;
84
84
  const fData = checkFilter ? await getFilterSQL({
85
85
  pg,
86
86
  table: params.table,
@@ -119,7 +119,7 @@ export default async function dataAPI(req, reply, called) {
119
119
 
120
120
  ${dbColumns.find((el) => el.name === 'geom' && pg.pgType?.[el.dataTypeID] === 'geometry') ? ',st_asgeojson(geom)::json as geom' : ''}
121
121
  from (select * from ${table} where ${sqlTable ? 'true' : (where.join(' and ') || 'true')} ${order}) t
122
- ${sqlTable} ${loadTable?.extra && fData?.extraSqlList ? fData?.extraSqlList : ''}
122
+ ${sqlTable}
123
123
  ${params.id ? cardSqlTable : ''}
124
124
  where ${where.join(' and ') || 'true'}
125
125
  ${order} ${offset} limit ${limit}`
@@ -162,14 +162,7 @@ export default async function dataAPI(req, reply, called) {
162
162
  const { total, filtered } = counts || {};
163
163
  const agg = Object.keys(counts).filter(el => !['total', 'filtered'].includes(el)).reduce((acc, el) => ({ ...acc, [el]: counts[el] }), {});
164
164
 
165
- if (hookData?.id || params.id) {
166
- const extraData = await extraDataGet({
167
- table: loadTable?.table,
168
- id: hookData?.id || params.id,
169
- form,
170
- }, pg);
171
- Object.assign(rows[0] || {}, { ...extraData || {} });
172
- }
165
+ await extraDataGet({ rows, table: loadTable?.table, form }, pg);
173
166
 
174
167
  await metaFormat({ rows, table: tokenData?.table || hookData?.table || params.table }, pg);
175
168