@opengis/fastify-table 1.3.14 → 1.3.15

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.14",
3
+ "version": "1.3.15",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -38,6 +38,9 @@ export default async function dataInsert({
38
38
 
39
39
  const filterData = Object.keys(data)
40
40
  .filter((el) => !['cdate', 'editor_date'].includes(el) && (typeof data[el] === 'boolean' ? true : data[el]) && names.includes(el)).map((el) => [el, data[el]]);
41
+ if (!filterData?.length) {
42
+ return null;
43
+ }
41
44
 
42
45
  const insertQuery = `insert into ${table}
43
46
 
@@ -27,43 +27,21 @@ export default async function extraData({
27
27
  || defaultTable;
28
28
 
29
29
  const { pk: mainPK, columns: mainColumns = [] } = await getMeta({ pg, table });
30
- const { pk, columns: extraColumns = [] } = await getMeta({ pg, table: extraDataTable });
31
30
 
32
31
  if (!mainPK) {
33
32
  return { error: `table pk not found: ${table}`, status: 404 };
34
33
  }
35
34
 
36
- if (!pk) {
35
+ if (!pg.pk?.[extraDataTable]) {
37
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';
39
+ Object.assign(data || {}, { object_id: id });
43
40
 
44
- if (mode === 'property') {
45
- Object.assign(data || {}, { object_id: id });
46
- }
47
-
48
- const deleteRes = mode === 'property'
49
- ? await pg.query(`delete from ${extraDataTable} where object_id=$1 and property_key = any($2::text[]) returning *`, [id, Object.keys(loadTemplate?.schema || {})])
50
- : await pg.query(`delete from ${extraDataTable} where ${mainPK}=$1 returning *`, [id]);
41
+ const deleteRes = await pg.query(`delete from ${extraDataTable} where object_id=$1 and property_key = any($2::text[]) returning *`, [id, Object.keys(loadTemplate?.schema || {})]);
51
42
 
52
43
  if (!data) {
53
- const res1 = mode === 'property'
54
- ? deleteRes?.rows?.reduce((acc, curr) => Object.assign(acc, { [curr.property_key]: format(curr.property_key, curr.value_text, loadTemplate?.schema) }), {})
55
- : deleteRes?.rows?.[0];
56
- return res1;
57
- }
58
-
59
- if (mode === 'column') {
60
- const filteredData = Object.keys(data)
61
- .filter(key => Object.keys(loadTemplate?.schema || {}).includes(key))
62
- .reduce((acc, curr) => Object.assign(acc, { [curr]: data[curr] }), {});
63
- Object.assign(filteredData, { [mainPK]: id });
64
- const res = await dataInsert({ pg, table: extraDataTable, data: filteredData, uid }).then(el => el.rows?.[0] || {});
65
- Object.assign(res, { id: res?.[pg.pk?.[table || '']] });
66
- return res;
44
+ return deleteRes?.rows?.reduce?.((acc, curr) => Object.assign(acc, { [curr.property_key]: format(curr.property_key, curr.value_text, loadTemplate?.schema) }), {});
67
45
  }
68
46
 
69
47
  const rows = Object.keys(data || {})
@@ -2,6 +2,8 @@ import {
2
2
  config, getAccess, getTemplate, getMeta, setToken, applyHook, getToken, pgClients,
3
3
  } from '../../../../utils.js';
4
4
 
5
+ import extraDataGet from '../../../plugins/extra/extraDataGet.js';
6
+
5
7
  export default async function tableAPI(req) {
6
8
  const {
7
9
  pg = pgClients.client, params, user = {}, query = {},
@@ -85,8 +87,12 @@ export default async function tableAPI(req) {
85
87
  array: 1,
86
88
  })[0];
87
89
  }
90
+
91
+ await extraDataGet({ rows: [data], table: tableName, form: hookData?.form || tokenData?.form || loadTable.form }, pg);
92
+
88
93
  const res = await applyHook('afterTable', {
89
94
  pg, table: tableName, payload: [data], user,
90
95
  });
96
+
91
97
  return res || data || {};
92
98
  }