@opengis/fastify-table 1.2.30 → 1.2.32
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 +10 -10
- 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/index.js +7 -7
- package/server/plugins/pg/pgClients.js +21 -21
- 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/getCustomQuery.js +13 -13
- 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/getTemplate.js +1 -1
- 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 +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/form.js +42 -42
- package/server/routes/table/controllers/search.js +74 -74
- 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,13 +1,13 @@
|
|
|
1
|
-
async function getCustomQuery({
|
|
2
|
-
pg, table, customFilter,
|
|
3
|
-
}) {
|
|
4
|
-
if (!customFilter) return null;
|
|
5
|
-
const customFilterList = customFilter?.split(',')?.map((el) => el?.split('_').pop());
|
|
6
|
-
const { property_json: customFilterSQL } = await pg.one(`select json_agg(json_build_object('id',property_id,'name',property_key,'query',property_text)
|
|
7
|
-
) as property_json from admin.properties where property_key is not null and property_entity='customQuery' and object_id=$1`, [table]);
|
|
8
|
-
const data = customFilterSQL?.length ? customFilterSQL.filter((el) => customFilterList.includes(el.id)) || [] : [];
|
|
9
|
-
const customQuery = data?.map((el) => el.query).join(' and ');
|
|
10
|
-
return `${customQuery}`;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default getCustomQuery;
|
|
1
|
+
async function getCustomQuery({
|
|
2
|
+
pg, table, customFilter,
|
|
3
|
+
}) {
|
|
4
|
+
if (!customFilter) return null;
|
|
5
|
+
const customFilterList = customFilter?.split(',')?.map((el) => el?.split('_').pop());
|
|
6
|
+
const { property_json: customFilterSQL } = await pg.one(`select json_agg(json_build_object('id',property_id,'name',property_key,'query',property_text)
|
|
7
|
+
) as property_json from admin.properties where property_key is not null and property_entity='customQuery' and object_id=$1`, [table]);
|
|
8
|
+
const data = customFilterSQL?.length ? customFilterSQL.filter((el) => customFilterList.includes(el.id)) || [] : [];
|
|
9
|
+
const customQuery = data?.map((el) => el.query).join(' and ');
|
|
10
|
+
return `${customQuery}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default getCustomQuery;
|
|
@@ -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;
|
|
@@ -55,7 +55,7 @@ export default async function getTemplate(type, name) {
|
|
|
55
55
|
|
|
56
56
|
const key = `${type}:${name}`;
|
|
57
57
|
if (name === 'cache' && !isProduction) return loadTemplate; // all cache debug
|
|
58
|
-
if (loadTemplate[key] && isProduction) return loadTemplate[key]; // from cache
|
|
58
|
+
if (loadTemplate[key] && isProduction && false) return loadTemplate[key]; // from cache
|
|
59
59
|
|
|
60
60
|
// type one or multi
|
|
61
61
|
const templateList = Array.isArray(type)
|
|
@@ -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 +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;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import eventStream from './funcs/eventStream.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
// fastify.decorate('eventStream', eventStream);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export default plugin;
|
|
1
|
+
import eventStream from './funcs/eventStream.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
// fastify.decorate('eventStream', eventStream);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default plugin;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import cronApi from './controllers/cronApi.js';
|
|
2
|
-
|
|
3
|
-
const cronSchema = {
|
|
4
|
-
params: {
|
|
5
|
-
name: { type: 'string', pattern: '^([\\d\\w._-]+)$' },
|
|
6
|
-
},
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
async function plugin(fastify, config = {}) {
|
|
10
|
-
const prefix = config.prefix || '/api';
|
|
11
|
-
fastify.get(`${prefix}/cron/:name`, { schema: cronSchema }, cronApi);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default plugin;
|
|
1
|
+
import cronApi from './controllers/cronApi.js';
|
|
2
|
+
|
|
3
|
+
const cronSchema = {
|
|
4
|
+
params: {
|
|
5
|
+
name: { type: 'string', pattern: '^([\\d\\w._-]+)$' },
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
async function plugin(fastify, config = {}) {
|
|
10
|
+
const prefix = config.prefix || '/api';
|
|
11
|
+
fastify.get(`${prefix}/cron/:name`, { schema: cronSchema }, cronApi);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default plugin;
|
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import {
|
|
2
|
-
config, getAccess, getTemplate, getMeta, setToken, applyHook, getToken,
|
|
3
|
-
} from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
export default async function tableAPI(req) {
|
|
6
|
-
const {
|
|
7
|
-
pg, params, user = {}, query = {},
|
|
8
|
-
} = req;
|
|
9
|
-
const tokenData = await getToken({ token: params?.table, uid: user.uid, json: 1 }) || {};
|
|
10
|
-
|
|
11
|
-
const hookData = await applyHook('preTable', {
|
|
12
|
-
table: params?.table, id: params?.id, ...tokenData || {}, user,
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
if (hookData?.message && hookData?.status) {
|
|
16
|
-
return { message: hookData?.message, status: hookData?.status };
|
|
17
|
-
}
|
|
18
|
-
const tableName1 = hookData?.table || tokenData.table || params.table;
|
|
19
|
-
|
|
20
|
-
const loadTable = await getTemplate('table', tableName1) || {};
|
|
21
|
-
if (!loadTable && !pg.pk?.[tokenData.table]) {
|
|
22
|
-
return { message: 'not found', status: 404 };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const { table, /* columns, */ form } = loadTable;
|
|
26
|
-
|
|
27
|
-
const tableName = table || hookData?.table || tokenData.table || params.table;
|
|
28
|
-
|
|
29
|
-
const id = hookData?.id || tokenData.id || params.id;
|
|
30
|
-
|
|
31
|
-
if (tokenData && !id) return { message: {} };
|
|
32
|
-
if (!tableName && !id) {
|
|
33
|
-
return { message: 'not enough params', status: 400 };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const { actions = [], query: accessQuery } = await getAccess({
|
|
37
|
-
table: hookData?.table || tokenData.table || params.table,
|
|
38
|
-
id,
|
|
39
|
-
user,
|
|
40
|
-
}) || {};
|
|
41
|
-
|
|
42
|
-
if (!actions.includes('edit') && !config?.local && !tokenData) {
|
|
43
|
-
return { message: 'access restricted', status: 403 };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const { pk, columns: dbColumns = [] } = await getMeta(tableName);
|
|
47
|
-
if (!pk) return { message: `table not found: ${table}`, status: 404 };
|
|
48
|
-
|
|
49
|
-
// const cols = columns.map((el) => el.name || el).join(',');
|
|
50
|
-
const formName = hookData?.form || tokenData?.form || form;
|
|
51
|
-
const formData = await getTemplate('form', formName) || {};
|
|
52
|
-
const schema = formData?.schema || formData;
|
|
53
|
-
// skip DataTable from another table
|
|
54
|
-
const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
|
|
55
|
-
// skip non-existing columns
|
|
56
|
-
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
57
|
-
|
|
58
|
-
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${tableName} limit 0`) : {};
|
|
59
|
-
const cols = loadTable?.table
|
|
60
|
-
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.map((col) => (col?.includes('geom') && schema?.[col]?.type === 'Geom' ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
|
|
61
|
-
: fields.map((el) => (el?.name?.includes('geom') && pg.pgType[el?.dataTypeID] === 'geometry' ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
62
|
-
const where = [`"${pk}" = $1`, loadTable.query, accessQuery].filter((el) => el);
|
|
63
|
-
const geom = dbColumns.find((el) => el.name === 'geom' && pg.pgType[el.dataTypeID] === 'geometry') ? ',st_asgeojson(geom)::json as geom' : '';
|
|
64
|
-
const q = `select "${pk}" as id, ${cols || '*'} ${geom} from ${tableName} t where ${where.join(' and ') || 'true'} limit 1`;
|
|
65
|
-
|
|
66
|
-
if (query?.sql === '1') return q;
|
|
67
|
-
|
|
68
|
-
const data = await pg.query(q, [id]).then(el => el.rows[0]);
|
|
69
|
-
if (!data) return { message: 'not found', status: 404 };
|
|
70
|
-
|
|
71
|
-
if (extraKeys?.length) {
|
|
72
|
-
await Promise.all(extraKeys?.map(async (key) => {
|
|
73
|
-
const { colModel, table: extraTable, parent_id: parentId } = schema[key];
|
|
74
|
-
const q1 = `select ${parentId} as parent, ${colModel.map((col) => col.name || col.key).join(',')} from ${extraTable} a where ${parentId}=$1`;
|
|
75
|
-
// console.log(tableName, formName, q1);
|
|
76
|
-
const { rows: extraRows } = await pg.query(q1, [hookData?.id || tokenData?.id || params?.id]);
|
|
77
|
-
Object.assign(data, { [key]: extraRows });
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
if (user?.uid && actions?.includes?.('edit')) {
|
|
81
|
-
data.token = tokenData?.table ? params.table : setToken({
|
|
82
|
-
ids: [JSON.stringify({ id, table: tableName, form: loadTable.form })],
|
|
83
|
-
uid: user.uid,
|
|
84
|
-
array: 1,
|
|
85
|
-
})[0];
|
|
86
|
-
}
|
|
87
|
-
const res = await applyHook('afterTable', {
|
|
88
|
-
table: tableName, payload: [data], user,
|
|
89
|
-
});
|
|
90
|
-
return res || data || {};
|
|
91
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
config, getAccess, getTemplate, getMeta, setToken, applyHook, getToken,
|
|
3
|
+
} from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
export default async function tableAPI(req) {
|
|
6
|
+
const {
|
|
7
|
+
pg, params, user = {}, query = {},
|
|
8
|
+
} = req;
|
|
9
|
+
const tokenData = await getToken({ token: params?.table, uid: user.uid, json: 1 }) || {};
|
|
10
|
+
|
|
11
|
+
const hookData = await applyHook('preTable', {
|
|
12
|
+
table: params?.table, id: params?.id, ...tokenData || {}, user,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
if (hookData?.message && hookData?.status) {
|
|
16
|
+
return { message: hookData?.message, status: hookData?.status };
|
|
17
|
+
}
|
|
18
|
+
const tableName1 = hookData?.table || tokenData.table || params.table;
|
|
19
|
+
|
|
20
|
+
const loadTable = await getTemplate('table', tableName1) || {};
|
|
21
|
+
if (!loadTable && !pg.pk?.[tokenData.table]) {
|
|
22
|
+
return { message: 'not found', status: 404 };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const { table, /* columns, */ form } = loadTable;
|
|
26
|
+
|
|
27
|
+
const tableName = table || hookData?.table || tokenData.table || params.table;
|
|
28
|
+
|
|
29
|
+
const id = hookData?.id || tokenData.id || params.id;
|
|
30
|
+
|
|
31
|
+
if (tokenData && !id) return { message: {} };
|
|
32
|
+
if (!tableName && !id) {
|
|
33
|
+
return { message: 'not enough params', status: 400 };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { actions = [], query: accessQuery } = await getAccess({
|
|
37
|
+
table: hookData?.table || tokenData.table || params.table,
|
|
38
|
+
id,
|
|
39
|
+
user,
|
|
40
|
+
}) || {};
|
|
41
|
+
|
|
42
|
+
if (!actions.includes('edit') && !config?.local && !tokenData) {
|
|
43
|
+
return { message: 'access restricted', status: 403 };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { pk, columns: dbColumns = [] } = await getMeta(tableName);
|
|
47
|
+
if (!pk) return { message: `table not found: ${table}`, status: 404 };
|
|
48
|
+
|
|
49
|
+
// const cols = columns.map((el) => el.name || el).join(',');
|
|
50
|
+
const formName = hookData?.form || tokenData?.form || form;
|
|
51
|
+
const formData = await getTemplate('form', formName) || {};
|
|
52
|
+
const schema = formData?.schema || formData;
|
|
53
|
+
// skip DataTable from another table
|
|
54
|
+
const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
|
|
55
|
+
// skip non-existing columns
|
|
56
|
+
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
57
|
+
|
|
58
|
+
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${tableName} limit 0`) : {};
|
|
59
|
+
const cols = loadTable?.table
|
|
60
|
+
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.map((col) => (col?.includes('geom') && schema?.[col]?.type === 'Geom' ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
|
|
61
|
+
: fields.map((el) => (el?.name?.includes('geom') && pg.pgType[el?.dataTypeID] === 'geometry' ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
62
|
+
const where = [`"${pk}" = $1`, loadTable.query, accessQuery].filter((el) => el);
|
|
63
|
+
const geom = dbColumns.find((el) => el.name === 'geom' && pg.pgType[el.dataTypeID] === 'geometry') ? ',st_asgeojson(geom)::json as geom' : '';
|
|
64
|
+
const q = `select "${pk}" as id, ${cols || '*'} ${geom} from ${tableName} t where ${where.join(' and ') || 'true'} limit 1`;
|
|
65
|
+
|
|
66
|
+
if (query?.sql === '1') return q;
|
|
67
|
+
|
|
68
|
+
const data = await pg.query(q, [id]).then(el => el.rows[0]);
|
|
69
|
+
if (!data) return { message: 'not found', status: 404 };
|
|
70
|
+
|
|
71
|
+
if (extraKeys?.length) {
|
|
72
|
+
await Promise.all(extraKeys?.map(async (key) => {
|
|
73
|
+
const { colModel, table: extraTable, parent_id: parentId } = schema[key];
|
|
74
|
+
const q1 = `select ${parentId} as parent, ${colModel.map((col) => col.name || col.key).join(',')} from ${extraTable} a where ${parentId}=$1`;
|
|
75
|
+
// console.log(tableName, formName, q1);
|
|
76
|
+
const { rows: extraRows } = await pg.query(q1, [hookData?.id || tokenData?.id || params?.id]);
|
|
77
|
+
Object.assign(data, { [key]: extraRows });
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
if (user?.uid && actions?.includes?.('edit')) {
|
|
81
|
+
data.token = tokenData?.table ? params.table : setToken({
|
|
82
|
+
ids: [JSON.stringify({ id, table: tableName, form: loadTable.form })],
|
|
83
|
+
uid: user.uid,
|
|
84
|
+
array: 1,
|
|
85
|
+
})[0];
|
|
86
|
+
}
|
|
87
|
+
const res = await applyHook('afterTable', {
|
|
88
|
+
table: tableName, payload: [data], user,
|
|
89
|
+
});
|
|
90
|
+
return res || data || {};
|
|
91
|
+
}
|