@opengis/fastify-table 1.2.13 → 1.2.14
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/index.js +79 -79
- package/package.json +1 -1
- package/server/migrations/0.sql +84 -84
- package/server/migrations/cls.sql +39 -39
- package/server/migrations/properties.sql +184 -184
- package/server/migrations/template.sql +34 -34
- package/server/migrations/users.sql +175 -175
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +24 -24
- package/server/plugins/crud/funcs/dataInsert.js +53 -53
- package/server/plugins/crud/funcs/dataUpdate.js +65 -65
- 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 +10 -10
- package/server/plugins/crud/funcs/utils/logChanges.js +118 -118
- 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 +21 -21
- package/server/plugins/migration/exec.migrations.js +37 -37
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/pgClients.js +21 -21
- package/server/plugins/policy/index.js +12 -12
- 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/addTemplateDir.js +8 -8
- package/server/plugins/table/funcs/getFilterSQL/index.js +96 -96
- package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +179 -179
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +66 -66
- package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- 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/metaFormat/getSelectVal.js +50 -50
- package/server/plugins/table/funcs/metaFormat/index.js +45 -45
- 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/deleteCrud.js +39 -39
- package/server/routes/crud/controllers/table.js +91 -91
- 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/filter.js +67 -67
- 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 +166 -166
- 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
- package/utils.js +129 -129
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
/* eslint-disable no-continue */
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
5
|
-
* @param {String} opt.query - запит до таблиці
|
|
6
|
-
* @param {String} opt.hash - інформація з хешу по запиту
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import formatValue from './formatValue.js';
|
|
10
|
-
|
|
11
|
-
function getQuery({
|
|
12
|
-
pg, filter: filterStr, table, tableSQL, fields, filterList,
|
|
13
|
-
}) {
|
|
14
|
-
if (!filterStr) return null; // filter list API
|
|
15
|
-
|
|
16
|
-
const mainOperators = ['=', '~', '>', '<'];
|
|
17
|
-
|
|
18
|
-
const filterQueryArray = decodeURIComponent(filterStr?.replace(/%/g, '%25').replace(/%/g, '\\%')?.replace(/(^,)|(,$)/g, '')).replace(/'/g, '').split(/[;|]/);
|
|
19
|
-
|
|
20
|
-
const resultList = [];
|
|
21
|
-
|
|
22
|
-
for (let i = 0; i < filterQueryArray.length; i += 1) {
|
|
23
|
-
const item = filterQueryArray[i];
|
|
24
|
-
const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
|
|
25
|
-
const [name] = item.split(operator);
|
|
26
|
-
|
|
27
|
-
// skip already added filter
|
|
28
|
-
if (resultList.find((el) => el.name === name)) {
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// filter
|
|
33
|
-
const filter = filterList?.find((el) => [el.id, el.name].includes(name)) || { type: 'text' };
|
|
34
|
-
|
|
35
|
-
// find all value
|
|
36
|
-
const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
|
|
37
|
-
|
|
38
|
-
const optimize = fields?.find((el) => el.name === name) ? null : tableSQL.find((el) => el.name === name);
|
|
39
|
-
|
|
40
|
-
// find field and skip not exists
|
|
41
|
-
const { dataTypeID } = fields?.find((el) => el.name === name) || fields?.find((el) => el.name === optimize?.pk) || {};
|
|
42
|
-
|
|
43
|
-
// format query
|
|
44
|
-
const {
|
|
45
|
-
op, query, filterType, fieldType,
|
|
46
|
-
} = formatValue({
|
|
47
|
-
pg,
|
|
48
|
-
table,
|
|
49
|
-
filter,
|
|
50
|
-
optimize,
|
|
51
|
-
name,
|
|
52
|
-
value,
|
|
53
|
-
operator,
|
|
54
|
-
dataTypeID,
|
|
55
|
-
}) || {};
|
|
56
|
-
if (!query) continue;
|
|
57
|
-
|
|
58
|
-
resultList.push({
|
|
59
|
-
name, value, query, operator: op, filterType, type: fieldType,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return resultList;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export default getQuery;
|
|
1
|
+
/* eslint-disable no-continue */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
5
|
+
* @param {String} opt.query - запит до таблиці
|
|
6
|
+
* @param {String} opt.hash - інформація з хешу по запиту
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import formatValue from './formatValue.js';
|
|
10
|
+
|
|
11
|
+
function getQuery({
|
|
12
|
+
pg, filter: filterStr, table, tableSQL, fields, filterList,
|
|
13
|
+
}) {
|
|
14
|
+
if (!filterStr) return null; // filter list API
|
|
15
|
+
|
|
16
|
+
const mainOperators = ['=', '~', '>', '<'];
|
|
17
|
+
|
|
18
|
+
const filterQueryArray = decodeURIComponent(filterStr?.replace(/%/g, '%25').replace(/%/g, '\\%')?.replace(/(^,)|(,$)/g, '')).replace(/'/g, "''").split(/[;|]/);
|
|
19
|
+
|
|
20
|
+
const resultList = [];
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < filterQueryArray.length; i += 1) {
|
|
23
|
+
const item = filterQueryArray[i];
|
|
24
|
+
const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
|
|
25
|
+
const [name] = item.split(operator);
|
|
26
|
+
|
|
27
|
+
// skip already added filter
|
|
28
|
+
if (resultList.find((el) => el.name === name)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// filter
|
|
33
|
+
const filter = filterList?.find((el) => [el.id, el.name].includes(name)) || { type: 'text' };
|
|
34
|
+
|
|
35
|
+
// find all value
|
|
36
|
+
const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
|
|
37
|
+
|
|
38
|
+
const optimize = fields?.find((el) => el.name === name) ? null : tableSQL.find((el) => el.name === name);
|
|
39
|
+
|
|
40
|
+
// find field and skip not exists
|
|
41
|
+
const { dataTypeID } = fields?.find((el) => el.name === name) || fields?.find((el) => el.name === optimize?.pk) || {};
|
|
42
|
+
|
|
43
|
+
// format query
|
|
44
|
+
const {
|
|
45
|
+
op, query, filterType, fieldType,
|
|
46
|
+
} = formatValue({
|
|
47
|
+
pg,
|
|
48
|
+
table,
|
|
49
|
+
filter,
|
|
50
|
+
optimize,
|
|
51
|
+
name,
|
|
52
|
+
value,
|
|
53
|
+
operator,
|
|
54
|
+
dataTypeID,
|
|
55
|
+
}) || {};
|
|
56
|
+
if (!query) continue;
|
|
57
|
+
|
|
58
|
+
resultList.push({
|
|
59
|
+
name, value, query, operator: op, filterType, type: fieldType,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return resultList;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default getQuery;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
function getOptimizedQuery({ body, table, q }, count) {
|
|
2
|
-
const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
|
|
3
|
-
|
|
4
|
-
const tableName = body?.table || body?.model || table;
|
|
5
|
-
|
|
6
|
-
const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
|
|
7
|
-
.map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
|
|
8
|
-
|
|
9
|
-
return `(select * from ${tableName} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default getOptimizedQuery;
|
|
1
|
+
function getOptimizedQuery({ body, table, q }, count) {
|
|
2
|
+
const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
|
|
3
|
+
|
|
4
|
+
const tableName = body?.table || body?.model || table;
|
|
5
|
+
|
|
6
|
+
const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
|
|
7
|
+
.map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
|
|
8
|
+
|
|
9
|
+
return `(select * from ${tableName} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default getOptimizedQuery;
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
function getTable(table) {
|
|
2
|
-
const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
3
|
-
?.map((el) => el.split(/[ )]/)[0]);
|
|
4
|
-
return result;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
9
|
-
* @param {String} opt.query - запит до таблиці
|
|
10
|
-
* @param {String} opt.hash - інформація з хешу по запиту
|
|
11
|
-
*/
|
|
12
|
-
const tableSql = {};
|
|
13
|
-
async function getTableSql({
|
|
14
|
-
pg, body, table, fields,
|
|
15
|
-
}) {
|
|
16
|
-
if (tableSql[table]) return tableSql[table];
|
|
17
|
-
|
|
18
|
-
const fieldList = fields.map((el) => el.name);
|
|
19
|
-
|
|
20
|
-
const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
|
|
21
|
-
|
|
22
|
-
if (!tableList) { tableSql[table] = []; return []; }
|
|
23
|
-
|
|
24
|
-
const data = await Promise.all(tableList?.map(async (tableEl) => {
|
|
25
|
-
const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`);
|
|
26
|
-
return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
|
|
27
|
-
}));
|
|
28
|
-
|
|
29
|
-
tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
|
|
30
|
-
|
|
31
|
-
return tableSql[table];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default getTableSql;
|
|
1
|
+
function getTable(table) {
|
|
2
|
+
const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
3
|
+
?.map((el) => el.split(/[ )]/)[0]);
|
|
4
|
+
return result;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
9
|
+
* @param {String} opt.query - запит до таблиці
|
|
10
|
+
* @param {String} opt.hash - інформація з хешу по запиту
|
|
11
|
+
*/
|
|
12
|
+
const tableSql = {};
|
|
13
|
+
async function getTableSql({
|
|
14
|
+
pg, body, table, fields,
|
|
15
|
+
}) {
|
|
16
|
+
if (tableSql[table]) return tableSql[table];
|
|
17
|
+
|
|
18
|
+
const fieldList = fields.map((el) => el.name);
|
|
19
|
+
|
|
20
|
+
const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
|
|
21
|
+
|
|
22
|
+
if (!tableList) { tableSql[table] = []; return []; }
|
|
23
|
+
|
|
24
|
+
const data = await Promise.all(tableList?.map(async (tableEl) => {
|
|
25
|
+
const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`);
|
|
26
|
+
return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
|
|
30
|
+
|
|
31
|
+
return tableSql[table];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default getTableSql;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
import config from '../../../../config.js';
|
|
5
|
-
|
|
6
|
-
const loadTemplate = {};
|
|
7
|
-
|
|
8
|
-
export default async function getTemplateDir(type) {
|
|
9
|
-
if (!type) return null;
|
|
10
|
-
|
|
11
|
-
const cwd = process.cwd();
|
|
12
|
-
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
13
|
-
|
|
14
|
-
if (!loadTemplate[type]) {
|
|
15
|
-
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
16
|
-
loadTemplate[type] = typeList;
|
|
17
|
-
}
|
|
18
|
-
return loadTemplate[type];
|
|
19
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import config from '../../../../config.js';
|
|
5
|
+
|
|
6
|
+
const loadTemplate = {};
|
|
7
|
+
|
|
8
|
+
export default async function getTemplateDir(type) {
|
|
9
|
+
if (!type) return null;
|
|
10
|
+
|
|
11
|
+
const cwd = process.cwd();
|
|
12
|
+
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
13
|
+
|
|
14
|
+
if (!loadTemplate[type]) {
|
|
15
|
+
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
16
|
+
loadTemplate[type] = typeList;
|
|
17
|
+
}
|
|
18
|
+
return loadTemplate[type];
|
|
19
|
+
}
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import getSelect from './getSelect.js';
|
|
2
|
-
|
|
3
|
-
import getFilterSQL from './getFilterSQL/index.js';
|
|
4
|
-
import getTemplate from './getTemplate.js';
|
|
5
|
-
import pgClients from '../../pg/pgClients.js';
|
|
6
|
-
import config from '../../../../config.js';
|
|
7
|
-
import getSelectVal from './metaFormat/getSelectVal.js';
|
|
8
|
-
|
|
9
|
-
export default async function gisIRColumn({
|
|
10
|
-
pg = pgClients.client, layer, column, sql, query = '1=1', filter, state, search, custom,
|
|
11
|
-
}) {
|
|
12
|
-
const time = Date.now();
|
|
13
|
-
|
|
14
|
-
const sel = await getSelect(query.cls || column, pg);
|
|
15
|
-
|
|
16
|
-
const body = await getTemplate('table', layer);
|
|
17
|
-
const fData = await getFilterSQL({
|
|
18
|
-
table: layer,
|
|
19
|
-
filter,
|
|
20
|
-
state,
|
|
21
|
-
search,
|
|
22
|
-
custom,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const { tlist } = await pg.one(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist from pg_class
|
|
26
|
-
where relkind in ('r','v','m')`);
|
|
27
|
-
|
|
28
|
-
const tableName = body?.table || layer;
|
|
29
|
-
if (!tlist.includes(tableName)) return { error: `table not found: ${tableName}`, status: 400 };
|
|
30
|
-
|
|
31
|
-
// eslint-disable-next-line max-len
|
|
32
|
-
const { fields } = await pg.query(`select * from (${fData?.optimizedSQL || `select * from ${tableName}`})q limit 0`);
|
|
33
|
-
|
|
34
|
-
const col = fields.find((el) => el.name === column);
|
|
35
|
-
|
|
36
|
-
if (!col) return { status: 404, message: 'not found' };
|
|
37
|
-
const colField = pg.pgType[col.dataTypeID]?.includes('[]') ? `unnest(${column})` : column;
|
|
38
|
-
|
|
39
|
-
const q = `select ${colField} as id, count(*)::int from (
|
|
40
|
-
${fData?.optimizedSQL || `select * from ${tableName} where ${body?.query || 'true'}`}
|
|
41
|
-
)t group by ${colField} order by count desc limit 15`;
|
|
42
|
-
|
|
43
|
-
if (sql) return q;
|
|
44
|
-
|
|
45
|
-
if (!body?.columns?.length) {
|
|
46
|
-
const { rows } = await pg.query(q);
|
|
47
|
-
if (sel?.arr?.length) {
|
|
48
|
-
rows.forEach((el) => {
|
|
49
|
-
const data = sel?.find((item) => item.id?.toString() === el.id?.toString());
|
|
50
|
-
Object.assign(el, data || {});
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
count: rows?.reduce((acc, el) => acc + el.count, 0),
|
|
55
|
-
sql: config.local ? q : undefined,
|
|
56
|
-
rows,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const { rows } = await pg.query(q);
|
|
61
|
-
const cls = query.cls || body?.columns?.find((el) => el.name === column)?.data || col.data || col.option;
|
|
62
|
-
const select = await getSelectVal({
|
|
63
|
-
pg, name: cls, values: rows.map((el) => el.id), ar: 1,
|
|
64
|
-
});
|
|
65
|
-
rows.forEach((el) => {
|
|
66
|
-
if (Array.isArray(select)) {
|
|
67
|
-
Object.assign(el, select.find((item) => item.id?.toString() === el.id?.toString()) || {});
|
|
68
|
-
}
|
|
69
|
-
else if (typeof select?.[el.id] === 'string') {
|
|
70
|
-
Object.assign(el, { text: select?.[el.id] });
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
Object.assign(el, select?.[el.id] || {});
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
return {
|
|
77
|
-
time: Date.now() - time,
|
|
78
|
-
count: rows.reduce((acc, el) => acc + el.count, 0),
|
|
79
|
-
sql: config.local ? q : undefined,
|
|
80
|
-
rows,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
1
|
+
import getSelect from './getSelect.js';
|
|
2
|
+
|
|
3
|
+
import getFilterSQL from './getFilterSQL/index.js';
|
|
4
|
+
import getTemplate from './getTemplate.js';
|
|
5
|
+
import pgClients from '../../pg/pgClients.js';
|
|
6
|
+
import config from '../../../../config.js';
|
|
7
|
+
import getSelectVal from './metaFormat/getSelectVal.js';
|
|
8
|
+
|
|
9
|
+
export default async function gisIRColumn({
|
|
10
|
+
pg = pgClients.client, layer, column, sql, query = '1=1', filter, state, search, custom,
|
|
11
|
+
}) {
|
|
12
|
+
const time = Date.now();
|
|
13
|
+
|
|
14
|
+
const sel = await getSelect(query.cls || column, pg);
|
|
15
|
+
|
|
16
|
+
const body = await getTemplate('table', layer);
|
|
17
|
+
const fData = await getFilterSQL({
|
|
18
|
+
table: layer,
|
|
19
|
+
filter,
|
|
20
|
+
state,
|
|
21
|
+
search,
|
|
22
|
+
custom,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const { tlist } = await pg.one(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist from pg_class
|
|
26
|
+
where relkind in ('r','v','m')`);
|
|
27
|
+
|
|
28
|
+
const tableName = body?.table || layer;
|
|
29
|
+
if (!tlist.includes(tableName)) return { error: `table not found: ${tableName}`, status: 400 };
|
|
30
|
+
|
|
31
|
+
// eslint-disable-next-line max-len
|
|
32
|
+
const { fields } = await pg.query(`select * from (${fData?.optimizedSQL || `select * from ${tableName}`})q limit 0`);
|
|
33
|
+
|
|
34
|
+
const col = fields.find((el) => el.name === column);
|
|
35
|
+
|
|
36
|
+
if (!col) return { status: 404, message: 'not found' };
|
|
37
|
+
const colField = pg.pgType[col.dataTypeID]?.includes('[]') ? `unnest(${column})` : column;
|
|
38
|
+
|
|
39
|
+
const q = `select ${colField} as id, count(*)::int from (
|
|
40
|
+
${fData?.optimizedSQL || `select * from ${tableName} where ${body?.query || 'true'}`}
|
|
41
|
+
)t group by ${colField} order by count desc limit 15`;
|
|
42
|
+
|
|
43
|
+
if (sql) return q;
|
|
44
|
+
|
|
45
|
+
if (!body?.columns?.length) {
|
|
46
|
+
const { rows } = await pg.query(q);
|
|
47
|
+
if (sel?.arr?.length) {
|
|
48
|
+
rows.forEach((el) => {
|
|
49
|
+
const data = sel?.find((item) => item.id?.toString() === el.id?.toString());
|
|
50
|
+
Object.assign(el, data || {});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
count: rows?.reduce((acc, el) => acc + el.count, 0),
|
|
55
|
+
sql: config.local ? q : undefined,
|
|
56
|
+
rows,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const { rows } = await pg.query(q);
|
|
61
|
+
const cls = query.cls || body?.columns?.find((el) => el.name === column)?.data || col.data || col.option;
|
|
62
|
+
const select = await getSelectVal({
|
|
63
|
+
pg, name: cls, values: rows.map((el) => el.id), ar: 1,
|
|
64
|
+
});
|
|
65
|
+
rows.forEach((el) => {
|
|
66
|
+
if (Array.isArray(select)) {
|
|
67
|
+
Object.assign(el, select.find((item) => item.id?.toString() === el.id?.toString()) || {});
|
|
68
|
+
}
|
|
69
|
+
else if (typeof select?.[el.id] === 'string') {
|
|
70
|
+
Object.assign(el, { text: select?.[el.id] });
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
Object.assign(el, select?.[el.id] || {});
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
time: Date.now() - time,
|
|
78
|
+
count: rows.reduce((acc, el) => acc + el.count, 0),
|
|
79
|
+
sql: config.local ? q : undefined,
|
|
80
|
+
rows,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import getSelect from '../getSelect.js';
|
|
2
|
-
import pgClients from '../../../pg/pgClients.js';
|
|
3
|
-
import redis from '../../../redis/client.js';
|
|
4
|
-
|
|
5
|
-
const selectIds = {};
|
|
6
|
-
export default async function getSelectVal({
|
|
7
|
-
pg = pgClients.client, name, values: valuesOrigin, ar = false,
|
|
8
|
-
}) {
|
|
9
|
-
if (!valuesOrigin?.length) return null;
|
|
10
|
-
const values = valuesOrigin.filter(el => typeof el === 'boolean' ? true : el).map(el => el.toString());
|
|
11
|
-
const cls = await getSelect(name, pg);
|
|
12
|
-
|
|
13
|
-
// === array ===
|
|
14
|
-
if (cls?.arr && Array.isArray(cls?.arr)) {
|
|
15
|
-
const resultArr = cls.arr.filter(el => values.includes(el.id.toString()));
|
|
16
|
-
if (ar) return resultArr;
|
|
17
|
-
return resultArr.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// === sql ===
|
|
21
|
-
if (!cls?.sql) return null;
|
|
22
|
-
|
|
23
|
-
// select id column name
|
|
24
|
-
if (!selectIds[name]) selectIds[name] = await pg.queryCache(`select * from (${cls.sql})q limit 0`).then((res) => res.fields?.[0]?.name);
|
|
25
|
-
const id = selectIds[name];
|
|
26
|
-
|
|
27
|
-
// cache
|
|
28
|
-
const key = `select:${name}`;
|
|
29
|
-
const cache = values?.length ? (await redis.hmget(key, values)).reduce((p, el, i) => ({ ...p, [values[i]]: el }), {}) : {};
|
|
30
|
-
const filteredValues = values.filter(el => !cache[el]);
|
|
31
|
-
|
|
32
|
-
// query select
|
|
33
|
-
const q = `with c(id,text) as (select * from (${cls.sql})q where ${id} = any('{${filteredValues}}')) select * from c`;
|
|
34
|
-
const data = filteredValues.length ? await pg.query(q).then(el => el.rows) : [];
|
|
35
|
-
|
|
36
|
-
const clsObj = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
|
|
37
|
-
|
|
38
|
-
if (data?.length) {
|
|
39
|
-
redis.hmset(key, clsObj);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (ar) {
|
|
43
|
-
return Object.keys(clsObj).reduce((acc, el) => {
|
|
44
|
-
acc.push({ id: el, text: typeof clsObj[el] === 'string' ? clsObj[el] : clsObj[el]?.text, color: clsObj[el]?.color });
|
|
45
|
-
return acc;
|
|
46
|
-
}, []);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return clsObj;
|
|
50
|
-
}
|
|
1
|
+
import getSelect from '../getSelect.js';
|
|
2
|
+
import pgClients from '../../../pg/pgClients.js';
|
|
3
|
+
import redis from '../../../redis/client.js';
|
|
4
|
+
|
|
5
|
+
const selectIds = {};
|
|
6
|
+
export default async function getSelectVal({
|
|
7
|
+
pg = pgClients.client, name, values: valuesOrigin, ar = false,
|
|
8
|
+
}) {
|
|
9
|
+
if (!valuesOrigin?.length) return null;
|
|
10
|
+
const values = valuesOrigin.filter(el => typeof el === 'boolean' ? true : el).map(el => el.toString());
|
|
11
|
+
const cls = await getSelect(name, pg);
|
|
12
|
+
|
|
13
|
+
// === array ===
|
|
14
|
+
if (cls?.arr && Array.isArray(cls?.arr)) {
|
|
15
|
+
const resultArr = cls.arr.filter(el => values.includes(el.id.toString()));
|
|
16
|
+
if (ar) return resultArr;
|
|
17
|
+
return resultArr.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// === sql ===
|
|
21
|
+
if (!cls?.sql) return null;
|
|
22
|
+
|
|
23
|
+
// select id column name
|
|
24
|
+
if (!selectIds[name]) selectIds[name] = await pg.queryCache(`select * from (${cls.sql})q limit 0`).then((res) => res.fields?.[0]?.name);
|
|
25
|
+
const id = selectIds[name];
|
|
26
|
+
|
|
27
|
+
// cache
|
|
28
|
+
const key = `select:${name}`;
|
|
29
|
+
const cache = values?.length ? (await redis.hmget(key, values)).reduce((p, el, i) => ({ ...p, [values[i]]: el }), {}) : {};
|
|
30
|
+
const filteredValues = values.filter(el => !cache[el]);
|
|
31
|
+
|
|
32
|
+
// query select
|
|
33
|
+
const q = `with c(id,text) as (select * from (${cls.sql})q where ${id} = any('{${filteredValues}}')) select * from c`;
|
|
34
|
+
const data = filteredValues.length ? await pg.query(q).then(el => el.rows) : [];
|
|
35
|
+
|
|
36
|
+
const clsObj = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
|
|
37
|
+
|
|
38
|
+
if (data?.length) {
|
|
39
|
+
redis.hmset(key, clsObj);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (ar) {
|
|
43
|
+
return Object.keys(clsObj).reduce((acc, el) => {
|
|
44
|
+
acc.push({ id: el, text: typeof clsObj[el] === 'string' ? clsObj[el] : clsObj[el]?.text, color: clsObj[el]?.color });
|
|
45
|
+
return acc;
|
|
46
|
+
}, []);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return clsObj;
|
|
50
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { handlebars } from '@opengis/fastify-hb/utils.js';
|
|
2
|
-
|
|
3
|
-
import getTemplate from '../getTemplate.js';
|
|
4
|
-
import getSelectVal from './getSelectVal.js';
|
|
5
|
-
|
|
6
|
-
export default async function metaFormat({
|
|
7
|
-
rows, table, cls, sufix = true,
|
|
8
|
-
}) {
|
|
9
|
-
const loadTable = table ? await getTemplate('table', table) : {};
|
|
10
|
-
const selectCols = Object.keys(cls || {}).map(key => ({ name: key, data: cls[key] })).concat(loadTable?.columns?.filter((e) => e.data) || []);
|
|
11
|
-
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
12
|
-
const htmlCols = loadTable?.columns?.filter?.((e) => e.name && e.format === 'html' && e.html) || [];
|
|
13
|
-
if (!selectCols?.length && !metaCls?.length && !htmlCols?.length) return rows;
|
|
14
|
-
|
|
15
|
-
await Promise.all(htmlCols.map(async (attr) => {
|
|
16
|
-
await Promise.all(rows.filter(row => row?.[attr.name])?.map(async (row) => {
|
|
17
|
-
const html = await handlebars.compile(attr.html)(row);
|
|
18
|
-
Object.assign(row, { [attr.name]: html?.replaceAll(/<[^>]*>/g, '')?.replace(/\n/g, '')?.trim() });
|
|
19
|
-
}));
|
|
20
|
-
}));
|
|
21
|
-
// cls & select format
|
|
22
|
-
|
|
23
|
-
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
24
|
-
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
25
|
-
if (!values.length) return null;
|
|
26
|
-
|
|
27
|
-
const clsValues = await getSelectVal({ name: attr.data, values });
|
|
28
|
-
if (!clsValues) return null;
|
|
29
|
-
|
|
30
|
-
rows.forEach(el => {
|
|
31
|
-
const val = el[attr.name]?.map?.(c => c ? clsValues[c.toString()] || clsValues[c] || c : null) || clsValues[el[attr.name]?.toString()] || clsValues[el[attr.name]] || el[attr.name];
|
|
32
|
-
if (!val) return;
|
|
33
|
-
if (!sufix) {
|
|
34
|
-
Object.assign(el, { [attr.name]: val.text || val });
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
return null;
|
|
42
|
-
}));
|
|
43
|
-
|
|
44
|
-
return rows;
|
|
45
|
-
}
|
|
1
|
+
import { handlebars } from '@opengis/fastify-hb/utils.js';
|
|
2
|
+
|
|
3
|
+
import getTemplate from '../getTemplate.js';
|
|
4
|
+
import getSelectVal from './getSelectVal.js';
|
|
5
|
+
|
|
6
|
+
export default async function metaFormat({
|
|
7
|
+
rows, table, cls, sufix = true,
|
|
8
|
+
}) {
|
|
9
|
+
const loadTable = table ? await getTemplate('table', table) : {};
|
|
10
|
+
const selectCols = Object.keys(cls || {}).map(key => ({ name: key, data: cls[key] })).concat(loadTable?.columns?.filter((e) => e.data) || []);
|
|
11
|
+
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
12
|
+
const htmlCols = loadTable?.columns?.filter?.((e) => e.name && e.format === 'html' && e.html) || [];
|
|
13
|
+
if (!selectCols?.length && !metaCls?.length && !htmlCols?.length) return rows;
|
|
14
|
+
|
|
15
|
+
await Promise.all(htmlCols.map(async (attr) => {
|
|
16
|
+
await Promise.all(rows.filter(row => row?.[attr.name])?.map(async (row) => {
|
|
17
|
+
const html = await handlebars.compile(attr.html)(row);
|
|
18
|
+
Object.assign(row, { [attr.name]: html?.replaceAll(/<[^>]*>/g, '')?.replace(/\n/g, '')?.trim() });
|
|
19
|
+
}));
|
|
20
|
+
}));
|
|
21
|
+
// cls & select format
|
|
22
|
+
|
|
23
|
+
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
24
|
+
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
25
|
+
if (!values.length) return null;
|
|
26
|
+
|
|
27
|
+
const clsValues = await getSelectVal({ name: attr.data, values });
|
|
28
|
+
if (!clsValues) return null;
|
|
29
|
+
|
|
30
|
+
rows.forEach(el => {
|
|
31
|
+
const val = el[attr.name]?.map?.(c => c ? clsValues[c.toString()] || clsValues[c] || c : null) || clsValues[el[attr.name]?.toString()] || clsValues[el[attr.name]] || el[attr.name];
|
|
32
|
+
if (!val) return;
|
|
33
|
+
if (!sufix) {
|
|
34
|
+
Object.assign(el, { [attr.name]: val.text || val });
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return null;
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
return rows;
|
|
45
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default [];
|
|
1
|
+
export default [];
|