@opengis/fastify-table 1.2.67 → 1.2.69
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/README.md +86 -86
- package/package.json +1 -1
- package/server/migrations/cls.sql +39 -39
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/getOpt.js +13 -13
- package/server/plugins/crud/funcs/setOpt.js +21 -21
- package/server/plugins/crud/funcs/setToken.js +44 -44
- package/server/plugins/crud/funcs/utils/getFolder.js +11 -11
- package/server/plugins/crud/funcs/validateData.js +3 -3
- package/server/plugins/crud/index.js +23 -23
- package/server/plugins/hook/index.js +8 -8
- package/server/plugins/logger/errorStatus.js +19 -19
- package/server/plugins/logger/index.js +26 -26
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/funcs/autoIndex.js +2 -2
- package/server/plugins/policy/sqlInjection.js +33 -33
- package/server/plugins/redis/client.js +8 -8
- package/server/plugins/redis/funcs/redisClients.js +3 -3
- package/server/plugins/redis/index.js +17 -17
- package/server/plugins/table/funcs/getData.js +3 -3
- package/server/plugins/table/funcs/getFilterSQL/index.js +68 -50
- package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +143 -119
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +9 -6
- package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +5 -11
- package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/plugins/table/funcs/getTemplates.js +19 -19
- package/server/plugins/table/funcs/gisIRColumn.js +82 -82
- package/server/plugins/table/funcs/loadTemplate.js +1 -1
- package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
- package/server/plugins/table/funcs/userTemplateDir.js +1 -1
- package/server/plugins/table/index.js +13 -13
- package/server/plugins/util/index.js +7 -7
- package/server/routes/cron/index.js +14 -14
- package/server/routes/crud/controllers/table.js +92 -88
- package/server/routes/logger/controllers/logger.file.js +92 -92
- package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
- package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
- package/server/routes/logger/index.js +17 -17
- package/server/routes/properties/controllers/properties.add.js +55 -55
- package/server/routes/properties/controllers/properties.get.js +17 -17
- package/server/routes/properties/index.js +16 -16
- package/server/routes/table/controllers/data.js +12 -7
- package/server/routes/table/controllers/filter.js +21 -4
- package/server/routes/table/controllers/form.js +42 -42
- package/server/routes/table/controllers/search.js +74 -74
- package/server/routes/table/controllers/suggest.js +2 -2
- package/server/routes/table/index.js +29 -29
- package/server/routes/table/schema.js +64 -64
- package/server/routes/util/controllers/status.monitor.js +8 -8
- package/server/routes/util/index.js +11 -11
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'crypto';
|
|
2
|
-
|
|
3
|
-
import config from '../../../../config.js';
|
|
4
|
-
|
|
5
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
6
|
-
|
|
7
|
-
const rclient = getRedis({ db: 0 });
|
|
8
|
-
|
|
9
|
-
// import { config, getRedis } from '../../../../utils.js';
|
|
10
|
-
|
|
11
|
-
const generateCodes = (ids, userToken) => {
|
|
12
|
-
const token = userToken || randomUUID();
|
|
13
|
-
const notNullIds = ids.filter((el) => el);
|
|
14
|
-
const obj = {};
|
|
15
|
-
const codes = notNullIds.reduce((acc, id) => {
|
|
16
|
-
const newToken = createHash('sha1').update(token + id).digest('base64url').replace(/-/g, '');
|
|
17
|
-
acc[newToken] = id; obj[id] = newToken;
|
|
18
|
-
return acc;
|
|
19
|
-
}, {});
|
|
20
|
-
return { codes, obj };
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function setToken({
|
|
24
|
-
ids: idsOrigin, uid, array,
|
|
25
|
-
}) {
|
|
26
|
-
// const rclient5 = getRedis({ db: 0, funcs });
|
|
27
|
-
|
|
28
|
-
if (!uid) return { user: 'empty' };
|
|
29
|
-
if (!Object.keys(idsOrigin).length) return { ids: 'empty' };
|
|
30
|
-
|
|
31
|
-
const ids = idsOrigin.map((el) => (typeof el === 'object' ? JSON.stringify(el) : el));
|
|
32
|
-
|
|
33
|
-
// TODO generate salt
|
|
34
|
-
const { codes, obj } = generateCodes(ids, uid);
|
|
35
|
-
|
|
36
|
-
if (!Object.keys(codes).length) return { ids: 'empty' };
|
|
37
|
-
|
|
38
|
-
rclient.hmset(`${config.pg.database}:token:edit:${uid}`, codes);
|
|
39
|
-
// console.log(`${config.pg.database}:token:edit:${uid}`, idsOrigin, Object.values(obj));
|
|
40
|
-
// TODO дополнительно писать в hset token -> uid
|
|
41
|
-
return array ? Object.values(obj) : obj;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export default setToken;
|
|
1
|
+
import { createHash, randomUUID } from 'crypto';
|
|
2
|
+
|
|
3
|
+
import config from '../../../../config.js';
|
|
4
|
+
|
|
5
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
6
|
+
|
|
7
|
+
const rclient = getRedis({ db: 0 });
|
|
8
|
+
|
|
9
|
+
// import { config, getRedis } from '../../../../utils.js';
|
|
10
|
+
|
|
11
|
+
const generateCodes = (ids, userToken) => {
|
|
12
|
+
const token = userToken || randomUUID();
|
|
13
|
+
const notNullIds = ids.filter((el) => el);
|
|
14
|
+
const obj = {};
|
|
15
|
+
const codes = notNullIds.reduce((acc, id) => {
|
|
16
|
+
const newToken = createHash('sha1').update(token + id).digest('base64url').replace(/-/g, '');
|
|
17
|
+
acc[newToken] = id; obj[id] = newToken;
|
|
18
|
+
return acc;
|
|
19
|
+
}, {});
|
|
20
|
+
return { codes, obj };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function setToken({
|
|
24
|
+
ids: idsOrigin, uid, array,
|
|
25
|
+
}) {
|
|
26
|
+
// const rclient5 = getRedis({ db: 0, funcs });
|
|
27
|
+
|
|
28
|
+
if (!uid) return { user: 'empty' };
|
|
29
|
+
if (!Object.keys(idsOrigin).length) return { ids: 'empty' };
|
|
30
|
+
|
|
31
|
+
const ids = idsOrigin.map((el) => (typeof el === 'object' ? JSON.stringify(el) : el));
|
|
32
|
+
|
|
33
|
+
// TODO generate salt
|
|
34
|
+
const { codes, obj } = generateCodes(ids, uid);
|
|
35
|
+
|
|
36
|
+
if (!Object.keys(codes).length) return { ids: 'empty' };
|
|
37
|
+
|
|
38
|
+
rclient.hmset(`${config.pg.database}:token:edit:${uid}`, codes);
|
|
39
|
+
// console.log(`${config.pg.database}:token:edit:${uid}`, idsOrigin, Object.values(obj));
|
|
40
|
+
// TODO дополнительно писать в hset token -> uid
|
|
41
|
+
return array ? Object.values(obj) : obj;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default setToken;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
|
|
3
|
-
import config from '../../../../../config.js';
|
|
4
|
-
|
|
5
|
-
export default function getFolder(req, type = 'server') {
|
|
6
|
-
if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
|
|
7
|
-
const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
|
|
8
|
-
const dbname = req.pg?.options?.database || req.pg?.database || config.pg?.database; // request / config params / default config params
|
|
9
|
-
const filepath = path.posix.join(types[type] || `/data/local/${dbname || ''}`, req.folder || config.folder || '');
|
|
10
|
-
return filepath;
|
|
11
|
-
}
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import config from '../../../../../config.js';
|
|
4
|
+
|
|
5
|
+
export default function getFolder(req, type = 'server') {
|
|
6
|
+
if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
|
|
7
|
+
const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
|
|
8
|
+
const dbname = req.pg?.options?.database || req.pg?.database || config.pg?.database; // request / config params / default config params
|
|
9
|
+
const filepath = path.posix.join(types[type] || `/data/local/${dbname || ''}`, req.folder || config.folder || '');
|
|
10
|
+
return filepath;
|
|
11
|
+
}
|
|
@@ -34,7 +34,7 @@ function checkBody({ body = {}, arr = [], idx }) {
|
|
|
34
34
|
const input = arr.find(el => el.key === key) || {};
|
|
35
35
|
|
|
36
36
|
if (input.colModel?.length && input.type?.toLowerCase() === 'datatable') {
|
|
37
|
-
const result = body[key]
|
|
37
|
+
const result = body[key]?.reduce?.((acc, item, i) => {
|
|
38
38
|
const check = checkBody({ body: item, arr: input.colModel, idx: i });
|
|
39
39
|
acc.push(check);
|
|
40
40
|
return acc;
|
|
@@ -48,10 +48,10 @@ function checkBody({ body = {}, arr = [], idx }) {
|
|
|
48
48
|
return acc1;
|
|
49
49
|
}, []);
|
|
50
50
|
|
|
51
|
-
const invalidField = res.find(el => el
|
|
51
|
+
const invalidField = res.find(el => el?.error);
|
|
52
52
|
|
|
53
53
|
if (invalidField) {
|
|
54
|
-
console.warn('invalid field: ', invalidField
|
|
54
|
+
console.warn('invalid field: ', invalidField?.key, invalidField?.error);
|
|
55
55
|
return invalidField;
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
// import getOpt from './funcs/getOpt.js';
|
|
2
|
-
// import setOpt from './funcs/setOpt.js';
|
|
3
|
-
|
|
4
|
-
// import isFileExists from './funcs/isFileExists.js';
|
|
5
|
-
|
|
6
|
-
// import dataUpdate from './funcs/dataUpdate.js';
|
|
7
|
-
// import dataInsert from './funcs/dataInsert.js';
|
|
8
|
-
|
|
9
|
-
// import getAccessFunc from './funcs/getAccess.js';
|
|
10
|
-
|
|
11
|
-
async function plugin(fastify) {
|
|
12
|
-
// fastify.decorate('setOpt', setOpt);
|
|
13
|
-
// fastify.decorate('getOpt', getOpt);
|
|
14
|
-
|
|
15
|
-
// fastify.decorate('dataUpdate', dataUpdate);
|
|
16
|
-
// fastify.decorate('dataInsert', dataInsert);
|
|
17
|
-
|
|
18
|
-
// fastify.decorate('getAccess', getAccessFunc);
|
|
19
|
-
|
|
20
|
-
// fastify.decorate('isFileExists', isFileExists);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default plugin;
|
|
1
|
+
// import getOpt from './funcs/getOpt.js';
|
|
2
|
+
// import setOpt from './funcs/setOpt.js';
|
|
3
|
+
|
|
4
|
+
// import isFileExists from './funcs/isFileExists.js';
|
|
5
|
+
|
|
6
|
+
// import dataUpdate from './funcs/dataUpdate.js';
|
|
7
|
+
// import dataInsert from './funcs/dataInsert.js';
|
|
8
|
+
|
|
9
|
+
// import getAccessFunc from './funcs/getAccess.js';
|
|
10
|
+
|
|
11
|
+
async function plugin(fastify) {
|
|
12
|
+
// fastify.decorate('setOpt', setOpt);
|
|
13
|
+
// fastify.decorate('getOpt', getOpt);
|
|
14
|
+
|
|
15
|
+
// fastify.decorate('dataUpdate', dataUpdate);
|
|
16
|
+
// fastify.decorate('dataInsert', dataInsert);
|
|
17
|
+
|
|
18
|
+
// fastify.decorate('getAccess', getAccessFunc);
|
|
19
|
+
|
|
20
|
+
// fastify.decorate('isFileExists', isFileExists);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default plugin;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import addHook from './funcs/addHook.js';
|
|
2
|
-
import applyHook from './funcs/applyHook.js';
|
|
3
|
-
|
|
4
|
-
async function plugin(fastify) {
|
|
5
|
-
// fastify.decorate('addHook', addHook);
|
|
6
|
-
// fastify.decorate('applyHook', applyHook);
|
|
7
|
-
}
|
|
8
|
-
export default plugin;
|
|
1
|
+
import addHook from './funcs/addHook.js';
|
|
2
|
+
import applyHook from './funcs/applyHook.js';
|
|
3
|
+
|
|
4
|
+
async function plugin(fastify) {
|
|
5
|
+
// fastify.decorate('addHook', addHook);
|
|
6
|
+
// fastify.decorate('applyHook', applyHook);
|
|
7
|
+
}
|
|
8
|
+
export default plugin;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import applyHookSync from '../hook/funcs/applyHookSync.js';
|
|
2
|
-
|
|
3
|
-
function errorStatus(error) {
|
|
4
|
-
const hook = applyHookSync('errorStatus', error);
|
|
5
|
-
if (hook) return hook;
|
|
6
|
-
|
|
7
|
-
if (error.routine === 'exec_stmt_raise' && error.file === 'pl_exec.c') {
|
|
8
|
-
return 601;
|
|
9
|
-
}
|
|
10
|
-
if (error.routine === 'ExecConstraints') {
|
|
11
|
-
return 602;
|
|
12
|
-
}
|
|
13
|
-
if (error.type === 'DatabaseError') {
|
|
14
|
-
return 600;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return 500;
|
|
18
|
-
}
|
|
19
|
-
export default errorStatus;
|
|
1
|
+
import applyHookSync from '../hook/funcs/applyHookSync.js';
|
|
2
|
+
|
|
3
|
+
function errorStatus(error) {
|
|
4
|
+
const hook = applyHookSync('errorStatus', error);
|
|
5
|
+
if (hook) return hook;
|
|
6
|
+
|
|
7
|
+
if (error.routine === 'exec_stmt_raise' && error.file === 'pl_exec.c') {
|
|
8
|
+
return 601;
|
|
9
|
+
}
|
|
10
|
+
if (error.routine === 'ExecConstraints') {
|
|
11
|
+
return 602;
|
|
12
|
+
}
|
|
13
|
+
if (error.type === 'DatabaseError') {
|
|
14
|
+
return 600;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return 500;
|
|
18
|
+
}
|
|
19
|
+
export default errorStatus;
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import errorMessage from './errorMessage.js';
|
|
2
|
-
import logger from './getLogger.js';
|
|
3
|
-
import pgClients from '../pg/pgClients.js';
|
|
4
|
-
|
|
5
|
-
async function plugin(fastify) {
|
|
6
|
-
fastify.setErrorHandler(async (error, request, reply) => {
|
|
7
|
-
// validation not error
|
|
8
|
-
if (error.validation) {
|
|
9
|
-
request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
|
|
10
|
-
return reply.status(422).send(error.toString());
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// logger
|
|
14
|
-
request.log.error(error, request);
|
|
15
|
-
console.error({ msg: error.message, where: error.where, stack: error.stack });
|
|
16
|
-
|
|
17
|
-
// errorMessage
|
|
18
|
-
const msg = errorMessage(error);
|
|
19
|
-
|
|
20
|
-
return reply.status(error.statusCode || 500).send(msg);
|
|
21
|
-
});
|
|
22
|
-
fastify.addHook('onListen', async () => {
|
|
23
|
-
logger.file('init', { db: pgClients.client?.options?.database });
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
export default plugin;
|
|
1
|
+
import errorMessage from './errorMessage.js';
|
|
2
|
+
import logger from './getLogger.js';
|
|
3
|
+
import pgClients from '../pg/pgClients.js';
|
|
4
|
+
|
|
5
|
+
async function plugin(fastify) {
|
|
6
|
+
fastify.setErrorHandler(async (error, request, reply) => {
|
|
7
|
+
// validation not error
|
|
8
|
+
if (error.validation) {
|
|
9
|
+
request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
|
|
10
|
+
return reply.status(422).send(error.toString());
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// logger
|
|
14
|
+
request.log.error(error, request);
|
|
15
|
+
console.error({ msg: error.message, where: error.where, stack: error.stack });
|
|
16
|
+
|
|
17
|
+
// errorMessage
|
|
18
|
+
const msg = errorMessage(error);
|
|
19
|
+
|
|
20
|
+
return reply.status(error.statusCode || 500).send(msg);
|
|
21
|
+
});
|
|
22
|
+
fastify.addHook('onListen', async () => {
|
|
23
|
+
logger.file('init', { db: pgClients.client?.options?.database });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export default plugin;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import execMigrations from './funcs/exec.migrations.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
// fastify.decorate('execMigrations', execMigrations);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export default plugin;
|
|
1
|
+
import execMigrations from './funcs/exec.migrations.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
// fastify.decorate('execMigrations', execMigrations);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default plugin;
|
|
@@ -16,8 +16,8 @@ async function autoIndex({
|
|
|
16
16
|
|
|
17
17
|
if (!filter?.length || !table) return null;
|
|
18
18
|
|
|
19
|
-
const attrs = filter.map((el) => (el.name || el).replace(/'/g, '')).sort();
|
|
20
|
-
const types = filter.map((el) => (el.name ? el : { name: el }))
|
|
19
|
+
const attrs = filter.map((el) => (el.name || el).replace(/'/g, '')).filter(el => el?.indexOf('(') === -1).sort();
|
|
20
|
+
const types = filter.filter(el => (el?.name || el)?.indexOf?.('(') === -1).map((el) => (el.name ? el : { name: el }))
|
|
21
21
|
.reduce((p, el) => ({ ...p, [el.name]: el.type || '-' }), {});
|
|
22
22
|
|
|
23
23
|
const redisKey = `autoindex1:${table}:${attrs.join(';')}`;
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
const sqlInjection = [
|
|
2
|
-
'()',
|
|
3
|
-
'^',
|
|
4
|
-
'*',
|
|
5
|
-
'like ',
|
|
6
|
-
'@variable',
|
|
7
|
-
'@@variable',
|
|
8
|
-
'group by ',
|
|
9
|
-
'union ',
|
|
10
|
-
'select ',
|
|
11
|
-
'having ',
|
|
12
|
-
'as injectx',
|
|
13
|
-
'where ',
|
|
14
|
-
'rlike ',
|
|
15
|
-
'if(',
|
|
16
|
-
'sleep(',
|
|
17
|
-
'waitfor delay',
|
|
18
|
-
'benchmark(',
|
|
19
|
-
'pg_sleep(',
|
|
20
|
-
"'\\\"",
|
|
21
|
-
'randomblob(',
|
|
22
|
-
'order by ',
|
|
23
|
-
'union all ',
|
|
24
|
-
'+or',
|
|
25
|
-
'or ',
|
|
26
|
-
'and ',
|
|
27
|
-
"'' ",
|
|
28
|
-
'""" ',
|
|
29
|
-
'<script',
|
|
30
|
-
'javascript:',
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
export default sqlInjection;
|
|
1
|
+
const sqlInjection = [
|
|
2
|
+
'()',
|
|
3
|
+
'^',
|
|
4
|
+
'*',
|
|
5
|
+
'like ',
|
|
6
|
+
'@variable',
|
|
7
|
+
'@@variable',
|
|
8
|
+
'group by ',
|
|
9
|
+
'union ',
|
|
10
|
+
'select ',
|
|
11
|
+
'having ',
|
|
12
|
+
'as injectx',
|
|
13
|
+
'where ',
|
|
14
|
+
'rlike ',
|
|
15
|
+
'if(',
|
|
16
|
+
'sleep(',
|
|
17
|
+
'waitfor delay',
|
|
18
|
+
'benchmark(',
|
|
19
|
+
'pg_sleep(',
|
|
20
|
+
"'\\\"",
|
|
21
|
+
'randomblob(',
|
|
22
|
+
'order by ',
|
|
23
|
+
'union all ',
|
|
24
|
+
'+or',
|
|
25
|
+
'or ',
|
|
26
|
+
'and ',
|
|
27
|
+
"'' ",
|
|
28
|
+
'""" ',
|
|
29
|
+
'<script',
|
|
30
|
+
'javascript:',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
export default sqlInjection;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import redisClients from './funcs/redisClients.js';
|
|
2
|
-
import getRedis from './funcs/getRedis.js';
|
|
3
|
-
|
|
4
|
-
if (!redisClients[0]) {
|
|
5
|
-
getRedis({ db: 0 });
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default redisClients[0];
|
|
1
|
+
import redisClients from './funcs/redisClients.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
|
|
4
|
+
if (!redisClients[0]) {
|
|
5
|
+
getRedis({ db: 0 });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default redisClients[0];
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const redisClients = {};
|
|
2
|
-
|
|
3
|
-
export default redisClients;
|
|
1
|
+
const redisClients = {};
|
|
2
|
+
|
|
3
|
+
export default redisClients;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
// import redis from './client.js';
|
|
2
|
-
import redisClients from './funcs/redisClients.js';
|
|
3
|
-
|
|
4
|
-
function close(fastify) {
|
|
5
|
-
// redis.quit();
|
|
6
|
-
Object.keys(redisClients).forEach((key) => redisClients[key].quit());
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async function plugin(fastify) {
|
|
10
|
-
// const client = getRedis({ db: 0 });
|
|
11
|
-
// client.getJSON = client.get;
|
|
12
|
-
// fastify.decorate('rclient', client);
|
|
13
|
-
// fastify.decorate('getRedis', getRedis);
|
|
14
|
-
fastify.addHook('onClose', close);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default plugin;
|
|
1
|
+
// import redis from './client.js';
|
|
2
|
+
import redisClients from './funcs/redisClients.js';
|
|
3
|
+
|
|
4
|
+
function close(fastify) {
|
|
5
|
+
// redis.quit();
|
|
6
|
+
Object.keys(redisClients).forEach((key) => redisClients[key].quit());
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function plugin(fastify) {
|
|
10
|
+
// const client = getRedis({ db: 0 });
|
|
11
|
+
// client.getJSON = client.get;
|
|
12
|
+
// fastify.decorate('rclient', client);
|
|
13
|
+
// fastify.decorate('getRedis', getRedis);
|
|
14
|
+
fastify.addHook('onClose', close);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default plugin;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import routeData from '../../../routes/table/controllers/data.js';
|
|
2
2
|
|
|
3
|
-
export default async function getData({ id, table, pg, filter, limit, page, search, user }) {
|
|
3
|
+
export default async function getData({ id, table, pg, filter, limit, page, search, user, sql }, reply, called) {
|
|
4
4
|
const params = { table, id };
|
|
5
|
-
const query = { filter, limit, page, search };
|
|
6
|
-
const result = await routeData({ pg, params, query, user });
|
|
5
|
+
const query = { filter, limit, page, search, sql };
|
|
6
|
+
const result = await routeData({ pg, params, query, user, sql }, reply, called);
|
|
7
7
|
return result;
|
|
8
8
|
}
|
|
@@ -7,24 +7,20 @@ import getTableSql from './util/getTableSql.js';
|
|
|
7
7
|
import getFilterQuery from './util/getFilterQuery.js';
|
|
8
8
|
import getOptimizedQuery from './util/getOptimizedQuery.js';
|
|
9
9
|
|
|
10
|
+
const checkInline = {};
|
|
10
11
|
const defaultTable = 'crm.extra_data';
|
|
11
12
|
|
|
12
|
-
function getExtraQuery(mainColumns,
|
|
13
|
-
const extraKeys =
|
|
14
|
-
? Object.keys(schema || {}).filter(key => !mainColumns.map(el => el?.name).includes(key) && extraColumns.map(el => el?.name).includes(key))
|
|
15
|
-
: Object.keys(schema || {}).filter(key => !mainColumns.map(el => el?.name).includes(key));
|
|
13
|
+
function getExtraQuery(mainColumns, schema, table, pk) {
|
|
14
|
+
const extraKeys = Object.keys(schema || {}).filter(key => !mainColumns.map(el => el?.name).includes(key));
|
|
16
15
|
|
|
17
|
-
if (mode === 'column') {
|
|
18
|
-
return { q: `left join lateral (select ${extraKeys.map(key => `"${key}"`).join(',')} from ${table} where ${pk}=t.${pk} limit 1) extra on 1=1`, extraKeys };
|
|
19
|
-
}
|
|
20
16
|
return {
|
|
21
|
-
q: extraKeys.map((el
|
|
17
|
+
q: extraKeys.map((el) => `,(select value_text as "${el.replace(/'/g, "''")}" from ${table} where property_key='${el.replace(/'/g, "''")}' and object_id=t.${pk} limit 1)`).join(' '),
|
|
22
18
|
extraKeys,
|
|
23
19
|
};
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
export default async function getFilterSQL({
|
|
27
|
-
table, filter, pg = pgClients.client, search, filterList, query, custom, state,
|
|
23
|
+
table, filter, pg = pgClients.client, search, filterList, query, custom, state, uid,
|
|
28
24
|
}) {
|
|
29
25
|
if (!table) return { error: 'param table is required', status: 400 };
|
|
30
26
|
|
|
@@ -40,32 +36,45 @@ export default async function getFilterSQL({
|
|
|
40
36
|
|| defaultTable
|
|
41
37
|
: undefined;
|
|
42
38
|
|
|
43
|
-
const { fields:
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
const { fields: fieldsModel = [] } = body?.table && pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
|
|
47
|
-
const { q: extraSqlList, extraKeys } = pg.pk?.[extraDataTable] && pg.pk?.[body?.table]
|
|
48
|
-
? getExtraQuery(fieldsModel, fieldsExtra, loadTemplate?.schema, extraDataTable, pg.pk[body?.table || table], mode)
|
|
39
|
+
const { fields: fieldsModel = [] } = body?.table && pg.pk[body.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
|
|
40
|
+
const { q: extraSqlColumns, extraKeys } = pg.pk?.[extraDataTable] && pg.pk?.[body.table || table]
|
|
41
|
+
? getExtraQuery(fieldsModel, loadTemplate?.schema, extraDataTable, pg.pk[body.table || table])
|
|
49
42
|
: {};
|
|
50
43
|
|
|
51
44
|
// console.log('extra getFilterSQL', extraDataTable, pg.pk?.[extraDataTable]);
|
|
52
45
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
46
|
+
// check sql inline fields count
|
|
47
|
+
if (!checkInline[body.table] && body.sql?.length && body.table) {
|
|
48
|
+
const filterSql = body.sql.filter(el => el.inline ?? true);
|
|
49
|
+
const d = await Promise.all(filterSql.map(el => pg.query(`select q.* from(select * from ${body.table})t left join lateral(${el.sql})q on 1=1 limit 0`).then(el => el.fields)))
|
|
50
|
+
d.forEach((el, i) => {
|
|
51
|
+
filterSql[i].inline = el.length == 1 ? true : false;
|
|
52
|
+
filterSql[i].fields = el.map(f => f.name);
|
|
53
|
+
});
|
|
54
|
+
checkInline[table] = body.sql;
|
|
55
|
+
} else if (checkInline[table]) {
|
|
56
|
+
body.sql = checkInline[table]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const sqlTable = body?.sql?.length
|
|
60
|
+
? body.sql
|
|
61
|
+
.filter((el) => !el?.disabled && !el.inline && el?.sql?.replace && (!el.sql.includes('{{uid}}') || uid))
|
|
62
|
+
.map((el, i) => ` left join lateral (${el.sql.replace('{{uid}}', uid)}) ${el.name || `t${i}`} on 1=1 `)
|
|
63
|
+
.join('')
|
|
64
|
+
: null;
|
|
65
|
+
const sqlInline = body.sql?.filter?.(el => el.inline).map(el => `,(${el.sql})`).join('');
|
|
66
|
+
|
|
67
|
+
const fieldQuery = `select * ${extraSqlColumns || ''} ${sqlInline || ''} from ${body?.table || table} t ${sqlTable || ''} limit 0`;
|
|
68
|
+
const { fields = [] } = !extra ? await pg.query(fieldQuery) : { fields: extraKeys.map(el => ({ name: el.name, dataTypeID: 25 })) };
|
|
62
69
|
const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
|
|
63
70
|
const searchColumn = body?.search_column || body?.meta?.search || autoSearchColumn;
|
|
64
71
|
const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
|
|
72
|
+
|
|
65
73
|
try {
|
|
66
74
|
const tableSQL = await getTableSql({
|
|
67
75
|
pg, body, table, fields,
|
|
68
76
|
});
|
|
77
|
+
|
|
69
78
|
const sval = `ilike '%${decodeURIComponent(search?.replace(/%/g, '%25')).replace(/'/g, "''").replace(/%/g, '\\%')}%'`;
|
|
70
79
|
const searchQuery = search && searchColumn
|
|
71
80
|
? ` (${searchColumn.split(',')?.map((name) => {
|
|
@@ -73,33 +82,40 @@ export default async function getFilterSQL({
|
|
|
73
82
|
return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery.replace(/limit 0/g, '')} where ${name} ${sval} )q where 1=1)` : `${name} ${sval}`;
|
|
74
83
|
}).join(' or ')} )` : '';
|
|
75
84
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
.concat(body?.
|
|
80
|
-
|
|
81
|
-
?.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
const filterList1 = await Promise.all((filterList || (body?.filter_list || [])
|
|
86
|
+
.concat(body?.filterInline || [])
|
|
87
|
+
.concat(body?.filterCustom || [])
|
|
88
|
+
.concat(body?.filterState || [])
|
|
89
|
+
.concat(body?.filterList || [])
|
|
90
|
+
.concat(body?.filters || [])
|
|
91
|
+
)?.filter(el => el.id || el.name)?.map(async (el) => {
|
|
92
|
+
el.name = el.name || el.id;
|
|
93
|
+
|
|
94
|
+
if (el.name && extraKeys?.includes?.(el.name)) {
|
|
95
|
+
Object.assign(el, { extra: { table: extraDataTable } });
|
|
96
|
+
}
|
|
97
|
+
if (!el?.data) return el;
|
|
98
|
+
const cls = await getTemplate(['cls', 'select'], el.data);
|
|
99
|
+
|
|
100
|
+
if (Array.isArray(cls) && cls?.length) {
|
|
101
|
+
Object.assign(el, { cls: el.data, options: cls, select: `select code, name from admin.cls where parent='${el.data}'` });
|
|
102
|
+
}
|
|
103
|
+
else if (typeof (cls?.sql || cls) === 'string') {
|
|
104
|
+
Object.assign(el, { select: cls?.sql || cls });
|
|
105
|
+
}
|
|
106
|
+
return el;
|
|
107
|
+
}));
|
|
108
|
+
|
|
109
|
+
const modelQuery = body?.model || body?.table || table;
|
|
95
110
|
|
|
96
111
|
const filters = getFilterQuery({
|
|
97
112
|
pg,
|
|
98
113
|
filter,
|
|
99
|
-
table:
|
|
114
|
+
table: modelQuery,
|
|
100
115
|
tableSQL,
|
|
101
116
|
fields,
|
|
102
117
|
filterList: filterList1,
|
|
118
|
+
uid,
|
|
103
119
|
});
|
|
104
120
|
|
|
105
121
|
// filter
|
|
@@ -110,16 +126,18 @@ export default async function getFilterSQL({
|
|
|
110
126
|
const q = [body?.query, query, searchQuery, filterQuery, stateQuery, customQuery].filter((el) => el).join(' and ');
|
|
111
127
|
|
|
112
128
|
// table
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
const
|
|
116
|
-
|
|
129
|
+
|
|
130
|
+
const obj = { table: modelQuery, orderby: body?.order || body?.orderby, sqlTable, sqlInline, extraSqlColumns, q };
|
|
131
|
+
const optimizedSQL = `select * from ${getOptimizedQuery(obj)} `;
|
|
132
|
+
const tableCount = getOptimizedQuery(obj, true);
|
|
133
|
+
|
|
117
134
|
return {
|
|
118
135
|
filterList,
|
|
119
|
-
|
|
136
|
+
filters,
|
|
120
137
|
q,
|
|
121
138
|
optimizedSQL,
|
|
122
|
-
|
|
139
|
+
sqlTable,
|
|
140
|
+
extraSqlColumns,
|
|
123
141
|
extraKeys,
|
|
124
142
|
tableCount,
|
|
125
143
|
table: modelQuery,
|