@opengis/fastify-table 1.1.129 → 1.1.130
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 +78 -78
- package/package.json +55 -55
- package/server/migrations/0.sql +80 -80
- package/server/migrations/cls.sql +39 -39
- package/server/migrations/properties.sql +144 -144
- package/server/migrations/users.sql +171 -171
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +23 -23
- package/server/plugins/crud/funcs/dataInsert.js +45 -45
- 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 +62 -62
- 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/funcs/exec.migrations.js +86 -86
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/funcs/getMeta.js +33 -33
- package/server/plugins/pg/funcs/getPG.js +33 -33
- 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 +170 -170
- 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/getSelect.js +29 -29
- 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/funcs/eventStream.js +28 -28
- package/server/plugins/util/index.js +7 -7
- package/server/routes/cron/index.js +14 -14
- package/server/routes/crud/controllers/deleteCrud.js +36 -36
- package/server/routes/crud/controllers/insert.js +83 -83
- package/server/routes/crud/controllers/table.js +91 -91
- package/server/routes/crud/controllers/update.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/data.js +156 -156
- package/server/routes/table/controllers/filter.js +66 -66
- 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 +122 -122
- 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 +122 -122
|
@@ -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,29 +1,29 @@
|
|
|
1
|
-
import pgClients from '../../pg/pgClients.js';
|
|
2
|
-
import getTemplate from './getTemplate.js';
|
|
3
|
-
|
|
4
|
-
const loadCls = {};
|
|
5
|
-
|
|
6
|
-
const sqls = {
|
|
7
|
-
sql: 'select data from admin.cls where name=$1',
|
|
8
|
-
json: 'select json_agg(json_build_object(\'id\', code, \'text\', name, \'icon\', icon, \'color\', color)) as data from admin.cls where parent=$1',
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export default async function getSelect(name, pg = pgClients.client) {
|
|
12
|
-
if (loadCls[name]) return loadCls[name];
|
|
13
|
-
|
|
14
|
-
const clsDataGIT = await getTemplate(['cls', 'select'], name);
|
|
15
|
-
const { type } = !clsDataGIT && pg.pk?.['admin.cls'] ? await pg.query('select type from admin.cls where parent is null and name=$1 limit 1', [name]).then(el => el.rows?.[0] || {}) : {};
|
|
16
|
-
const q = !clsDataGIT && type && ['sql', 'json'].includes(type) ? sqls[type] : undefined;
|
|
17
|
-
const clsDataDB = q ? await pg.query(q, [name]).then(el => el.rows?.[0]?.data) : undefined;
|
|
18
|
-
const clsData = clsDataGIT || clsDataDB;
|
|
19
|
-
if (!clsData) return null;
|
|
20
|
-
|
|
21
|
-
// console.log(clsData);
|
|
22
|
-
if (clsData && Array.isArray(clsData)) {
|
|
23
|
-
loadCls[name] = { arr: clsData };
|
|
24
|
-
return loadCls[name];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
loadCls[name] = clsData.sql ? clsData : { sql: clsData };
|
|
28
|
-
return loadCls[name];
|
|
29
|
-
}
|
|
1
|
+
import pgClients from '../../pg/pgClients.js';
|
|
2
|
+
import getTemplate from './getTemplate.js';
|
|
3
|
+
|
|
4
|
+
const loadCls = {};
|
|
5
|
+
|
|
6
|
+
const sqls = {
|
|
7
|
+
sql: 'select data from admin.cls where name=$1',
|
|
8
|
+
json: 'select json_agg(json_build_object(\'id\', code, \'text\', name, \'icon\', icon, \'color\', color)) as data from admin.cls where parent=$1',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default async function getSelect(name, pg = pgClients.client) {
|
|
12
|
+
if (loadCls[name]) return loadCls[name];
|
|
13
|
+
|
|
14
|
+
const clsDataGIT = await getTemplate(['cls', 'select'], name);
|
|
15
|
+
const { type } = !clsDataGIT && pg.pk?.['admin.cls'] ? await pg.query('select type from admin.cls where parent is null and name=$1 limit 1', [name]).then(el => el.rows?.[0] || {}) : {};
|
|
16
|
+
const q = !clsDataGIT && type && ['sql', 'json'].includes(type) ? sqls[type] : undefined;
|
|
17
|
+
const clsDataDB = q ? await pg.query(q, [name]).then(el => el.rows?.[0]?.data) : undefined;
|
|
18
|
+
const clsData = clsDataGIT || clsDataDB;
|
|
19
|
+
if (!clsData) return null;
|
|
20
|
+
|
|
21
|
+
// console.log(clsData);
|
|
22
|
+
if (clsData && Array.isArray(clsData)) {
|
|
23
|
+
loadCls[name] = { arr: clsData };
|
|
24
|
+
return loadCls[name];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
loadCls[name] = clsData.sql ? clsData : { sql: clsData };
|
|
28
|
+
return loadCls[name];
|
|
29
|
+
}
|
|
@@ -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 => 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 => 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
|
+
}
|