@opengis/fastify-table 1.1.39 → 1.1.41
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/Changelog.md +1 -1
- package/package.json +1 -1
- package/table/controllers/filter.js +12 -2
- package/table/controllers/table.js +7 -6
- package/test/api/user.test.js +3 -2
- package/user/controllers/user.cls.js +7 -3
- package/utils.js +50 -19
package/Changelog.md
CHANGED
package/package.json
CHANGED
|
@@ -30,11 +30,21 @@ export default async function filter(req) {
|
|
|
30
30
|
});
|
|
31
31
|
Object.assign(el, { options });
|
|
32
32
|
}));
|
|
33
|
+
|
|
34
|
+
const q = ((loadTable?.filterState || []).concat(loadTable?.filterCustom || [])).filter((el) => el.name && el.sql).map((el) => `select count(*), '${el.name}' as name from (${optimizedSQL})q where ${el.sql}`).join(' union all ');
|
|
35
|
+
const { rows = [] } = q ? await req.pg.query(q) : {};
|
|
36
|
+
if (rows?.length) {
|
|
37
|
+
((loadTable?.filterState || []).concat(loadTable?.filterCustom || [])).filter((el) => el.name && el.sql).forEach((el) => {
|
|
38
|
+
const { count } = rows.find((row) => row.name === el.name) || {};
|
|
39
|
+
Object.assign(el, { count, sql: undefined });
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
33
43
|
return {
|
|
34
44
|
time: Date.now() - time,
|
|
35
45
|
list: filters,
|
|
36
|
-
custom: loadTable?.filterCustom
|
|
46
|
+
custom: loadTable?.filterCustom,
|
|
37
47
|
inline: loadTable?.filterInline,
|
|
38
|
-
state: loadTable?.filterState
|
|
48
|
+
state: loadTable?.filterState,
|
|
39
49
|
};
|
|
40
50
|
}
|
|
@@ -5,19 +5,20 @@ import { applyHook } from '../../utils.js';
|
|
|
5
5
|
|
|
6
6
|
export default async function tableAPI(req) {
|
|
7
7
|
const {
|
|
8
|
-
pg, params = {}, query = {},
|
|
8
|
+
pg, params = {}, query = {},
|
|
9
9
|
} = req;
|
|
10
10
|
if (!params.id) return { message: 'not enough params', status: 400 };
|
|
11
11
|
|
|
12
|
-
const loadTable = await getTemplate('table', opt?.table || params.table) || {};
|
|
13
|
-
if (!loadTable) {
|
|
14
|
-
if (!pg.pk?.[opt?.table || params.table]) { return { message: 'not found', status: 404 }; }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
12
|
const check = await applyHook('preTable', { req });
|
|
18
13
|
if (check?.message && check?.status) {
|
|
19
14
|
return { message: check?.message, status: check?.status };
|
|
20
15
|
}
|
|
16
|
+
const { opt = {} } = req;
|
|
17
|
+
|
|
18
|
+
const loadTable = await getTemplate('table', opt?.table || params.table) || {};
|
|
19
|
+
if (!loadTable) {
|
|
20
|
+
if (!pg.pk?.[opt?.table || params.table]) { return { message: 'not found', status: 404 }; }
|
|
21
|
+
}
|
|
21
22
|
|
|
22
23
|
const {
|
|
23
24
|
table, /* columns, */ form,
|
package/test/api/user.test.js
CHANGED
|
@@ -64,9 +64,10 @@ test('applyHook to API data/table', async (t) => {
|
|
|
64
64
|
url: `${prefix}/user-cls/${body.name}`,
|
|
65
65
|
body,
|
|
66
66
|
});
|
|
67
|
+
const resp = res.json();
|
|
67
68
|
assert.equal(res.statusCode, 200);
|
|
68
|
-
assert.equal(
|
|
69
|
-
assert.equal(
|
|
69
|
+
assert.equal(resp.status || 200, 200);
|
|
70
|
+
assert.equal(resp.message?.children?.length, 1);
|
|
70
71
|
});
|
|
71
72
|
|
|
72
73
|
await t.test('clean up', async () => {
|
|
@@ -11,6 +11,10 @@ export default async function userCls(req) {
|
|
|
11
11
|
return { message: 'access restricted', status: 403 };
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
if (query.type && !['json', 'sql'].includes(query.type)) {
|
|
15
|
+
return { message: 'param type is invalid', status: 400 };
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
const q = `select user_clsid as id, name, type,
|
|
15
19
|
case when type='json' then (
|
|
16
20
|
${params?.id
|
|
@@ -28,7 +32,7 @@ export default async function userCls(req) {
|
|
|
28
32
|
case when type='sql' then data else null end as sql from admin.user_cls u
|
|
29
33
|
where (case when type='json' then parent is null else true end)
|
|
30
34
|
and uid=(select uid from admin.users where $1 in (login,uid) limit 1)
|
|
31
|
-
and parent is null and ${params?.id ? 'u.name=$2' : '1=1'}`;
|
|
35
|
+
and parent is null and ${query.type ? `type='${query.type}'` : '1=1'} and ${params?.id ? 'u.name=$2' : '1=1'}`;
|
|
32
36
|
|
|
33
37
|
if (query?.sql) return q;
|
|
34
38
|
|
|
@@ -40,8 +44,8 @@ export default async function userCls(req) {
|
|
|
40
44
|
if (row.type === 'json') { delete row.sql; Object.assign(row, { children: row.children || (params?.id ? [] : 0) }); }
|
|
41
45
|
});
|
|
42
46
|
|
|
43
|
-
const clsList = getTemplatePath('cls');
|
|
44
|
-
const selectList = getTemplatePath('select');
|
|
47
|
+
const clsList = query.type === 'sql' ? [] : getTemplatePath('cls'); // skip cls if type sql
|
|
48
|
+
const selectList = query.type === 'json' ? [] : getTemplatePath('select'); // skip sql if type json
|
|
45
49
|
|
|
46
50
|
const userClsNames = rows.map((el) => el.name);
|
|
47
51
|
const selectNames = selectList.map((el) => el[0]);
|
package/utils.js
CHANGED
|
@@ -21,7 +21,7 @@ import dataDelete from './crud/funcs/dataDelete.js';
|
|
|
21
21
|
import getToken from './crud/funcs/getToken.js';
|
|
22
22
|
import setToken from './crud/funcs/setToken.js';
|
|
23
23
|
import checkXSS from './crud/controllers/utils/checkXSS.js';
|
|
24
|
-
import
|
|
24
|
+
import getTableColumn from './table/controllers/utils/gisIRColumn.js';
|
|
25
25
|
import getMeta from './pg/funcs/getMeta.js';
|
|
26
26
|
import getAccess from './crud/funcs/getAccess.js';
|
|
27
27
|
import getSelectVal from './table/funcs/metaFormat/getSelectVal.js';
|
|
@@ -32,38 +32,69 @@ import addHook from './hook/funcs/addHook.js';
|
|
|
32
32
|
import execMigrations from './migration/exec.migrations.js';
|
|
33
33
|
import addNotification from './notification/funcs/addNotification.js';
|
|
34
34
|
import sendNotification from './notification/funcs/sendNotification.js';
|
|
35
|
+
import getFilterSQL from './table/funcs/getFilterSQL/index.js';
|
|
36
|
+
|
|
37
|
+
import addCron from './cron/funcs/addCron.js';
|
|
38
|
+
import getOpt from './crud/funcs/getOpt.js';
|
|
39
|
+
import setOpt from './crud/funcs/setOpt.js';
|
|
40
|
+
|
|
41
|
+
import isFileExists from './crud/funcs/isFileExists.js';
|
|
42
|
+
|
|
35
43
|
import logger from './logger/getLogger.js';
|
|
36
44
|
// const logger = false; // test!
|
|
37
45
|
|
|
38
46
|
export default null;
|
|
39
47
|
export {
|
|
48
|
+
|
|
49
|
+
addCron,
|
|
50
|
+
execMigrations,
|
|
51
|
+
getRedis,
|
|
40
52
|
logger,
|
|
53
|
+
isFileExists,
|
|
54
|
+
|
|
55
|
+
// hook
|
|
56
|
+
addHook,
|
|
57
|
+
applyHook,
|
|
58
|
+
|
|
59
|
+
// template
|
|
41
60
|
getTemplate,
|
|
42
61
|
getTemplatePath,
|
|
43
|
-
userTemplateDir,
|
|
44
|
-
metaFormat,
|
|
45
|
-
autoIndex,
|
|
46
62
|
addTemplateDir,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
63
|
+
userTemplateDir,
|
|
64
|
+
|
|
65
|
+
// notification
|
|
66
|
+
addNotification,
|
|
67
|
+
sendNotification,
|
|
68
|
+
|
|
69
|
+
// security
|
|
70
|
+
checkXSS,
|
|
71
|
+
getAccess,
|
|
72
|
+
getToken,
|
|
73
|
+
getOpt,
|
|
74
|
+
setOpt,
|
|
75
|
+
setToken,
|
|
76
|
+
|
|
77
|
+
// crud
|
|
51
78
|
dataInsert,
|
|
52
79
|
dataUpdate,
|
|
53
80
|
dataDelete,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
81
|
+
|
|
82
|
+
// table
|
|
83
|
+
autoIndex,
|
|
84
|
+
metaFormat,
|
|
85
|
+
getFilterSQL,
|
|
59
86
|
getMeta,
|
|
60
|
-
|
|
87
|
+
getTableColumn,
|
|
88
|
+
|
|
89
|
+
// pg
|
|
90
|
+
initPG,
|
|
91
|
+
getPG,
|
|
92
|
+
getPGAsync,
|
|
93
|
+
pgClients,
|
|
94
|
+
|
|
95
|
+
// select
|
|
61
96
|
getSelectVal,
|
|
62
97
|
getSelectMeta,
|
|
63
98
|
getSelect,
|
|
64
|
-
|
|
65
|
-
addHook,
|
|
66
|
-
execMigrations,
|
|
67
|
-
addNotification,
|
|
68
|
-
sendNotification,
|
|
99
|
+
|
|
69
100
|
};
|