@opengis/fastify-table 1.1.59 → 1.1.61
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 +109 -118
- package/package.json +46 -46
- 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 +175 -172
- package/server/migrations/users.sql +168 -168
- package/server/plugins/cron/funcs/addCron.js +128 -128
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +21 -21
- package/server/plugins/crud/funcs/dataInsert.js +33 -33
- package/server/plugins/crud/funcs/dataUpdate.js +50 -50
- package/server/plugins/crud/funcs/getAccess.js +48 -48
- package/server/plugins/crud/funcs/getOpt.js +12 -12
- package/server/plugins/crud/funcs/setOpt.js +19 -19
- package/server/plugins/crud/funcs/setToken.js +56 -56
- 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/migration/funcs/exec.migrations.js +79 -79
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/funcs/autoIndex.js +0 -1
- package/server/plugins/pg/funcs/getMeta.js +28 -29
- package/server/plugins/pg/funcs/getPG.js +30 -30
- package/server/plugins/pg/index.js +1 -3
- 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 +15 -15
- 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 +19 -19
- package/server/plugins/table/funcs/getSelectMeta.js +57 -57
- package/server/plugins/table/funcs/getTemplates.js +19 -19
- package/server/plugins/table/funcs/gisIRColumn.js +72 -69
- 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 +23 -23
- package/server/plugins/table/funcs/metaFormat/index.js +30 -30
- 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 +73 -73
- package/server/routes/crud/controllers/update.js +76 -76
- package/server/routes/crud/index.js +19 -19
- 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 -15
- 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 +153 -151
- package/server/routes/table/controllers/filter.js +60 -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 +84 -84
- package/server/routes/table/controllers/table.js +81 -81
- 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 +120 -119
- package/server/plugins/pg/funcs/pgClients.js +0 -2
- /package/server/{plugins/logger → routes/logger/controllers}/logger.test.api.js +0 -0
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getMeta, metaFormat, getTemplates, getTemplate, handlebars,
|
|
3
|
-
} from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
function sequence(tables, data, fn) {
|
|
6
|
-
return tables.reduce((promise, table) => promise.then(() => fn({
|
|
7
|
-
...data, tableName: table.replace('.json', ''),
|
|
8
|
-
})), Promise.resolve());
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async function getData({
|
|
12
|
-
pg, tableName, query = {}, maxLimit, res,
|
|
13
|
-
}) {
|
|
14
|
-
const loadTable = await getTemplate('table', tableName);
|
|
15
|
-
|
|
16
|
-
if (!loadTable) { return { message: 'not found', status: 404 }; }
|
|
17
|
-
|
|
18
|
-
const {
|
|
19
|
-
table, columns, meta,
|
|
20
|
-
} = loadTable;
|
|
21
|
-
|
|
22
|
-
const { pk } = await getMeta(table);
|
|
23
|
-
|
|
24
|
-
const cols = columns.map((el) => el.name || el).join(',');
|
|
25
|
-
const [orderColumn, orderDir] = (query.order || loadTable.order || '').split('-');
|
|
26
|
-
const order = cols.includes(orderColumn) && orderColumn?.length ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}` : '';
|
|
27
|
-
|
|
28
|
-
const limit = Math.max(maxLimit - res.rows.length, 0);
|
|
29
|
-
// Math.max(query.offset - res.rows.length,0)
|
|
30
|
-
const offset = query.page && query.page > 0 ? ` offset ${(query.page - 1) * limit}` : '';
|
|
31
|
-
|
|
32
|
-
const search1 = meta?.search && query.key ? `(${meta?.search.concat(meta?.title ? `,${meta?.title}` : '').split(',').map(el => `${el} ilike '%${query.key}%'`).join(' or ')})` : 'false';
|
|
33
|
-
|
|
34
|
-
const where = [!pk ? 'false' : 'true', loadTable.query, search1].filter((el) => el);
|
|
35
|
-
const q = `select ${[`"${pk}" as id`, meta?.title ? `${meta.title} as title` : ''].filter((el) => el).join(',')} from ${table} t where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
|
|
36
|
-
if (query.sql) {
|
|
37
|
-
res.sql.push(q);
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const { rows } = await pg.query(q);
|
|
42
|
-
|
|
43
|
-
const total = await pg.queryCache(`select count(*) from ${table} t where ${where.join(' and ') || 'true'}`).then((el) => el?.rows[0]?.count) || 0;
|
|
44
|
-
|
|
45
|
-
await metaFormat({ rows, table: tableName });
|
|
46
|
-
res.total += +total;
|
|
47
|
-
rows.forEach((row) => {
|
|
48
|
-
const href = meta?.href ? handlebars.compile(meta.href)({ ...row, [pk]: row.id }) : undefined;
|
|
49
|
-
res.rows.push({
|
|
50
|
-
...row, register: tableName, register_title: loadTable.ua, href,
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export default async function search({
|
|
57
|
-
pg, funcs, query = {},
|
|
58
|
-
}) {
|
|
59
|
-
const time = Date.now();
|
|
60
|
-
|
|
61
|
-
const tables = query.table ? [query.table] : await getTemplates('table');
|
|
62
|
-
const res = { rows: [], sql: [], total: 0 };
|
|
63
|
-
|
|
64
|
-
const maxLimit = Math.min(100, query.limit || '16');
|
|
65
|
-
await sequence(tables, {
|
|
66
|
-
pg, funcs, query, maxLimit, res,
|
|
67
|
-
}, getData);
|
|
68
|
-
|
|
69
|
-
if (query.sql) return res.sql.join(';\n');
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
time: Date.now() - time, total: res.total, count: res.rows.length, rows: res.rows,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
getMeta, metaFormat, getTemplates, getTemplate, handlebars,
|
|
3
|
+
} from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
function sequence(tables, data, fn) {
|
|
6
|
+
return tables.reduce((promise, table) => promise.then(() => fn({
|
|
7
|
+
...data, tableName: table.replace('.json', ''),
|
|
8
|
+
})), Promise.resolve());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function getData({
|
|
12
|
+
pg, tableName, query = {}, maxLimit, res,
|
|
13
|
+
}) {
|
|
14
|
+
const loadTable = await getTemplate('table', tableName);
|
|
15
|
+
|
|
16
|
+
if (!loadTable) { return { message: 'not found', status: 404 }; }
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
table, columns, meta,
|
|
20
|
+
} = loadTable;
|
|
21
|
+
|
|
22
|
+
const { pk } = await getMeta(table);
|
|
23
|
+
|
|
24
|
+
const cols = columns.map((el) => el.name || el).join(',');
|
|
25
|
+
const [orderColumn, orderDir] = (query.order || loadTable.order || '').split('-');
|
|
26
|
+
const order = cols.includes(orderColumn) && orderColumn?.length ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}` : '';
|
|
27
|
+
|
|
28
|
+
const limit = Math.max(maxLimit - res.rows.length, 0);
|
|
29
|
+
// Math.max(query.offset - res.rows.length,0)
|
|
30
|
+
const offset = query.page && query.page > 0 ? ` offset ${(query.page - 1) * limit}` : '';
|
|
31
|
+
|
|
32
|
+
const search1 = meta?.search && query.key ? `(${meta?.search.concat(meta?.title ? `,${meta?.title}` : '').split(',').map(el => `${el} ilike '%${query.key}%'`).join(' or ')})` : 'false';
|
|
33
|
+
|
|
34
|
+
const where = [!pk ? 'false' : 'true', loadTable.query, search1].filter((el) => el);
|
|
35
|
+
const q = `select ${[`"${pk}" as id`, meta?.title ? `${meta.title} as title` : ''].filter((el) => el).join(',')} from ${table} t where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
|
|
36
|
+
if (query.sql) {
|
|
37
|
+
res.sql.push(q);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const { rows } = await pg.query(q);
|
|
42
|
+
|
|
43
|
+
const total = await pg.queryCache(`select count(*) from ${table} t where ${where.join(' and ') || 'true'}`).then((el) => el?.rows[0]?.count) || 0;
|
|
44
|
+
|
|
45
|
+
await metaFormat({ rows, table: tableName });
|
|
46
|
+
res.total += +total;
|
|
47
|
+
rows.forEach((row) => {
|
|
48
|
+
const href = meta?.href ? handlebars.compile(meta.href)({ ...row, [pk]: row.id }) : undefined;
|
|
49
|
+
res.rows.push({
|
|
50
|
+
...row, register: tableName, register_title: loadTable.ua, href,
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default async function search({
|
|
57
|
+
pg, funcs, query = {},
|
|
58
|
+
}) {
|
|
59
|
+
const time = Date.now();
|
|
60
|
+
|
|
61
|
+
const tables = query.table ? [query.table] : await getTemplates('table');
|
|
62
|
+
const res = { rows: [], sql: [], total: 0 };
|
|
63
|
+
|
|
64
|
+
const maxLimit = Math.min(100, query.limit || '16');
|
|
65
|
+
await sequence(tables, {
|
|
66
|
+
pg, funcs, query, maxLimit, res,
|
|
67
|
+
}, getData);
|
|
68
|
+
|
|
69
|
+
if (query.sql) return res.sql.join(';\n');
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
time: Date.now() - time, total: res.total, count: res.rows.length, rows: res.rows,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
import {
|
|
2
|
-
config, getPG, getTemplate, getSelectMeta,
|
|
3
|
-
} from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
const limit = 50;
|
|
6
|
-
const headers = {
|
|
7
|
-
'Access-Control-Allow-Origin': '*',
|
|
8
|
-
'Access-Control-Allow-Methods': 'GET',
|
|
9
|
-
'Cache-Control': 'no-cache',
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export default async function suggest(req) {
|
|
13
|
-
const {
|
|
14
|
-
params, query, pg: pg1, user,
|
|
15
|
-
} = req;
|
|
16
|
-
|
|
17
|
-
const lang = query.lang || 'ua';
|
|
18
|
-
const time = Date.now();
|
|
19
|
-
const parent = query.parent || '';
|
|
20
|
-
|
|
21
|
-
const [table, column] = params.data?.includes(':') ? params.data.split(':') : [];
|
|
22
|
-
const selectName = query.sel || query.name || params.data;
|
|
23
|
-
if (!selectName) return { headers, status: 400, message: 'name is required' };
|
|
24
|
-
|
|
25
|
-
const body = await getTemplate('table', table);
|
|
26
|
-
if (table && !pg1.pk[body?.table || table]) {
|
|
27
|
-
return { headers, status: 400, message: 'param name is invalid: 1' };
|
|
28
|
-
}
|
|
29
|
-
const columnExists = body?.columns?.find((col) => col?.name === column);
|
|
30
|
-
if (table && (!column || !columnExists)) {
|
|
31
|
-
return { headers, status: 400, message: 'param name is invalid: 2' };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const meta = table && column
|
|
35
|
-
? {
|
|
36
|
-
original: `with c(id,text) as (select row_number() over(), ${column} from ${body?.table || table} group by ${column} ) select * from c`,
|
|
37
|
-
searchQuery: '("text" ilike $1 )',
|
|
38
|
-
}
|
|
39
|
-
: await getSelectMeta({ name: selectName, nocache: query?.nocache });
|
|
40
|
-
const pg = meta?.db ? getPG(meta.db) : pg1;
|
|
41
|
-
if (!meta) return { headers, status: 404, message: 'Not found query select ' };
|
|
42
|
-
if (query.meta) return meta;
|
|
43
|
-
|
|
44
|
-
const { arr, searchQuery } = meta;
|
|
45
|
-
|
|
46
|
-
if (arr) {
|
|
47
|
-
const lower = query.key?.toLowerCase();
|
|
48
|
-
const data = query.key || query.val
|
|
49
|
-
? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
|
|
50
|
-
: arr;
|
|
51
|
-
return {
|
|
52
|
-
limit, count: data.length, mode: 'array', time: Date.now() - time, data,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// search
|
|
57
|
-
const search = query.key ? searchQuery : null;
|
|
58
|
-
|
|
59
|
-
// val
|
|
60
|
-
// const pk = meta.originalCols.split(',')[0];
|
|
61
|
-
|
|
62
|
-
const val = query.val ? ` id=any('{${query.val.replace(/'/g, "''")}}')` : '';
|
|
63
|
-
const where = [search, val, 'id is not null'].filter((el) => el).join(' and ') || 'true';
|
|
64
|
-
const sqlSuggest = `${meta.original.replace(/{{parent}}/gi, parent)} where ${where} limit ${limit}`.replace(/{{uid}}/g, user?.uid || '0');
|
|
65
|
-
if (query.sql && config.local) return sqlSuggest;
|
|
66
|
-
|
|
67
|
-
// query
|
|
68
|
-
const { rows: dataNew } = meta.searchColumn ? { rows: [] } : await pg.query(sqlSuggest, query.key ? [`${query.key}%`] : []);
|
|
69
|
-
const { rows: dataNew1 } = dataNew.length < limit ? await pg.query(sqlSuggest, query.key ? [`%${query.key}%`] : []) : {};
|
|
70
|
-
const ids = dataNew.map((el) => el.id);
|
|
71
|
-
const data = dataNew.concat((dataNew1 || []).filter((el) => !ids?.includes(el.id)));
|
|
72
|
-
|
|
73
|
-
const message = {
|
|
74
|
-
time: Date.now() - time,
|
|
75
|
-
count: data.length,
|
|
76
|
-
total: meta.count - 0,
|
|
77
|
-
mode: 'sql',
|
|
78
|
-
db: meta.db,
|
|
79
|
-
sql: config.local ? sqlSuggest : undefined,
|
|
80
|
-
data,
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
return message;
|
|
84
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
config, getPG, getTemplate, getSelectMeta,
|
|
3
|
+
} from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
const limit = 50;
|
|
6
|
+
const headers = {
|
|
7
|
+
'Access-Control-Allow-Origin': '*',
|
|
8
|
+
'Access-Control-Allow-Methods': 'GET',
|
|
9
|
+
'Cache-Control': 'no-cache',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default async function suggest(req) {
|
|
13
|
+
const {
|
|
14
|
+
params, query, pg: pg1, user,
|
|
15
|
+
} = req;
|
|
16
|
+
|
|
17
|
+
const lang = query.lang || 'ua';
|
|
18
|
+
const time = Date.now();
|
|
19
|
+
const parent = query.parent || '';
|
|
20
|
+
|
|
21
|
+
const [table, column] = params.data?.includes(':') ? params.data.split(':') : [];
|
|
22
|
+
const selectName = query.sel || query.name || params.data;
|
|
23
|
+
if (!selectName) return { headers, status: 400, message: 'name is required' };
|
|
24
|
+
|
|
25
|
+
const body = await getTemplate('table', table);
|
|
26
|
+
if (table && !pg1.pk[body?.table || table]) {
|
|
27
|
+
return { headers, status: 400, message: 'param name is invalid: 1' };
|
|
28
|
+
}
|
|
29
|
+
const columnExists = body?.columns?.find((col) => col?.name === column);
|
|
30
|
+
if (table && (!column || !columnExists)) {
|
|
31
|
+
return { headers, status: 400, message: 'param name is invalid: 2' };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const meta = table && column
|
|
35
|
+
? {
|
|
36
|
+
original: `with c(id,text) as (select row_number() over(), ${column} from ${body?.table || table} group by ${column} ) select * from c`,
|
|
37
|
+
searchQuery: '("text" ilike $1 )',
|
|
38
|
+
}
|
|
39
|
+
: await getSelectMeta({ name: selectName, nocache: query?.nocache });
|
|
40
|
+
const pg = meta?.db ? getPG(meta.db) : pg1;
|
|
41
|
+
if (!meta) return { headers, status: 404, message: 'Not found query select ' };
|
|
42
|
+
if (query.meta) return meta;
|
|
43
|
+
|
|
44
|
+
const { arr, searchQuery } = meta;
|
|
45
|
+
|
|
46
|
+
if (arr) {
|
|
47
|
+
const lower = query.key?.toLowerCase();
|
|
48
|
+
const data = query.key || query.val
|
|
49
|
+
? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
|
|
50
|
+
: arr;
|
|
51
|
+
return {
|
|
52
|
+
limit, count: data.length, mode: 'array', time: Date.now() - time, data,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// search
|
|
57
|
+
const search = query.key ? searchQuery : null;
|
|
58
|
+
|
|
59
|
+
// val
|
|
60
|
+
// const pk = meta.originalCols.split(',')[0];
|
|
61
|
+
|
|
62
|
+
const val = query.val ? ` id=any('{${query.val.replace(/'/g, "''")}}')` : '';
|
|
63
|
+
const where = [search, val, 'id is not null'].filter((el) => el).join(' and ') || 'true';
|
|
64
|
+
const sqlSuggest = `${meta.original.replace(/{{parent}}/gi, parent)} where ${where} limit ${limit}`.replace(/{{uid}}/g, user?.uid || '0');
|
|
65
|
+
if (query.sql && config.local) return sqlSuggest;
|
|
66
|
+
|
|
67
|
+
// query
|
|
68
|
+
const { rows: dataNew } = meta.searchColumn ? { rows: [] } : await pg.query(sqlSuggest, query.key ? [`${query.key}%`] : []);
|
|
69
|
+
const { rows: dataNew1 } = dataNew.length < limit ? await pg.query(sqlSuggest, query.key ? [`%${query.key}%`] : []) : {};
|
|
70
|
+
const ids = dataNew.map((el) => el.id);
|
|
71
|
+
const data = dataNew.concat((dataNew1 || []).filter((el) => !ids?.includes(el.id)));
|
|
72
|
+
|
|
73
|
+
const message = {
|
|
74
|
+
time: Date.now() - time,
|
|
75
|
+
count: data.length,
|
|
76
|
+
total: meta.count - 0,
|
|
77
|
+
mode: 'sql',
|
|
78
|
+
db: meta.db,
|
|
79
|
+
sql: config.local ? sqlSuggest : undefined,
|
|
80
|
+
data,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return message;
|
|
84
|
+
}
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
import {
|
|
2
|
-
config, getAccess, getTemplate, getMeta, setToken, applyHook,
|
|
3
|
-
} from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
export default async function tableAPI(req) {
|
|
6
|
-
const {
|
|
7
|
-
pg, params, user, query = {},
|
|
8
|
-
} = req;
|
|
9
|
-
const hookData = await applyHook('preTable', {
|
|
10
|
-
table: params?.table, id: params?.id, user,
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
if (hookData?.message && hookData?.status) {
|
|
14
|
-
return { message: hookData?.message, status: hookData?.status };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (!params?.id && !hookData?.id) {
|
|
18
|
-
return { message: 'not enough params', status: 400 };
|
|
19
|
-
}
|
|
20
|
-
const tableName = hookData?.table || params.table;
|
|
21
|
-
const loadTable = await getTemplate('table', tableName) || {};
|
|
22
|
-
if (!loadTable || (pg.pk?.[tableName])) {
|
|
23
|
-
return { message: 'not found', status: 404 };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const { actions = [], query: accessQuery } = await getAccess({
|
|
27
|
-
table: hookData?.table || params.table,
|
|
28
|
-
id: hookData?.id || params?.id,
|
|
29
|
-
user,
|
|
30
|
-
}) || {};
|
|
31
|
-
|
|
32
|
-
if (!actions.includes('edit') && !config?.local) {
|
|
33
|
-
return { message: 'access restricted', status: 403 };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const { table, /* columns, */ form } = loadTable;
|
|
37
|
-
|
|
38
|
-
const { pk, columns: dbColumns = [] } = await getMeta(table || hookData?.table || params.table);
|
|
39
|
-
if (!pk) return { message: `table not found: ${table}`, status: 404 };
|
|
40
|
-
|
|
41
|
-
// const cols = columns.map((el) => el.name || el).join(',');
|
|
42
|
-
const schema = await getTemplate('form', hookData?.form || form) || {};
|
|
43
|
-
// skip DataTable from another table
|
|
44
|
-
const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
|
|
45
|
-
// skip non-existing columns
|
|
46
|
-
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
47
|
-
|
|
48
|
-
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${table || hookData?.table || params.table} limit 0`) : {};
|
|
49
|
-
const cols = loadTable?.table
|
|
50
|
-
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.map((col) => (col?.includes('geom') ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
|
|
51
|
-
: fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
52
|
-
const where = [`"${pk}" = $1`, loadTable.query, accessQuery].filter((el) => el);
|
|
53
|
-
const geom = columnList.includes('geom') ? ',st_asgeojson(geom)::json as geom' : '';
|
|
54
|
-
const q = `select "${pk}" as id, ${cols || '*'} ${geom} from ${table || hookData?.table || params.table} t where ${where.join(' and ') || 'true'} limit 1`;
|
|
55
|
-
|
|
56
|
-
if (query?.sql === '1') return q;
|
|
57
|
-
|
|
58
|
-
const data = await pg.query(q, [hookData?.id || params.id]).then(el => el.rows[0]);
|
|
59
|
-
if (!data) return { message: 'not found', status: 404 };
|
|
60
|
-
|
|
61
|
-
if (extraKeys?.length) {
|
|
62
|
-
await Promise.all(extraKeys?.map(async (key) => {
|
|
63
|
-
const { colModel, table: extraTable, parent_id: parentId } = schema[key];
|
|
64
|
-
const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [hookData?.id || params?.id]);
|
|
65
|
-
Object.assign(data, { [key]: extraRows });
|
|
66
|
-
}));
|
|
67
|
-
}
|
|
68
|
-
if (user.uid) {
|
|
69
|
-
const [token] = setToken({
|
|
70
|
-
ids: [JSON.stringify({ id: params?.id, table: params.table || loadTable.table, form: loadTable.form })],
|
|
71
|
-
mode: 'w',
|
|
72
|
-
uid: user.uid,
|
|
73
|
-
array: 1,
|
|
74
|
-
});
|
|
75
|
-
data.token = token;
|
|
76
|
-
}
|
|
77
|
-
const res = await applyHook('afterTable', {
|
|
78
|
-
table: loadTable?.table, payload: [data], user,
|
|
79
|
-
});
|
|
80
|
-
return res || data || {};
|
|
81
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
config, getAccess, getTemplate, getMeta, setToken, applyHook,
|
|
3
|
+
} from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
export default async function tableAPI(req) {
|
|
6
|
+
const {
|
|
7
|
+
pg, params, user, query = {},
|
|
8
|
+
} = req;
|
|
9
|
+
const hookData = await applyHook('preTable', {
|
|
10
|
+
table: params?.table, id: params?.id, user,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
if (hookData?.message && hookData?.status) {
|
|
14
|
+
return { message: hookData?.message, status: hookData?.status };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!params?.id && !hookData?.id) {
|
|
18
|
+
return { message: 'not enough params', status: 400 };
|
|
19
|
+
}
|
|
20
|
+
const tableName = hookData?.table || params.table;
|
|
21
|
+
const loadTable = await getTemplate('table', tableName) || {};
|
|
22
|
+
if (!loadTable || (pg.pk?.[tableName])) {
|
|
23
|
+
return { message: 'not found', status: 404 };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { actions = [], query: accessQuery } = await getAccess({
|
|
27
|
+
table: hookData?.table || params.table,
|
|
28
|
+
id: hookData?.id || params?.id,
|
|
29
|
+
user,
|
|
30
|
+
}) || {};
|
|
31
|
+
|
|
32
|
+
if (!actions.includes('edit') && !config?.local) {
|
|
33
|
+
return { message: 'access restricted', status: 403 };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { table, /* columns, */ form } = loadTable;
|
|
37
|
+
|
|
38
|
+
const { pk, columns: dbColumns = [] } = await getMeta(table || hookData?.table || params.table);
|
|
39
|
+
if (!pk) return { message: `table not found: ${table}`, status: 404 };
|
|
40
|
+
|
|
41
|
+
// const cols = columns.map((el) => el.name || el).join(',');
|
|
42
|
+
const schema = await getTemplate('form', hookData?.form || form) || {};
|
|
43
|
+
// skip DataTable from another table
|
|
44
|
+
const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
|
|
45
|
+
// skip non-existing columns
|
|
46
|
+
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
47
|
+
|
|
48
|
+
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${table || hookData?.table || params.table} limit 0`) : {};
|
|
49
|
+
const cols = loadTable?.table
|
|
50
|
+
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.map((col) => (col?.includes('geom') ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
|
|
51
|
+
: fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
52
|
+
const where = [`"${pk}" = $1`, loadTable.query, accessQuery].filter((el) => el);
|
|
53
|
+
const geom = columnList.includes('geom') ? ',st_asgeojson(geom)::json as geom' : '';
|
|
54
|
+
const q = `select "${pk}" as id, ${cols || '*'} ${geom} from ${table || hookData?.table || params.table} t where ${where.join(' and ') || 'true'} limit 1`;
|
|
55
|
+
|
|
56
|
+
if (query?.sql === '1') return q;
|
|
57
|
+
|
|
58
|
+
const data = await pg.query(q, [hookData?.id || params.id]).then(el => el.rows[0]);
|
|
59
|
+
if (!data) return { message: 'not found', status: 404 };
|
|
60
|
+
|
|
61
|
+
if (extraKeys?.length) {
|
|
62
|
+
await Promise.all(extraKeys?.map(async (key) => {
|
|
63
|
+
const { colModel, table: extraTable, parent_id: parentId } = schema[key];
|
|
64
|
+
const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [hookData?.id || params?.id]);
|
|
65
|
+
Object.assign(data, { [key]: extraRows });
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
if (user.uid) {
|
|
69
|
+
const [token] = setToken({
|
|
70
|
+
ids: [JSON.stringify({ id: params?.id, table: params.table || loadTable.table, form: loadTable.form })],
|
|
71
|
+
mode: 'w',
|
|
72
|
+
uid: user.uid,
|
|
73
|
+
array: 1,
|
|
74
|
+
});
|
|
75
|
+
data.token = token;
|
|
76
|
+
}
|
|
77
|
+
const res = await applyHook('afterTable', {
|
|
78
|
+
table: loadTable?.table, payload: [data], user,
|
|
79
|
+
});
|
|
80
|
+
return res || data || {};
|
|
81
|
+
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import suggest from './controllers/suggest.js';
|
|
2
|
-
import data from './controllers/data.js';
|
|
3
|
-
import table from './controllers/table.js';
|
|
4
|
-
import card from './controllers/card.js';
|
|
5
|
-
import search from './controllers/search.js';
|
|
6
|
-
import filter from './controllers/filter.js';
|
|
7
|
-
import form from './controllers/form.js';
|
|
8
|
-
|
|
9
|
-
import loadTemplatePath from '../../plugins/table/funcs/loadTemplatePath.js';
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
tableSchema, searchSchema, suggestSchema, formSchema, filterSchema,
|
|
13
|
-
} from './schema.js';
|
|
14
|
-
|
|
15
|
-
async function plugin(fastify, config = {}) {
|
|
16
|
-
const prefix = config.prefix || '/api';
|
|
17
|
-
fastify.get(`${prefix}/suggest/:data`, { schema: suggestSchema }, suggest);
|
|
18
|
-
fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
|
|
19
|
-
fastify.get(`${prefix}/table/:table/:id?`, { schema: tableSchema }, table);
|
|
20
|
-
fastify.get(`${prefix}/card/:table/:id`, { schema: tableSchema }, card);
|
|
21
|
-
fastify.get(`${prefix}/search`, { schema: searchSchema }, search);
|
|
22
|
-
|
|
23
|
-
fastify.get(`${prefix}/templates`, () => loadTemplatePath);
|
|
24
|
-
fastify.get(`${prefix}/filter/:table`, { schema: filterSchema }, filter);
|
|
25
|
-
fastify.get(`${prefix}/form/:form`, { schema: formSchema }, form);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export default plugin;
|
|
1
|
+
import suggest from './controllers/suggest.js';
|
|
2
|
+
import data from './controllers/data.js';
|
|
3
|
+
import table from './controllers/table.js';
|
|
4
|
+
import card from './controllers/card.js';
|
|
5
|
+
import search from './controllers/search.js';
|
|
6
|
+
import filter from './controllers/filter.js';
|
|
7
|
+
import form from './controllers/form.js';
|
|
8
|
+
|
|
9
|
+
import loadTemplatePath from '../../plugins/table/funcs/loadTemplatePath.js';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
tableSchema, searchSchema, suggestSchema, formSchema, filterSchema,
|
|
13
|
+
} from './schema.js';
|
|
14
|
+
|
|
15
|
+
async function plugin(fastify, config = {}) {
|
|
16
|
+
const prefix = config.prefix || '/api';
|
|
17
|
+
fastify.get(`${prefix}/suggest/:data`, { schema: suggestSchema }, suggest);
|
|
18
|
+
fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
|
|
19
|
+
fastify.get(`${prefix}/table/:table/:id?`, { schema: tableSchema }, table);
|
|
20
|
+
fastify.get(`${prefix}/card/:table/:id`, { schema: tableSchema }, card);
|
|
21
|
+
fastify.get(`${prefix}/search`, { schema: searchSchema }, search);
|
|
22
|
+
|
|
23
|
+
fastify.get(`${prefix}/templates`, () => loadTemplatePath);
|
|
24
|
+
fastify.get(`${prefix}/filter/:table`, { schema: filterSchema }, filter);
|
|
25
|
+
fastify.get(`${prefix}/form/:form`, { schema: formSchema }, form);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default plugin;
|