@opengis/fastify-table 1.1.97 → 1.1.98
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/index.js +78 -78
- package/package.json +45 -45
- package/server/migrations/0.sql +80 -80
- package/server/migrations/cls.sql +39 -39
- package/server/migrations/properties.sql +144 -144
- package/server/migrations/roles.sql +188 -188
- package/server/migrations/users.sql +170 -170
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +21 -21
- package/server/plugins/crud/funcs/dataInsert.js +41 -41
- package/server/plugins/crud/funcs/dataUpdate.js +64 -64
- package/server/plugins/crud/funcs/getAccess.js +48 -48
- 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 +29 -29
- package/server/plugins/pg/funcs/getPG.js +33 -33
- package/server/plugins/pg/pgClients.js +21 -21
- package/server/plugins/policy/funcs/checkPolicy.js +92 -92
- 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/getSelectMeta.js +67 -67
- 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 +31 -31
- 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 +80 -80
- package/server/routes/crud/controllers/table.js +91 -91
- package/server/routes/crud/controllers/update.js +82 -82
- package/server/routes/crud/index.js +21 -21
- 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 +144 -144
- package/server/routes/table/controllers/filter.js +68 -60
- 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 +88 -88
- package/server/routes/table/index.js +28 -28
- 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,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,67 +1,67 @@
|
|
|
1
|
-
import logger from '../../logger/getLogger.js';
|
|
2
|
-
import autoIndex from '../../pg/funcs/autoIndex.js';
|
|
3
|
-
import pgClients from '../../pg/pgClients.js';
|
|
4
|
-
|
|
5
|
-
import getSelect from './getSelect.js';
|
|
6
|
-
|
|
7
|
-
const limit = 50;
|
|
8
|
-
const selectMeta = {};
|
|
9
|
-
|
|
10
|
-
export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
|
|
11
|
-
if (selectMeta[name] && !nocache) return selectMeta[name];
|
|
12
|
-
|
|
13
|
-
const cls = await getSelect(name, pg);
|
|
14
|
-
|
|
15
|
-
if (!cls) return null;
|
|
16
|
-
if (cls.arr) return cls;
|
|
17
|
-
if (!cls.sql) return null;
|
|
18
|
-
|
|
19
|
-
const { sql: original } = cls;
|
|
20
|
-
if (!original.toLowerCase) { console.log(`sql select null: ${name}`); return null; }
|
|
21
|
-
|
|
22
|
-
const sql = `with c(id,text) as (select * from (${original})q limit ${limit}) select * from c`;
|
|
23
|
-
|
|
24
|
-
/*= == meta table === */
|
|
25
|
-
|
|
26
|
-
const tableNew = original.toLowerCase().replace(/\n/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
27
|
-
.map((el) => el.split(/[ )]/)[0].replace(/[\r\n]+/g, ''));
|
|
28
|
-
|
|
29
|
-
const dataOrigin = await pg.query(sql?.replace(`limit ${limit}`, 'limit 0'));
|
|
30
|
-
|
|
31
|
-
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
32
|
-
|
|
33
|
-
// const table = getTable(original);
|
|
34
|
-
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
35
|
-
|
|
36
|
-
// column name
|
|
37
|
-
const cols = dataOrigin.fields.map((el) => el.name);
|
|
38
|
-
const originalCols = dataOrigin1.fields.map((el) => el.name);
|
|
39
|
-
const type = dataOrigin.fields.map((el) => pg.pgType?.[el.dataTypeID] || 'text');
|
|
40
|
-
|
|
41
|
-
autoIndex({ table: tableNew[0], columns: [originalCols[1]].concat(cls?.searchColumn?.split(',')), gin: 1 }).catch(err => logger.error(err));
|
|
42
|
-
|
|
43
|
-
const searchColumn = cls?.searchColumn || (
|
|
44
|
-
dataOrigin.fields.find((el) => el.name === 'search') ? 'search' : dataOrigin1.fields[1].name);
|
|
45
|
-
|
|
46
|
-
const searchQuery = `(${searchColumn.split(',').map((el) => `lower("${el}") ~ $1 `).join(' or ')})`;
|
|
47
|
-
// console.log(dataOrigin1.fields.map((el) => el.name).join(','));
|
|
48
|
-
const data = {
|
|
49
|
-
minLength: cls.minLength,
|
|
50
|
-
limit: cls.limit,
|
|
51
|
-
type: type.join(','),
|
|
52
|
-
cols: cols.join(','),
|
|
53
|
-
originalCols: originalCols.join(','),
|
|
54
|
-
pk: originalCols[0],
|
|
55
|
-
db: cls.db,
|
|
56
|
-
original: `select * from (${original})q`,
|
|
57
|
-
sql,
|
|
58
|
-
count,
|
|
59
|
-
searchQuery,
|
|
60
|
-
table: tableNew.join(','),
|
|
61
|
-
time: new Date().toISOString(),
|
|
62
|
-
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
selectMeta[name] = data;
|
|
66
|
-
return data;
|
|
67
|
-
}
|
|
1
|
+
import logger from '../../logger/getLogger.js';
|
|
2
|
+
import autoIndex from '../../pg/funcs/autoIndex.js';
|
|
3
|
+
import pgClients from '../../pg/pgClients.js';
|
|
4
|
+
|
|
5
|
+
import getSelect from './getSelect.js';
|
|
6
|
+
|
|
7
|
+
const limit = 50;
|
|
8
|
+
const selectMeta = {};
|
|
9
|
+
|
|
10
|
+
export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
|
|
11
|
+
if (selectMeta[name] && !nocache) return selectMeta[name];
|
|
12
|
+
|
|
13
|
+
const cls = await getSelect(name, pg);
|
|
14
|
+
|
|
15
|
+
if (!cls) return null;
|
|
16
|
+
if (cls.arr) return cls;
|
|
17
|
+
if (!cls.sql) return null;
|
|
18
|
+
|
|
19
|
+
const { sql: original } = cls;
|
|
20
|
+
if (!original.toLowerCase) { console.log(`sql select null: ${name}`); return null; }
|
|
21
|
+
|
|
22
|
+
const sql = `with c(id,text) as (select * from (${original})q limit ${limit}) select * from c`;
|
|
23
|
+
|
|
24
|
+
/*= == meta table === */
|
|
25
|
+
|
|
26
|
+
const tableNew = original.toLowerCase().replace(/\n/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
27
|
+
.map((el) => el.split(/[ )]/)[0].replace(/[\r\n]+/g, ''));
|
|
28
|
+
|
|
29
|
+
const dataOrigin = await pg.query(sql?.replace(`limit ${limit}`, 'limit 0'));
|
|
30
|
+
|
|
31
|
+
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
32
|
+
|
|
33
|
+
// const table = getTable(original);
|
|
34
|
+
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
35
|
+
|
|
36
|
+
// column name
|
|
37
|
+
const cols = dataOrigin.fields.map((el) => el.name);
|
|
38
|
+
const originalCols = dataOrigin1.fields.map((el) => el.name);
|
|
39
|
+
const type = dataOrigin.fields.map((el) => pg.pgType?.[el.dataTypeID] || 'text');
|
|
40
|
+
|
|
41
|
+
autoIndex({ table: tableNew[0], columns: [originalCols[1]].concat(cls?.searchColumn?.split(',')), gin: 1 }).catch(err => logger.error(err));
|
|
42
|
+
|
|
43
|
+
const searchColumn = cls?.searchColumn || (
|
|
44
|
+
dataOrigin.fields.find((el) => el.name === 'search') ? 'search' : dataOrigin1.fields[1].name);
|
|
45
|
+
|
|
46
|
+
const searchQuery = `(${searchColumn.split(',').map((el) => `lower("${el}") ~ $1 `).join(' or ')})`;
|
|
47
|
+
// console.log(dataOrigin1.fields.map((el) => el.name).join(','));
|
|
48
|
+
const data = {
|
|
49
|
+
minLength: cls.minLength,
|
|
50
|
+
limit: cls.limit,
|
|
51
|
+
type: type.join(','),
|
|
52
|
+
cols: cols.join(','),
|
|
53
|
+
originalCols: originalCols.join(','),
|
|
54
|
+
pk: originalCols[0],
|
|
55
|
+
db: cls.db,
|
|
56
|
+
original: `select * from (${original})q`,
|
|
57
|
+
sql,
|
|
58
|
+
count,
|
|
59
|
+
searchQuery,
|
|
60
|
+
table: tableNew.join(','),
|
|
61
|
+
time: new Date().toISOString(),
|
|
62
|
+
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
selectMeta[name] = data;
|
|
66
|
+
return data;
|
|
67
|
+
}
|
|
@@ -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.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 = (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 = (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,31 +1,31 @@
|
|
|
1
|
-
import getTemplate from '../getTemplate.js';
|
|
2
|
-
import getSelectVal from './getSelectVal.js';
|
|
3
|
-
|
|
4
|
-
// import { getTemplate, getSelectVal } from '../../../../../utils.js';
|
|
5
|
-
|
|
6
|
-
export default async function metaFormat({ rows, table }) {
|
|
7
|
-
const loadTable = await getTemplate('table', table);
|
|
8
|
-
const selectCols = loadTable?.columns?.filter((e) => e.data);
|
|
9
|
-
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
10
|
-
if (!selectCols?.length && !metaCls?.length) return rows;
|
|
11
|
-
|
|
12
|
-
// cls & select format
|
|
13
|
-
|
|
14
|
-
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
15
|
-
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
16
|
-
if (!values.length) return null;
|
|
17
|
-
|
|
18
|
-
const cls = await getSelectVal({ name: attr.data, values });
|
|
19
|
-
if (!cls) return null;
|
|
20
|
-
|
|
21
|
-
rows.forEach(el => {
|
|
22
|
-
const val = el[attr.name]?.map?.(c => cls[c.toString()] || cls[c] || c) || cls[el[attr.name]?.toString()] || cls[el[attr.name]] || el[attr.name];
|
|
23
|
-
if (!val) return;
|
|
24
|
-
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
return null;
|
|
28
|
-
}));
|
|
29
|
-
|
|
30
|
-
return rows;
|
|
31
|
-
}
|
|
1
|
+
import getTemplate from '../getTemplate.js';
|
|
2
|
+
import getSelectVal from './getSelectVal.js';
|
|
3
|
+
|
|
4
|
+
// import { getTemplate, getSelectVal } from '../../../../../utils.js';
|
|
5
|
+
|
|
6
|
+
export default async function metaFormat({ rows, table }) {
|
|
7
|
+
const loadTable = await getTemplate('table', table);
|
|
8
|
+
const selectCols = loadTable?.columns?.filter((e) => e.data);
|
|
9
|
+
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
10
|
+
if (!selectCols?.length && !metaCls?.length) return rows;
|
|
11
|
+
|
|
12
|
+
// cls & select format
|
|
13
|
+
|
|
14
|
+
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
15
|
+
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
16
|
+
if (!values.length) return null;
|
|
17
|
+
|
|
18
|
+
const cls = await getSelectVal({ name: attr.data, values });
|
|
19
|
+
if (!cls) return null;
|
|
20
|
+
|
|
21
|
+
rows.forEach(el => {
|
|
22
|
+
const val = el[attr.name]?.map?.(c => cls[c.toString()] || cls[c] || c) || cls[el[attr.name]?.toString()] || cls[el[attr.name]] || el[attr.name];
|
|
23
|
+
if (!val) return;
|
|
24
|
+
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return null;
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
return rows;
|
|
31
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default [];
|
|
1
|
+
export default [];
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import metaFormat from './funcs/metaFormat/index.js';
|
|
2
|
-
import getFilterSQL from './funcs/getFilterSQL/index.js';
|
|
3
|
-
import getTemplate from './funcs/getTemplate.js';
|
|
4
|
-
import getSelect from './funcs/getSelect.js';
|
|
5
|
-
|
|
6
|
-
async function plugin(fastify) {
|
|
7
|
-
// fastify.decorate('metaFormat', metaFormat);
|
|
8
|
-
// fastify.decorate('getFilterSQL', getFilterSQL);
|
|
9
|
-
// fastify.decorate('getTemplate', getTemplate);
|
|
10
|
-
// fastify.decorate('getSelect', getSelect);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default plugin;
|
|
1
|
+
import metaFormat from './funcs/metaFormat/index.js';
|
|
2
|
+
import getFilterSQL from './funcs/getFilterSQL/index.js';
|
|
3
|
+
import getTemplate from './funcs/getTemplate.js';
|
|
4
|
+
import getSelect from './funcs/getSelect.js';
|
|
5
|
+
|
|
6
|
+
async function plugin(fastify) {
|
|
7
|
+
// fastify.decorate('metaFormat', metaFormat);
|
|
8
|
+
// fastify.decorate('getFilterSQL', getFilterSQL);
|
|
9
|
+
// fastify.decorate('getTemplate', getTemplate);
|
|
10
|
+
// fastify.decorate('getSelect', getSelect);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default plugin;
|