@opengis/fastify-table 1.2.45 → 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,4 +1,4 @@
|
|
|
1
|
-
import { config, getTemplate, pgClients
|
|
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
|
-
|
|
14
|
+
rows = [], table, form,
|
|
15
15
|
}, pg = pgClients.client) {
|
|
16
|
-
|
|
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
|
|
24
|
-
|| config.extraData?.[
|
|
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
|
-
|
|
30
|
-
|
|
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: ${
|
|
35
|
+
if (!pg.pk?.[extraDataTable]) {
|
|
36
|
+
return { error: `extra table pk not found: ${extraDataTable}`, status: 404 };
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
};
|
|
@@ -10,13 +10,16 @@ import getOptimizedQuery from './util/getOptimizedQuery.js';
|
|
|
10
10
|
const defaultTable = 'crm.extra_data';
|
|
11
11
|
|
|
12
12
|
function getExtraQuery(mainColumns, extraColumns, schema, table, pk, mode = 'property') {
|
|
13
|
-
const
|
|
13
|
+
const extraKeys = mode === 'column'
|
|
14
|
+
? Object.keys(schema || {}).filter(key => !mainColumns.map(el => el?.name).includes(key) && extraColumns.map(el => el?.name).includes(key))
|
|
15
|
+
: Object.keys(schema || {}).filter(key => !mainColumns.map(el => el?.name).includes(key));
|
|
16
|
+
|
|
14
17
|
if (mode === 'column') {
|
|
15
|
-
return { q: `left join lateral (select ${
|
|
18
|
+
return { q: `left join lateral (select ${extraKeys.map(key => `"${key}"`).join(',')} from ${table} where ${pk}=t.${pk} limit 1) extra on 1=1`, extraKeys };
|
|
16
19
|
}
|
|
17
20
|
return {
|
|
18
|
-
q: `left join lateral (select
|
|
19
|
-
|
|
21
|
+
q: extraKeys.map((el, idx) => `left join lateral (select value_text as "${el.replace(/'/g, "''")}" from ${table} where property_key='${el.replace(/'/g, "''")}' and object_id=t.${pk} limit 1) extra${idx} on 1=1`).join(' '),
|
|
22
|
+
extraKeys,
|
|
20
23
|
};
|
|
21
24
|
}
|
|
22
25
|
|
|
@@ -41,7 +44,7 @@ export default async function getFilterSQL({
|
|
|
41
44
|
const mode = fieldsExtra.find((col) => col?.name === pg.pk?.[body?.table || table]) ? 'column' : 'property';
|
|
42
45
|
|
|
43
46
|
const { fields: fieldsModel = [] } = body?.table && pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
|
|
44
|
-
const { q: extraSqlList,
|
|
47
|
+
const { q: extraSqlList, extraKeys } = pg.pk?.[extraDataTable] && pg.pk?.[body?.table]
|
|
45
48
|
? getExtraQuery(fieldsModel, fieldsExtra, loadTemplate?.schema, extraDataTable, pg.pk[body?.table || table], mode)
|
|
46
49
|
: {};
|
|
47
50
|
|
|
@@ -55,7 +58,7 @@ export default async function getFilterSQL({
|
|
|
55
58
|
}).join(' ')
|
|
56
59
|
: '';
|
|
57
60
|
const fieldQuery = `select * from ${body?.table || table} t ${sqlList || ''} ${extraSqlList || ''} limit 0`;
|
|
58
|
-
const { fields = [] } = await pg.query(fieldQuery);
|
|
61
|
+
const { fields = [] } = !extra || mode === 'column' ? await pg.query(fieldQuery) : { fields: extraKeys.map(el => ({ name: el.name, dataTypeID: 25 })) };
|
|
59
62
|
const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
|
|
60
63
|
const searchColumn = body?.search_column || body?.meta?.search || autoSearchColumn;
|
|
61
64
|
const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
|
|
@@ -74,7 +77,7 @@ export default async function getFilterSQL({
|
|
|
74
77
|
.concat(body?.filters || [])) /* .concat(extraFilters || []).concat(customFilters || []) */
|
|
75
78
|
|
|
76
79
|
?.map(async (el) => {
|
|
77
|
-
if (el.name &&
|
|
80
|
+
if (el.name && extraKeys?.includes?.(el.name)) {
|
|
78
81
|
Object.assign(el, { extra: { table: extraDataTable, mode } });
|
|
79
82
|
}
|
|
80
83
|
if (!el?.data) return el;
|
|
@@ -114,6 +117,8 @@ export default async function getFilterSQL({
|
|
|
114
117
|
|
|
115
118
|
q,
|
|
116
119
|
optimizedSQL,
|
|
120
|
+
extraSqlList,
|
|
121
|
+
extraKeys,
|
|
117
122
|
tableCount,
|
|
118
123
|
table: modelQuery,
|
|
119
124
|
// filter parts
|
|
@@ -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
|
-
|
|
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
|
|