@opengis/fastify-table 1.3.45 → 1.3.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
|
@@ -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') {
|
|
@@ -8,7 +8,7 @@ import getSelect from './getSelect.js';
|
|
|
8
8
|
const limit = 50;
|
|
9
9
|
const selectMeta = {};
|
|
10
10
|
|
|
11
|
-
export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
|
|
11
|
+
export default async function getSelectMeta({ name, pg = pgClients.client, nocache, parent }) {
|
|
12
12
|
if (selectMeta[name] && !nocache) return selectMeta[name];
|
|
13
13
|
|
|
14
14
|
const cls = await getSelect(name, pg);
|
|
@@ -23,7 +23,7 @@ export default async function getSelectMeta({ name, pg = pgClients.client, nocac
|
|
|
23
23
|
const { sql: original } = cls;
|
|
24
24
|
if (!original.toLowerCase) { console.log(`sql select null: ${name}`); return null; }
|
|
25
25
|
|
|
26
|
-
const sql = `with c(id,text) as (select * from (${original})q limit ${limit}) select * from c`;
|
|
26
|
+
const sql = `with c(id,text) as (select * from (${original.replace('{{parent}}', parent)})q limit ${limit}) select * from c`;
|
|
27
27
|
|
|
28
28
|
/*= == meta table === */
|
|
29
29
|
|
|
@@ -32,10 +32,10 @@ export default async function getSelectMeta({ name, pg = pgClients.client, nocac
|
|
|
32
32
|
|
|
33
33
|
const dataOrigin = await pg1.query(sql?.replace(`limit ${limit}`, 'limit 0'));
|
|
34
34
|
|
|
35
|
-
const dataOrigin1 = await pg1.query(`${original} limit 0`);
|
|
35
|
+
const dataOrigin1 = await pg1.query(`${original.replace('{{parent}}', parent)} limit 0`);
|
|
36
36
|
|
|
37
37
|
// const table = getTable(original);
|
|
38
|
-
const count = cls?.count ?? await pg1.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
38
|
+
const count = cls?.count ?? await pg1.query(`select count(*) from (${original.replace('{{parent}}', parent)})q`).then((el) => el?.rows?.[0].count);
|
|
39
39
|
|
|
40
40
|
// column name
|
|
41
41
|
const cols = dataOrigin.fields.map((el) => el.name);
|
|
@@ -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
|
}
|
|
@@ -56,7 +56,7 @@ export default async function suggest(req) {
|
|
|
56
56
|
|
|
57
57
|
const meta = table && column
|
|
58
58
|
? getTableColumnMeta(table, column, query?.key || query?.val)
|
|
59
|
-
: await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache });
|
|
59
|
+
: await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache, parent });
|
|
60
60
|
|
|
61
61
|
if (meta?.minLength && query.key && query.key.length < meta?.minLength) {
|
|
62
62
|
return { message: `min length: ${meta.minLength}` };
|