@opengis/fastify-table 1.2.24 → 1.2.25
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,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
config, getTemplate, getFilterSQL, getMeta, metaFormat, getAccess, setToken, gisIRColumn, applyHook, handlebars, handlebarsSync, getSelect, setOpt,
|
|
2
|
+
config, getTemplate, getFilterSQL, getMeta, metaFormat, getAccess, setToken, gisIRColumn, applyHook, handlebars, handlebarsSync, getSelect, setOpt, getOpt,
|
|
3
3
|
} from '../../../../utils.js';
|
|
4
4
|
|
|
5
5
|
import conditions from './utils/conditions.js';
|
|
@@ -26,11 +26,13 @@ export default async function dataAPI(req) {
|
|
|
26
26
|
return { message: hookData?.message, status: hookData?.status };
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
const
|
|
30
|
-
if (!loadTable) { return { message: 'template not found', status: 404 }; }
|
|
29
|
+
const tokenData = await getOpt(params.table, user?.uid);
|
|
31
30
|
|
|
32
|
-
const
|
|
33
|
-
|
|
31
|
+
const loadTable = await getTemplate('table', tokenData?.table || hookData?.table || params.table);
|
|
32
|
+
if (!loadTable && !(tokenData?.table && pg.pk?.[tokenData?.table])) { return { message: 'template not found', status: 404 }; }
|
|
33
|
+
|
|
34
|
+
const id = tokenData?.id || hookData?.id || params?.id;
|
|
35
|
+
const { actions = [], query: accessQuery } = await getAccess({ table: tokenData?.table || hookData?.table || params.table, id, user }) || {};
|
|
34
36
|
|
|
35
37
|
if (!actions.includes('view') && !config?.local) {
|
|
36
38
|
return { message: 'access restricted', status: 403 };
|
|
@@ -38,12 +40,12 @@ export default async function dataAPI(req) {
|
|
|
38
40
|
|
|
39
41
|
const {
|
|
40
42
|
table, columns = [], sql, cardSql, filters, form, meta, sqlColumns, public: ispublic,
|
|
41
|
-
} = loadTable;
|
|
43
|
+
} = loadTable || tokenData;
|
|
42
44
|
|
|
43
45
|
const tableMeta = await getMeta(table);
|
|
44
46
|
if (tableMeta?.view) {
|
|
45
|
-
if (!loadTable?.key) return { message: `key not found: ${table}`, status: 404 };
|
|
46
|
-
Object.assign(tableMeta, { pk: loadTable?.key });
|
|
47
|
+
if (!loadTable?.key && !tokenData?.key) return { message: `key not found: ${table}`, status: 404 };
|
|
48
|
+
Object.assign(tableMeta, { pk: loadTable?.key || tokenData?.key });
|
|
47
49
|
}
|
|
48
50
|
const { pk, columns: dbColumns = [] } = tableMeta || {};
|
|
49
51
|
|
|
@@ -84,26 +86,26 @@ export default async function dataAPI(req) {
|
|
|
84
86
|
json: 1,
|
|
85
87
|
}) : {};
|
|
86
88
|
|
|
87
|
-
const keyQuery = query.key && loadTable
|
|
89
|
+
const keyQuery = query.key && (loadTable?.key || tokenData?.key) && !(hookData?.id || tokenData?.id || params.id) ? `${loadTable?.key || tokenData?.key}=$1` : null;
|
|
88
90
|
|
|
89
91
|
const limit = Math.min(maxLimit, +(query.limit || 20));
|
|
90
92
|
|
|
91
93
|
const offset = query.page && query.page > 0 ? ` offset ${(query.page - 1) * limit}` : '';
|
|
92
94
|
// id, query, filter
|
|
93
|
-
const [orderColumn, orderDir] = (query.order || loadTable
|
|
95
|
+
const [orderColumn, orderDir] = (query.order || loadTable?.order || '').split(/[- ]/);
|
|
94
96
|
|
|
95
97
|
const order = query.order && columnList.includes(orderColumn) && orderColumn?.length
|
|
96
98
|
? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}`
|
|
97
|
-
: `order by ${(loadTable
|
|
98
|
-
const search = loadTable
|
|
99
|
-
? `(${loadTable
|
|
99
|
+
: `order by ${(loadTable?.order || 'true')}`;
|
|
100
|
+
const search = loadTable?.meta?.search && query.search
|
|
101
|
+
? `(${loadTable?.meta?.search?.split(',')?.map(el => `${el} ilike '%${query.search.replace(/%/g, '\\%').replace(/'/g, "''")}%'`).join(' or ')})`
|
|
100
102
|
: null;
|
|
101
103
|
const queryBbox = query?.bbox ? query.bbox.replace(/ /g, ',').split(',')?.map((el) => el - 0) : [];
|
|
102
104
|
const queryPolyline = meta?.bbox && query?.polyline ? `ST_Contains(ST_MakePolygon(ST_LineFromEncodedPolyline('${query?.polyline}')),${meta.bbox})` : undefined;
|
|
103
105
|
const bbox = meta?.bbox && queryBbox.filter((el) => !Number.isNaN(el))?.length === 4 ? `${meta.bbox} && 'box(${queryBbox[0]} ${queryBbox[1]},${queryBbox[2]} ${queryBbox[3]})'::box2d ` : undefined;
|
|
104
106
|
|
|
105
107
|
const interfaceQuery = params?.query ? await handlebars.compile(params?.query)({ user, uid }) : undefined;
|
|
106
|
-
const where = [(hookData?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable
|
|
108
|
+
const where = [(tokenData?.id || hookData?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable?.query, tokenData?.query, fData.q, search, accessQuery || '1=1', bbox, queryPolyline, interfaceQuery].filter((el) => el).filter((el) => (user?.user_type === 'superadmin' ? !el.includes('{{uid}}') : true));
|
|
107
109
|
|
|
108
110
|
// const cardColumns = cardSqlFiltered.length ? `,${cardSqlFiltered.map((el) => el.name)}` : '';
|
|
109
111
|
const q = `select ${pk ? `"${pk}" as id,` : ''}
|
|
@@ -122,9 +124,9 @@ export default async function dataAPI(req) {
|
|
|
122
124
|
|
|
123
125
|
if (query.sql === '1') { return q; }
|
|
124
126
|
|
|
125
|
-
const { rows } = await pg.query(q, (hookData?.id || params.id ? [hookData?.id || params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
|
|
127
|
+
const { rows } = await pg.query(q, (tokenData?.id || hookData?.id || params.id ? [tokenData?.id || hookData?.id || params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
|
|
126
128
|
|
|
127
|
-
const filterWhere = [fData.q, search, bbox, queryPolyline, interfaceQuery, loadTable
|
|
129
|
+
const filterWhere = [fData.q, search, bbox, queryPolyline, interfaceQuery, loadTable?.query, tokenData?.query].filter((el) => el);
|
|
128
130
|
|
|
129
131
|
const aggColumns = columns.filter((el) => el.agg).reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.agg }), {});
|
|
130
132
|
const aggregates = dbColumns.map((el) => ({ name: el.name, type: pg.pgType[el.dataTypeID] })).filter((el) => ['numeric', 'double precision'].includes(el.type) && aggColumns[el.name]);
|
|
@@ -133,22 +135,22 @@ export default async function dataAPI(req) {
|
|
|
133
135
|
count(*) FILTER(WHERE ${filterWhere.join(' and ') || 'true'})::int as filtered
|
|
134
136
|
${aggregates.length ? `,${aggregates.map((el) => `${aggColumns[el.name]}(${el.name}) FILTER(WHERE ${filterWhere.join(' and ') || 'true'}) as ${el.name}`).join(',')}` : ''}
|
|
135
137
|
from ${table} t ${sqlTable}
|
|
136
|
-
where ${[loadTable
|
|
138
|
+
where ${[loadTable?.query, tokenData?.query, accessQuery].filter(el => el).join(' and ') || 'true'} `
|
|
137
139
|
.replace(/{{uid}}/g, uid);
|
|
138
140
|
|
|
139
141
|
if (query.sql === '2') { return qCount; }
|
|
140
142
|
|
|
141
|
-
const counts = keyQuery || hookData?.id || params.id
|
|
143
|
+
const counts = keyQuery || tokenData?.id || hookData?.id || params.id
|
|
142
144
|
? { total: rows.length, filtered: rows.length }
|
|
143
|
-
: await pg.queryCache(qCount, { table: loadTable
|
|
145
|
+
: await pg.queryCache(qCount, { table: loadTable?.table || tokenData?.table, time: 5 }).then(el => el?.rows[0] || {});
|
|
144
146
|
|
|
145
147
|
const { total, filtered } = counts;
|
|
146
148
|
const agg = Object.keys(counts).filter(el => !['total', 'filtered'].includes(el)).reduce((acc, el) => ({ ...acc, [el]: counts[el] }), {});
|
|
147
149
|
|
|
148
|
-
await metaFormat({ rows, table: hookData?.table || params.table });
|
|
150
|
+
await metaFormat({ rows, table: tokenData?.table || hookData?.table || params.table });
|
|
149
151
|
|
|
150
152
|
const status = [];
|
|
151
|
-
if (loadTable
|
|
153
|
+
if (loadTable?.meta?.status) {
|
|
152
154
|
const statusColumn = loadTable.meta?.cls?.[loadTable.meta?.status]
|
|
153
155
|
? { name: loadTable.meta?.status, data: loadTable.meta?.cls?.[loadTable.meta?.status] }
|
|
154
156
|
: loadTable.columns.find(col => col.name === loadTable.meta?.status) || {};
|
|
@@ -159,16 +161,16 @@ export default async function dataAPI(req) {
|
|
|
159
161
|
?.forEach(el => status.push({ ...el, count: rows.filter(row => el.id === row[statusColumn.name]).length }));
|
|
160
162
|
}
|
|
161
163
|
|
|
162
|
-
const template = await getTemplate('card', hookData?.table || params.table);
|
|
164
|
+
const template = await getTemplate('card', tokenData?.table || hookData?.table || params.table);
|
|
163
165
|
const index = template?.find(el => el[0] === 'index.yml')?.[1] || {};
|
|
164
166
|
|
|
165
167
|
const html = {};
|
|
166
168
|
const { panels = [] } = index;
|
|
167
169
|
|
|
168
|
-
|
|
170
|
+
const tokens = {};
|
|
171
|
+
if (template && (tokenData?.id || hookData?.id || params.id)) {
|
|
169
172
|
|
|
170
173
|
// tokens result
|
|
171
|
-
const tokens = {};
|
|
172
174
|
if (index?.tokens && typeof index?.tokens === 'object' && !Array.isArray(index?.tokens)) {
|
|
173
175
|
Object.keys(index.tokens || {})
|
|
174
176
|
.filter(key => index?.tokens[key]?.public
|
|
@@ -235,7 +237,8 @@ export default async function dataAPI(req) {
|
|
|
235
237
|
const res = {
|
|
236
238
|
time: Date.now() - time,
|
|
237
239
|
public: ispublic,
|
|
238
|
-
|
|
240
|
+
tokens,
|
|
241
|
+
card: loadTable?.card,
|
|
239
242
|
actions,
|
|
240
243
|
total,
|
|
241
244
|
filtered,
|
|
@@ -255,7 +258,7 @@ export default async function dataAPI(req) {
|
|
|
255
258
|
// console.log({ add: loadTable.table, form: loadTable.form });
|
|
256
259
|
if (uid && actions.includes('add')) {
|
|
257
260
|
const addTokens = setToken({
|
|
258
|
-
ids: [JSON.stringify({ table: hookData?.table || params.table, form: loadTable
|
|
261
|
+
ids: [JSON.stringify({ table: tokenData?.table || hookData?.table || params.table, form: loadTable?.form })],
|
|
259
262
|
uid,
|
|
260
263
|
array: 1,
|
|
261
264
|
});
|
|
@@ -263,7 +266,7 @@ export default async function dataAPI(req) {
|
|
|
263
266
|
}
|
|
264
267
|
|
|
265
268
|
const result = await applyHook('afterData', {
|
|
266
|
-
table: loadTable
|
|
269
|
+
table: loadTable?.table || tokenData?.table, payload: res, user,
|
|
267
270
|
});
|
|
268
271
|
|
|
269
272
|
return result || res;
|