@opengis/fastify-table 1.1.118 → 1.1.120
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
|
@@ -5,7 +5,7 @@ export default async function cronApi(req) {
|
|
|
5
5
|
params = {}, user = {}, hostname,
|
|
6
6
|
} = req;
|
|
7
7
|
|
|
8
|
-
if ((!user.uid || !user.user_type?.includes('admin')) && !hostname?.includes('
|
|
8
|
+
if ((!user.uid || !user.user_type?.includes('admin')) && !hostname?.includes('local')) {
|
|
9
9
|
return { message: 'access restricted', status: 403 };
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -106,28 +106,36 @@ export default async function dataAPI(req) {
|
|
|
106
106
|
where ${where.join(' and ') || 'true'}
|
|
107
107
|
${order} ${offset} limit ${limit}`
|
|
108
108
|
.replace(/{{uid}}/g, uid);
|
|
109
|
-
|
|
109
|
+
|
|
110
110
|
// if (user?.user_type === 'superadmin') console.log(q);
|
|
111
111
|
|
|
112
112
|
if (query.sql === '1') { return q; }
|
|
113
113
|
|
|
114
114
|
const { rows } = await pg.query(q, (hookData?.id || params.id ? [hookData?.id || params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
|
|
115
115
|
|
|
116
|
-
const filterWhere = [fData.q, search, bbox, queryPolyline, interfaceQuery].filter((el) => el);
|
|
116
|
+
const filterWhere = [fData.q, search, bbox, queryPolyline, interfaceQuery, loadTable.query].filter((el) => el);
|
|
117
117
|
|
|
118
|
+
const aggregates = dbColumns.map((el) => ({ name: el.name, type: pg.pgType[el.dataTypeID] })).filter((el) => ['numeric', 'double precision'].includes(el.type));
|
|
118
119
|
const qCount = `select
|
|
119
120
|
count(*)::int as total,
|
|
120
|
-
count(*) FILTER(WHERE ${filterWhere.
|
|
121
|
+
count(*) FILTER(WHERE ${filterWhere.join(' and ') || 'true'})::int as filtered
|
|
122
|
+
${aggregates.length ? `,${aggregates.map((el) => `sum(${el.name}) FILTER(WHERE ${filterWhere.join(' and ') || 'true'}) as ${el.name}`).join(',')}` : ''}
|
|
121
123
|
from ${table} t ${sqlTable}
|
|
122
124
|
where ${[loadTable.query, accessQuery].filter(el => el).join(' and ') || 'true'} `
|
|
123
125
|
.replace(/{{uid}}/g, uid);
|
|
124
126
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
if (query.sql === '2') { return qCount; }
|
|
128
|
+
|
|
129
|
+
const counts = keyQuery || hookData?.id || params.id
|
|
130
|
+
? { total: rows.length, filtered: rows.length }
|
|
131
|
+
: await pg.queryCache(qCount, { table: loadTable.table, time: 5 }).then(el => el?.rows[0] || {});
|
|
132
|
+
|
|
133
|
+
const { total, filtered } = counts;
|
|
134
|
+
const agg = Object.keys(counts).filter(el => !['total', 'filtered'].includes(el)).reduce((acc, el) => ({ ...acc, [el]: counts[el] }), {});
|
|
127
135
|
|
|
128
136
|
await metaFormat({ rows, table: hookData?.table || params.table });
|
|
129
137
|
const res = {
|
|
130
|
-
time: Date.now() - time, public: ispublic, card: loadTable.card, actions, total, filtered, count: rows.length, pk, form, rows, meta, columns, filters,
|
|
138
|
+
time: Date.now() - time, public: ispublic, card: loadTable.card, actions, total, filtered, count: rows.length, pk, form, agg, rows, meta, columns, filters,
|
|
131
139
|
};
|
|
132
140
|
|
|
133
141
|
// console.log({ add: loadTable.table, form: loadTable.form });
|
|
@@ -21,17 +21,21 @@ export default async function suggest(req) {
|
|
|
21
21
|
const [table, column] = params.data?.includes(':') ? params.data.split(':') : [];
|
|
22
22
|
const selectName = query.sel || query.name || params.data;
|
|
23
23
|
|
|
24
|
-
if (!selectName) return {
|
|
24
|
+
if (!selectName) return { status: 400, message: 'name is required' };
|
|
25
25
|
|
|
26
26
|
const body = await getTemplate('table', table);
|
|
27
27
|
|
|
28
28
|
if (table && !pg1.pk[body?.table || table]) {
|
|
29
|
-
return {
|
|
29
|
+
return { status: 400, message: 'param name is invalid: 1' };
|
|
30
30
|
}
|
|
31
31
|
const columnExists = body?.columns?.find((col) => col?.name === column);
|
|
32
32
|
|
|
33
33
|
if (table && (!column || !columnExists)) {
|
|
34
|
-
return {
|
|
34
|
+
return { status: 400, message: 'param name is invalid: 2' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (query.limit && query.limit < 0) {
|
|
38
|
+
return { status: 400, message: 'param limit is invalid' };
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
const meta = table && column
|
|
@@ -59,11 +63,17 @@ export default async function suggest(req) {
|
|
|
59
63
|
|
|
60
64
|
if (arr) {
|
|
61
65
|
const lower = query.key?.toLowerCase();
|
|
62
|
-
const
|
|
66
|
+
const data1 = query.key || query.val
|
|
63
67
|
? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
|
|
64
68
|
: arr;
|
|
69
|
+
const data = data1.slice(0, Math.min(query.limit || limit, limit));
|
|
65
70
|
return {
|
|
66
|
-
|
|
71
|
+
time: Date.now() - time,
|
|
72
|
+
limit: Math.min(query.limit || limit, limit),
|
|
73
|
+
count: data.length,
|
|
74
|
+
total: arr.length,
|
|
75
|
+
mode: 'array',
|
|
76
|
+
data,
|
|
67
77
|
};
|
|
68
78
|
}
|
|
69
79
|
|
|
@@ -85,7 +95,7 @@ export default async function suggest(req) {
|
|
|
85
95
|
? `id in (select ${filterColumn} from ${(loadTable?.table || query.token).replace(/'/g, "''")})`
|
|
86
96
|
: 'true';
|
|
87
97
|
|
|
88
|
-
const sqlSuggest = `with c(id,text) as ( ${meta.original.replace(/{{parent}}/gi, parent)} where ${where} order by 2) select * from c where ${filter} limit ${meta.limit || limit}`.replace(/{{uid}}/g, user?.uid || '0');
|
|
98
|
+
const sqlSuggest = `with c(id,text) as ( ${meta.original.replace(/{{parent}}/gi, parent)} where ${where} order by 2) select * from c where ${filter} limit ${Math.min(query.limit || meta.limit || limit, limit)}`.replace(/{{uid}}/g, user?.uid || '0');
|
|
89
99
|
|
|
90
100
|
if (query.sql && config.local) {
|
|
91
101
|
return sqlSuggest;
|
|
@@ -99,6 +109,7 @@ export default async function suggest(req) {
|
|
|
99
109
|
|
|
100
110
|
const message = {
|
|
101
111
|
time: Date.now() - time,
|
|
112
|
+
limit: Math.min(query.limit || meta.limit || limit, limit),
|
|
102
113
|
count: data.length,
|
|
103
114
|
total: meta.count - 0,
|
|
104
115
|
mode: 'sql',
|