@opengis/fastify-table 1.3.46 → 1.3.48
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
|
@@ -45,7 +45,8 @@ export default async function getAccess({ table, user = {} }, pg = pgClients.cli
|
|
|
45
45
|
const tableActions = ['view'].concat(body?.actions || body?.action_default || []);
|
|
46
46
|
|
|
47
47
|
if (userType === 'admin') {
|
|
48
|
-
|
|
48
|
+
const adminActions = body?.form ? ['view', 'del', 'edit', 'add'] : ['view', 'del'];
|
|
49
|
+
return { actions: body?.actions || body?.action_default ? tableActions : adminActions, query: '1=1' };
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
if (body?.public || body?.access === 'public') {
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
dataDelete, getTemplate, getAccess, applyHook, getToken, config, pgClients,
|
|
3
3
|
} from '../../../../utils.js';
|
|
4
4
|
|
|
5
|
-
export default async function deleteCrud(req) {
|
|
5
|
+
export default async function deleteCrud(req, reply) {
|
|
6
6
|
const { pg = pgClients.client, user, params = {}, headers = {} } = req || {};
|
|
7
7
|
|
|
8
8
|
const hookData = await applyHook('preDelete', {
|
|
@@ -10,7 +10,7 @@ export default async function deleteCrud(req) {
|
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
if (hookData?.message && hookData?.status) {
|
|
13
|
-
return
|
|
13
|
+
return reply.status(hookData.status).send(hookData.message);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
const { referer } = headers;
|
|
@@ -22,18 +22,25 @@ export default async function deleteCrud(req) {
|
|
|
22
22
|
const { actions = [] } = await getAccess({ table: del, id, user }, pg) || {};
|
|
23
23
|
|
|
24
24
|
if (!actions.includes('del') && !config?.local && !tokenData) {
|
|
25
|
-
return
|
|
25
|
+
return reply.status(403).send('access restricted');
|
|
26
26
|
}
|
|
27
27
|
const loadTemplate = await getTemplate('table', del);
|
|
28
28
|
|
|
29
29
|
const { table } = loadTemplate || hookData || tokenData || req.params || {};
|
|
30
30
|
|
|
31
|
-
if (!table)
|
|
32
|
-
if (!id)
|
|
31
|
+
if (!table) reply.status(404).send('table is required');
|
|
32
|
+
if (!id) reply.status(404).send('id is required');
|
|
33
33
|
|
|
34
34
|
const data = await dataDelete({
|
|
35
35
|
pg, table, id, uid: user?.uid, tokenData, referer,
|
|
36
|
+
}).catch(err => {
|
|
37
|
+
if (err.message?.includes?.('foreign key' || 'unique')) {
|
|
38
|
+
const constraint = err.message.match(/constraint "([^"]+)"/g);
|
|
39
|
+
return reply.status(400).send('Видалення заборонено для збереження цілісності БД: ' + constraint);
|
|
40
|
+
}
|
|
41
|
+
if (config.trace) console.error(err.toString());
|
|
42
|
+
return err.toString();
|
|
36
43
|
});
|
|
37
44
|
|
|
38
|
-
return { rowCount: data
|
|
45
|
+
return reply.status(200).send({ rowCount: data?.rowCount || 0, msg: !data?.rowCount ? data : null });
|
|
39
46
|
}
|
|
@@ -77,18 +77,18 @@ export default async function suggest(req) {
|
|
|
77
77
|
if (arr && query.token && query.column) {
|
|
78
78
|
const loadTable = await getTemplate('table', query.token);
|
|
79
79
|
|
|
80
|
-
const { columns = [] } = await getMeta({ pg, table: tableName });
|
|
80
|
+
const { columns = [] } = await getMeta({ pg, table: loadTable?.table || tableName });
|
|
81
81
|
|
|
82
82
|
const column = columns.find(el => el.name === query.column);
|
|
83
83
|
const args = { table: tableName };
|
|
84
84
|
if (!column) return [];
|
|
85
|
-
const sqlCls = `select array_agg(distinct value) from (select ${pg.pgType?.[column.dataTypeID]?.includes('[]') ? `unnest(${column.name})` : `${column.name}`} as value from ${(tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || '1=1'})q`;
|
|
85
|
+
const sqlCls = `select array_agg(distinct value)::text[] from (select ${pg.pgType?.[column.dataTypeID]?.includes('[]') ? `unnest(${column.name})` : `${column.name}`} as value from ${(loadTable?.table || tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || '1=1'})q`;
|
|
86
86
|
|
|
87
87
|
if (query.sql && (config.local || user?.user_type?.includes?.('admin'))) {
|
|
88
88
|
return sqlCls;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
if (pg.pk?.[tableName] && column?.name) {
|
|
91
|
+
if (pg.pk?.[loadTable?.table || tableName] && column?.name) {
|
|
92
92
|
const vals = await pg.queryCache(sqlCls, args).then(el => el.rows?.[0]?.array_agg || []);
|
|
93
93
|
// console.log(vals);
|
|
94
94
|
|
|
@@ -97,7 +97,7 @@ export default async function suggest(req) {
|
|
|
97
97
|
? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
|
|
98
98
|
: arr;
|
|
99
99
|
|
|
100
|
-
const data2 = data1.filter((el) => vals.includes(el.id));
|
|
100
|
+
const data2 = data1.filter((el) => el.id && vals.includes(el.id.toString()));
|
|
101
101
|
const data = data2.slice(0, Math.min(query.limit || limit, limit));
|
|
102
102
|
return {
|
|
103
103
|
time: Date.now() - time,
|