@opengis/fastify-table 1.2.13 → 1.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -86
- package/index.js +79 -79
- package/package.json +1 -1
- package/server/migrations/0.sql +84 -84
- package/server/migrations/cls.sql +39 -39
- package/server/migrations/properties.sql +184 -184
- package/server/migrations/template.sql +34 -34
- package/server/migrations/users.sql +175 -175
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +24 -24
- package/server/plugins/crud/funcs/dataInsert.js +53 -53
- package/server/plugins/crud/funcs/dataUpdate.js +65 -65
- package/server/plugins/crud/funcs/getOpt.js +13 -13
- package/server/plugins/crud/funcs/setOpt.js +21 -21
- package/server/plugins/crud/funcs/setToken.js +44 -44
- package/server/plugins/crud/funcs/utils/getFolder.js +10 -10
- package/server/plugins/crud/funcs/utils/logChanges.js +118 -118
- package/server/plugins/crud/index.js +23 -23
- package/server/plugins/hook/index.js +8 -8
- package/server/plugins/logger/errorStatus.js +19 -19
- package/server/plugins/logger/index.js +21 -21
- package/server/plugins/migration/exec.migrations.js +37 -37
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/pgClients.js +21 -21
- package/server/plugins/policy/index.js +12 -12
- package/server/plugins/policy/sqlInjection.js +33 -33
- package/server/plugins/redis/client.js +8 -8
- package/server/plugins/redis/funcs/redisClients.js +3 -3
- package/server/plugins/redis/index.js +17 -17
- package/server/plugins/table/funcs/addTemplateDir.js +8 -8
- package/server/plugins/table/funcs/getFilterSQL/index.js +96 -96
- package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +179 -179
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +66 -66
- package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/plugins/table/funcs/getSelectMeta.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/metaFormat/getSelectVal.js +50 -50
- package/server/plugins/table/funcs/metaFormat/index.js +45 -45
- package/server/plugins/table/funcs/userTemplateDir.js +1 -1
- package/server/plugins/table/index.js +13 -13
- package/server/plugins/util/index.js +7 -7
- package/server/routes/cron/index.js +14 -14
- package/server/routes/crud/controllers/deleteCrud.js +39 -39
- package/server/routes/crud/controllers/table.js +91 -91
- package/server/routes/logger/controllers/logger.file.js +92 -92
- package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
- package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
- package/server/routes/logger/index.js +17 -17
- package/server/routes/properties/controllers/properties.add.js +55 -55
- package/server/routes/properties/controllers/properties.get.js +17 -17
- package/server/routes/properties/index.js +16 -16
- package/server/routes/table/controllers/filter.js +67 -67
- package/server/routes/table/controllers/form.js +42 -42
- package/server/routes/table/controllers/search.js +74 -74
- package/server/routes/table/controllers/suggest.js +166 -166
- package/server/routes/table/index.js +29 -29
- package/server/routes/table/schema.js +64 -64
- package/server/routes/util/controllers/status.monitor.js +8 -8
- package/server/routes/util/index.js +11 -11
- package/utils.js +129 -129
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
import getTemplate from '../../..//table/funcs/getTemplate.js';
|
|
2
|
-
import { metaFormat } from '@opengis/fastify-table/utils.js';
|
|
3
|
-
|
|
4
|
-
const defaultTitles = {
|
|
5
|
-
editor_date: 'Дата оновлення',
|
|
6
|
-
editor_id: 'Редактор',
|
|
7
|
-
сdate: 'Дата створення',
|
|
8
|
-
uid: 'Автор',
|
|
9
|
-
body: 'Зміст',
|
|
10
|
-
entity_id: 'ID Сутності',
|
|
11
|
-
entity_type: 'Таблиця сутності',
|
|
12
|
-
file_path: 'Шлях до файлу',
|
|
13
|
-
uploaded_name: 'Назва файлу',
|
|
14
|
-
size: 'Розмір файлу',
|
|
15
|
-
ext: 'Розширення файлу',
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
function getValue(val, tableName) {
|
|
19
|
-
if (!val) return null;
|
|
20
|
-
if (['crm.files'].includes(tableName)) {
|
|
21
|
-
return typeof val === 'object'
|
|
22
|
-
? JSON.stringify(val)
|
|
23
|
-
: val;
|
|
24
|
-
}
|
|
25
|
-
return typeof val === 'object'
|
|
26
|
-
? JSON.stringify(val)?.substring?.(0, 30)
|
|
27
|
-
: val?.toString?.()?.substring?.(0, 30);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// extract titles and cls from form schema
|
|
31
|
-
// alt: extract table template from referer -> form
|
|
32
|
-
export default async function logChanges({
|
|
33
|
-
pg, table: table1, tokenData, referer, id, data, uid = 1, type,
|
|
34
|
-
}) {
|
|
35
|
-
const table = table1.replace(/"/g, '');
|
|
36
|
-
if (!id) {
|
|
37
|
-
console.error('param id is required');
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
if (!table || !pg.pk?.[table]) {
|
|
41
|
-
console.error('table not found');
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
if (!pg.pk?.['log.table_changes'] || !pg.pk?.['log.table_changes_data']) {
|
|
45
|
-
console.error('log table not found');
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
if (!type) {
|
|
49
|
-
console.error('invalid type');
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
const { change_id: changeId } = await pg.query(`insert into log.table_changes(change_date,change_type,change_user_id,entity_type,entity_id)
|
|
55
|
-
values(CURRENT_DATE, $1, $2, $3, $4) returning change_id`, [type, uid, table, id]).then((res) => res.rows?.[0] || {});
|
|
56
|
-
|
|
57
|
-
const q = `select json_object_agg(entity_key, value_new) from (
|
|
58
|
-
select
|
|
59
|
-
entity_key,
|
|
60
|
-
value_new,
|
|
61
|
-
( rank() over (partition by entity_key order by cdate desc) = 1 ) as is_latest
|
|
62
|
-
from log.table_changes_data
|
|
63
|
-
|
|
64
|
-
where change_id in (
|
|
65
|
-
select
|
|
66
|
-
change_id
|
|
67
|
-
from log.table_changes
|
|
68
|
-
where entity_id=$1
|
|
69
|
-
and entity_type=$2
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
)q where is_latest`;
|
|
73
|
-
// console.log(q, type, id);
|
|
74
|
-
|
|
75
|
-
const old = type !== 'INSERT' ? await pg.query(q, [id, table]).then(el => el.rows?.[0]?.json_object_agg || {}) : {};
|
|
76
|
-
|
|
77
|
-
const body = await getTemplate('form', tokenData?.form);
|
|
78
|
-
const schema = body?.schema || body || {};
|
|
79
|
-
const titles = Object.keys(schema)
|
|
80
|
-
.reduce((acc, curr) => Object.assign(acc, { [curr]: schema[curr].title || schema[curr].ua }), {});
|
|
81
|
-
const cls = Object.keys(schema)
|
|
82
|
-
.filter(el => schema[el]?.data)
|
|
83
|
-
.reduce((acc, curr) => Object.assign(acc, { [curr]: schema[curr].data }), {});
|
|
84
|
-
|
|
85
|
-
if (data) {
|
|
86
|
-
await metaFormat({ rows: [data], cls, sufix: false });
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const newObj = Object.fromEntries(Object.entries(data || {}).map(el => ([[titles[el[0]] || defaultTitles[el[0]] || el[0]], el[1]])));
|
|
90
|
-
const changesData = Object.keys(newObj || {}).map(el => ({
|
|
91
|
-
change_id: changeId,
|
|
92
|
-
entity_key: el,
|
|
93
|
-
value_old: getValue(old?.[el], table),
|
|
94
|
-
value_new: type === 'DELETE' ? null : getValue(newObj?.[el], table),
|
|
95
|
-
uid,
|
|
96
|
-
})).filter(el => el?.value_new !== el?.value_old);
|
|
97
|
-
|
|
98
|
-
const res = await Promise.all(changesData.map(async (el) => {
|
|
99
|
-
const insertQuery = `insert into log.table_changes_data (${Object.entries(el)?.map((key) => `"${key[0]}"`).join(',')})
|
|
100
|
-
values (${Object.entries(el)?.map((key, i) => `$${i + 1}`).join(',')}) returning *`;
|
|
101
|
-
|
|
102
|
-
const { rows = [] } = await pg.query(insertQuery, [...Object.entries(el).map((el1) => (el1[1] && typeof el1[1] === 'object' && (!Array.isArray(el1[1]) || typeof el1[1]?.[0] === 'object') ? JSON.stringify(el1[1]) : el1[1]))]) || {};
|
|
103
|
-
return rows[0];
|
|
104
|
-
}));
|
|
105
|
-
|
|
106
|
-
const newData = type === 'DELETE' ? {} : (Array.isArray(res) ? res : [res]).reduce((acc, curr) => Object.assign(acc, { [curr.entity_key]: curr.value_new }), {});
|
|
107
|
-
// console.log('logChanges OK', type);
|
|
108
|
-
return {
|
|
109
|
-
change_id: changeId, entity_type: table, entity_id: id, uid, change_type: type, old, new: newData,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
console.error('logChanges error', type, table, id, data, err.toString());
|
|
114
|
-
return {
|
|
115
|
-
error: err.toString(), entity_type: table, entity_id: id, uid, change_type: type,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
1
|
+
import getTemplate from '../../..//table/funcs/getTemplate.js';
|
|
2
|
+
import { metaFormat } from '@opengis/fastify-table/utils.js';
|
|
3
|
+
|
|
4
|
+
const defaultTitles = {
|
|
5
|
+
editor_date: 'Дата оновлення',
|
|
6
|
+
editor_id: 'Редактор',
|
|
7
|
+
сdate: 'Дата створення',
|
|
8
|
+
uid: 'Автор',
|
|
9
|
+
body: 'Зміст',
|
|
10
|
+
entity_id: 'ID Сутності',
|
|
11
|
+
entity_type: 'Таблиця сутності',
|
|
12
|
+
file_path: 'Шлях до файлу',
|
|
13
|
+
uploaded_name: 'Назва файлу',
|
|
14
|
+
size: 'Розмір файлу',
|
|
15
|
+
ext: 'Розширення файлу',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function getValue(val, tableName) {
|
|
19
|
+
if (!val) return null;
|
|
20
|
+
if (['crm.files'].includes(tableName)) {
|
|
21
|
+
return typeof val === 'object'
|
|
22
|
+
? JSON.stringify(val)
|
|
23
|
+
: val;
|
|
24
|
+
}
|
|
25
|
+
return typeof val === 'object'
|
|
26
|
+
? JSON.stringify(val)?.substring?.(0, 30)
|
|
27
|
+
: val?.toString?.()?.substring?.(0, 30);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// extract titles and cls from form schema
|
|
31
|
+
// alt: extract table template from referer -> form
|
|
32
|
+
export default async function logChanges({
|
|
33
|
+
pg, table: table1, tokenData, referer, id, data, uid = 1, type,
|
|
34
|
+
}) {
|
|
35
|
+
const table = table1.replace(/"/g, '');
|
|
36
|
+
if (!id) {
|
|
37
|
+
console.error('param id is required');
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
if (!table || !pg.pk?.[table]) {
|
|
41
|
+
console.error('table not found');
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if (!pg.pk?.['log.table_changes'] || !pg.pk?.['log.table_changes_data']) {
|
|
45
|
+
console.error('log table not found');
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
if (!type) {
|
|
49
|
+
console.error('invalid type');
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const { change_id: changeId } = await pg.query(`insert into log.table_changes(change_date,change_type,change_user_id,entity_type,entity_id)
|
|
55
|
+
values(CURRENT_DATE, $1, $2, $3, $4) returning change_id`, [type, uid, table, id]).then((res) => res.rows?.[0] || {});
|
|
56
|
+
|
|
57
|
+
const q = `select json_object_agg(entity_key, value_new) from (
|
|
58
|
+
select
|
|
59
|
+
entity_key,
|
|
60
|
+
value_new,
|
|
61
|
+
( rank() over (partition by entity_key order by cdate desc) = 1 ) as is_latest
|
|
62
|
+
from log.table_changes_data
|
|
63
|
+
|
|
64
|
+
where change_id in (
|
|
65
|
+
select
|
|
66
|
+
change_id
|
|
67
|
+
from log.table_changes
|
|
68
|
+
where entity_id=$1
|
|
69
|
+
and entity_type=$2
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
)q where is_latest`;
|
|
73
|
+
// console.log(q, type, id);
|
|
74
|
+
|
|
75
|
+
const old = type !== 'INSERT' ? await pg.query(q, [id, table]).then(el => el.rows?.[0]?.json_object_agg || {}) : {};
|
|
76
|
+
|
|
77
|
+
const body = await getTemplate('form', tokenData?.form);
|
|
78
|
+
const schema = body?.schema || body || {};
|
|
79
|
+
const titles = Object.keys(schema)
|
|
80
|
+
.reduce((acc, curr) => Object.assign(acc, { [curr]: schema[curr].title || schema[curr].ua }), {});
|
|
81
|
+
const cls = Object.keys(schema)
|
|
82
|
+
.filter(el => schema[el]?.data)
|
|
83
|
+
.reduce((acc, curr) => Object.assign(acc, { [curr]: schema[curr].data }), {});
|
|
84
|
+
|
|
85
|
+
if (data) {
|
|
86
|
+
await metaFormat({ rows: [data], cls, sufix: false });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const newObj = Object.fromEntries(Object.entries(data || {}).map(el => ([[titles[el[0]] || defaultTitles[el[0]] || el[0]], el[1]])));
|
|
90
|
+
const changesData = Object.keys(newObj || {}).map(el => ({
|
|
91
|
+
change_id: changeId,
|
|
92
|
+
entity_key: el,
|
|
93
|
+
value_old: getValue(old?.[el], table),
|
|
94
|
+
value_new: type === 'DELETE' ? null : getValue(newObj?.[el], table),
|
|
95
|
+
uid,
|
|
96
|
+
})).filter(el => el?.value_new !== el?.value_old);
|
|
97
|
+
|
|
98
|
+
const res = await Promise.all(changesData.map(async (el) => {
|
|
99
|
+
const insertQuery = `insert into log.table_changes_data (${Object.entries(el)?.map((key) => `"${key[0]}"`).join(',')})
|
|
100
|
+
values (${Object.entries(el)?.map((key, i) => `$${i + 1}`).join(',')}) returning *`;
|
|
101
|
+
|
|
102
|
+
const { rows = [] } = await pg.query(insertQuery, [...Object.entries(el).map((el1) => (el1[1] && typeof el1[1] === 'object' && (!Array.isArray(el1[1]) || typeof el1[1]?.[0] === 'object') ? JSON.stringify(el1[1]) : el1[1]))]) || {};
|
|
103
|
+
return rows[0];
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
const newData = type === 'DELETE' ? {} : (Array.isArray(res) ? res : [res]).reduce((acc, curr) => Object.assign(acc, { [curr.entity_key]: curr.value_new }), {});
|
|
107
|
+
// console.log('logChanges OK', type);
|
|
108
|
+
return {
|
|
109
|
+
change_id: changeId, entity_type: table, entity_id: id, uid, change_type: type, old, new: newData,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
console.error('logChanges error', type, table, id, data, err.toString());
|
|
114
|
+
return {
|
|
115
|
+
error: err.toString(), entity_type: table, entity_id: id, uid, change_type: type,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
// import getOpt from './funcs/getOpt.js';
|
|
2
|
-
// import setOpt from './funcs/setOpt.js';
|
|
3
|
-
|
|
4
|
-
// import isFileExists from './funcs/isFileExists.js';
|
|
5
|
-
|
|
6
|
-
// import dataUpdate from './funcs/dataUpdate.js';
|
|
7
|
-
// import dataInsert from './funcs/dataInsert.js';
|
|
8
|
-
|
|
9
|
-
// import getAccessFunc from './funcs/getAccess.js';
|
|
10
|
-
|
|
11
|
-
async function plugin(fastify) {
|
|
12
|
-
// fastify.decorate('setOpt', setOpt);
|
|
13
|
-
// fastify.decorate('getOpt', getOpt);
|
|
14
|
-
|
|
15
|
-
// fastify.decorate('dataUpdate', dataUpdate);
|
|
16
|
-
// fastify.decorate('dataInsert', dataInsert);
|
|
17
|
-
|
|
18
|
-
// fastify.decorate('getAccess', getAccessFunc);
|
|
19
|
-
|
|
20
|
-
// fastify.decorate('isFileExists', isFileExists);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default plugin;
|
|
1
|
+
// import getOpt from './funcs/getOpt.js';
|
|
2
|
+
// import setOpt from './funcs/setOpt.js';
|
|
3
|
+
|
|
4
|
+
// import isFileExists from './funcs/isFileExists.js';
|
|
5
|
+
|
|
6
|
+
// import dataUpdate from './funcs/dataUpdate.js';
|
|
7
|
+
// import dataInsert from './funcs/dataInsert.js';
|
|
8
|
+
|
|
9
|
+
// import getAccessFunc from './funcs/getAccess.js';
|
|
10
|
+
|
|
11
|
+
async function plugin(fastify) {
|
|
12
|
+
// fastify.decorate('setOpt', setOpt);
|
|
13
|
+
// fastify.decorate('getOpt', getOpt);
|
|
14
|
+
|
|
15
|
+
// fastify.decorate('dataUpdate', dataUpdate);
|
|
16
|
+
// fastify.decorate('dataInsert', dataInsert);
|
|
17
|
+
|
|
18
|
+
// fastify.decorate('getAccess', getAccessFunc);
|
|
19
|
+
|
|
20
|
+
// fastify.decorate('isFileExists', isFileExists);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default plugin;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import addHook from './funcs/addHook.js';
|
|
2
|
-
import applyHook from './funcs/applyHook.js';
|
|
3
|
-
|
|
4
|
-
async function plugin(fastify) {
|
|
5
|
-
// fastify.decorate('addHook', addHook);
|
|
6
|
-
// fastify.decorate('applyHook', applyHook);
|
|
7
|
-
}
|
|
8
|
-
export default plugin;
|
|
1
|
+
import addHook from './funcs/addHook.js';
|
|
2
|
+
import applyHook from './funcs/applyHook.js';
|
|
3
|
+
|
|
4
|
+
async function plugin(fastify) {
|
|
5
|
+
// fastify.decorate('addHook', addHook);
|
|
6
|
+
// fastify.decorate('applyHook', applyHook);
|
|
7
|
+
}
|
|
8
|
+
export default plugin;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import applyHookSync from '../hook/funcs/applyHookSync.js';
|
|
2
|
-
|
|
3
|
-
function errorStatus(error) {
|
|
4
|
-
const hook = applyHookSync('errorStatus', error);
|
|
5
|
-
if (hook) return hook;
|
|
6
|
-
|
|
7
|
-
if (error.routine === 'exec_stmt_raise' && error.file === 'pl_exec.c') {
|
|
8
|
-
return 601;
|
|
9
|
-
}
|
|
10
|
-
if (error.routine === 'ExecConstraints') {
|
|
11
|
-
return 602;
|
|
12
|
-
}
|
|
13
|
-
if (error.type === 'DatabaseError') {
|
|
14
|
-
return 600;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return 500;
|
|
18
|
-
}
|
|
19
|
-
export default errorStatus;
|
|
1
|
+
import applyHookSync from '../hook/funcs/applyHookSync.js';
|
|
2
|
+
|
|
3
|
+
function errorStatus(error) {
|
|
4
|
+
const hook = applyHookSync('errorStatus', error);
|
|
5
|
+
if (hook) return hook;
|
|
6
|
+
|
|
7
|
+
if (error.routine === 'exec_stmt_raise' && error.file === 'pl_exec.c') {
|
|
8
|
+
return 601;
|
|
9
|
+
}
|
|
10
|
+
if (error.routine === 'ExecConstraints') {
|
|
11
|
+
return 602;
|
|
12
|
+
}
|
|
13
|
+
if (error.type === 'DatabaseError') {
|
|
14
|
+
return 600;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return 500;
|
|
18
|
+
}
|
|
19
|
+
export default errorStatus;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import errorMessage from './errorMessage.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
fastify.setErrorHandler(async (error, request, reply) => {
|
|
5
|
-
// validation not error
|
|
6
|
-
if (error.validation) {
|
|
7
|
-
request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
|
|
8
|
-
return reply.status(422).send(error.toString());
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// logger
|
|
12
|
-
request.log.error(error, request);
|
|
13
|
-
console.error({ msg: error.message, where: error.where, stack: error.stack });
|
|
14
|
-
|
|
15
|
-
// errorMessage
|
|
16
|
-
const msg = errorMessage(error);
|
|
17
|
-
|
|
18
|
-
return reply.status(error.statusCode || 500).send(msg);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
export default plugin;
|
|
1
|
+
import errorMessage from './errorMessage.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
fastify.setErrorHandler(async (error, request, reply) => {
|
|
5
|
+
// validation not error
|
|
6
|
+
if (error.validation) {
|
|
7
|
+
request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
|
|
8
|
+
return reply.status(422).send(error.toString());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// logger
|
|
12
|
+
request.log.error(error, request);
|
|
13
|
+
console.error({ msg: error.message, where: error.where, stack: error.stack });
|
|
14
|
+
|
|
15
|
+
// errorMessage
|
|
16
|
+
const msg = errorMessage(error);
|
|
17
|
+
|
|
18
|
+
return reply.status(error.statusCode || 500).send(msg);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export default plugin;
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { readdirSync, existsSync } from 'node:fs';
|
|
3
|
-
|
|
4
|
-
import config from '../../../config.js';
|
|
5
|
-
import execSql from './exec.sql.js';
|
|
6
|
-
// import getCallerDir from './get.caller.dir.js';
|
|
7
|
-
|
|
8
|
-
const time = Date.now();
|
|
9
|
-
|
|
10
|
-
export default async function execMigrations(dirPath, iscore) {
|
|
11
|
-
if (
|
|
12
|
-
!dirPath
|
|
13
|
-
|| (config.migrationsCore === false && iscore)
|
|
14
|
-
|| config.migrations === false
|
|
15
|
-
) {
|
|
16
|
-
console.log('migrations skip', 'core: ', !!iscore, 'dir: ', dirPath || 'not specified');
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
console.log('migrations start', dirPath, Date.now() - time);
|
|
21
|
-
|
|
22
|
-
const exists = existsSync(dirPath);
|
|
23
|
-
|
|
24
|
-
if (exists) {
|
|
25
|
-
// get directory sql file list
|
|
26
|
-
const content = readdirSync(dirPath, { withFileTypes: true })
|
|
27
|
-
?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
|
|
28
|
-
?.map((el) => el.name) || [];
|
|
29
|
-
|
|
30
|
-
// execute sql files
|
|
31
|
-
if (content?.length) {
|
|
32
|
-
await content.reduce((promise, filename) => promise.then(() => execSql(path.join(dirPath, filename))), Promise.resolve());
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
console.log('migrations finish', dirPath, exists, Date.now() - time);
|
|
37
|
-
}
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { readdirSync, existsSync } from 'node:fs';
|
|
3
|
+
|
|
4
|
+
import config from '../../../config.js';
|
|
5
|
+
import execSql from './exec.sql.js';
|
|
6
|
+
// import getCallerDir from './get.caller.dir.js';
|
|
7
|
+
|
|
8
|
+
const time = Date.now();
|
|
9
|
+
|
|
10
|
+
export default async function execMigrations(dirPath, iscore) {
|
|
11
|
+
if (
|
|
12
|
+
!dirPath
|
|
13
|
+
|| (config.migrationsCore === false && iscore)
|
|
14
|
+
|| config.migrations === false
|
|
15
|
+
) {
|
|
16
|
+
console.log('migrations skip', 'core: ', !!iscore, 'dir: ', dirPath || 'not specified');
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
console.log('migrations start', dirPath, Date.now() - time);
|
|
21
|
+
|
|
22
|
+
const exists = existsSync(dirPath);
|
|
23
|
+
|
|
24
|
+
if (exists) {
|
|
25
|
+
// get directory sql file list
|
|
26
|
+
const content = readdirSync(dirPath, { withFileTypes: true })
|
|
27
|
+
?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
|
|
28
|
+
?.map((el) => el.name) || [];
|
|
29
|
+
|
|
30
|
+
// execute sql files
|
|
31
|
+
if (content?.length) {
|
|
32
|
+
await content.reduce((promise, filename) => promise.then(() => execSql(path.join(dirPath, filename))), Promise.resolve());
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
console.log('migrations finish', dirPath, exists, Date.now() - time);
|
|
37
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import execMigrations from './funcs/exec.migrations.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
// fastify.decorate('execMigrations', execMigrations);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export default plugin;
|
|
1
|
+
import execMigrations from './funcs/exec.migrations.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
// fastify.decorate('execMigrations', execMigrations);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default plugin;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import pg from 'pg';
|
|
2
|
-
|
|
3
|
-
import config from '../../../config.js';
|
|
4
|
-
import init from './funcs/init.js';
|
|
5
|
-
|
|
6
|
-
const pgClients = {};
|
|
7
|
-
if (config.pg) {
|
|
8
|
-
const client = new pg.Pool({
|
|
9
|
-
host: config.pg?.host || '127.0.0.1',
|
|
10
|
-
port: config.pg?.port || 5432,
|
|
11
|
-
database: config.pg?.database || 'postgres',
|
|
12
|
-
user: config.pg?.user || 'postgres',
|
|
13
|
-
password: config.pg?.password || 'postgres',
|
|
14
|
-
});
|
|
15
|
-
client.init = async () => {
|
|
16
|
-
await init(client);
|
|
17
|
-
};
|
|
18
|
-
client.init();
|
|
19
|
-
pgClients.client = client;
|
|
20
|
-
}
|
|
21
|
-
export default pgClients;
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
|
|
3
|
+
import config from '../../../config.js';
|
|
4
|
+
import init from './funcs/init.js';
|
|
5
|
+
|
|
6
|
+
const pgClients = {};
|
|
7
|
+
if (config.pg) {
|
|
8
|
+
const client = new pg.Pool({
|
|
9
|
+
host: config.pg?.host || '127.0.0.1',
|
|
10
|
+
port: config.pg?.port || 5432,
|
|
11
|
+
database: config.pg?.database || 'postgres',
|
|
12
|
+
user: config.pg?.user || 'postgres',
|
|
13
|
+
password: config.pg?.password || 'postgres',
|
|
14
|
+
});
|
|
15
|
+
client.init = async () => {
|
|
16
|
+
await init(client);
|
|
17
|
+
};
|
|
18
|
+
client.init();
|
|
19
|
+
pgClients.client = client;
|
|
20
|
+
}
|
|
21
|
+
export default pgClients;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import checkPolicy from './funcs/checkPolicy.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
fastify.addHook('preParsing', async (request, reply) => {
|
|
5
|
-
const resp = checkPolicy(request, reply);
|
|
6
|
-
if (resp) {
|
|
7
|
-
return resp;
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default plugin;
|
|
1
|
+
import checkPolicy from './funcs/checkPolicy.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
fastify.addHook('preParsing', async (request, reply) => {
|
|
5
|
+
const resp = checkPolicy(request, reply);
|
|
6
|
+
if (resp) {
|
|
7
|
+
return resp;
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default plugin;
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
const sqlInjection = [
|
|
2
|
-
'()',
|
|
3
|
-
'^',
|
|
4
|
-
'*',
|
|
5
|
-
'like ',
|
|
6
|
-
'@variable',
|
|
7
|
-
'@@variable',
|
|
8
|
-
'group by ',
|
|
9
|
-
'union ',
|
|
10
|
-
'select ',
|
|
11
|
-
'having ',
|
|
12
|
-
'as injectx',
|
|
13
|
-
'where ',
|
|
14
|
-
'rlike ',
|
|
15
|
-
'if(',
|
|
16
|
-
'sleep(',
|
|
17
|
-
'waitfor delay',
|
|
18
|
-
'benchmark(',
|
|
19
|
-
'pg_sleep(',
|
|
20
|
-
"'\\\"",
|
|
21
|
-
'randomblob(',
|
|
22
|
-
'order by ',
|
|
23
|
-
'union all ',
|
|
24
|
-
'+or',
|
|
25
|
-
'or ',
|
|
26
|
-
'and ',
|
|
27
|
-
"'' ",
|
|
28
|
-
'""" ',
|
|
29
|
-
'<script',
|
|
30
|
-
'javascript:',
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
export default sqlInjection;
|
|
1
|
+
const sqlInjection = [
|
|
2
|
+
'()',
|
|
3
|
+
'^',
|
|
4
|
+
'*',
|
|
5
|
+
'like ',
|
|
6
|
+
'@variable',
|
|
7
|
+
'@@variable',
|
|
8
|
+
'group by ',
|
|
9
|
+
'union ',
|
|
10
|
+
'select ',
|
|
11
|
+
'having ',
|
|
12
|
+
'as injectx',
|
|
13
|
+
'where ',
|
|
14
|
+
'rlike ',
|
|
15
|
+
'if(',
|
|
16
|
+
'sleep(',
|
|
17
|
+
'waitfor delay',
|
|
18
|
+
'benchmark(',
|
|
19
|
+
'pg_sleep(',
|
|
20
|
+
"'\\\"",
|
|
21
|
+
'randomblob(',
|
|
22
|
+
'order by ',
|
|
23
|
+
'union all ',
|
|
24
|
+
'+or',
|
|
25
|
+
'or ',
|
|
26
|
+
'and ',
|
|
27
|
+
"'' ",
|
|
28
|
+
'""" ',
|
|
29
|
+
'<script',
|
|
30
|
+
'javascript:',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
export default sqlInjection;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import redisClients from './funcs/redisClients.js';
|
|
2
|
-
import getRedis from './funcs/getRedis.js';
|
|
3
|
-
|
|
4
|
-
if (!redisClients[0]) {
|
|
5
|
-
getRedis({ db: 0 });
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default redisClients[0];
|
|
1
|
+
import redisClients from './funcs/redisClients.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
|
|
4
|
+
if (!redisClients[0]) {
|
|
5
|
+
getRedis({ db: 0 });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default redisClients[0];
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const redisClients = {};
|
|
2
|
-
|
|
3
|
-
export default redisClients;
|
|
1
|
+
const redisClients = {};
|
|
2
|
+
|
|
3
|
+
export default redisClients;
|