@opengis/fastify-table 1.1.60 → 1.1.62

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.
Files changed (74) hide show
  1. package/index.js +109 -103
  2. package/package.json +46 -46
  3. package/server/migrations/0.sql +80 -80
  4. package/server/migrations/cls.sql +39 -39
  5. package/server/migrations/properties.sql +144 -144
  6. package/server/migrations/roles.sql +175 -175
  7. package/server/migrations/users.sql +168 -168
  8. package/server/plugins/cron/funcs/addCron.js +128 -128
  9. package/server/plugins/cron/index.js +6 -6
  10. package/server/plugins/crud/funcs/dataDelete.js +21 -21
  11. package/server/plugins/crud/funcs/dataInsert.js +33 -33
  12. package/server/plugins/crud/funcs/dataUpdate.js +50 -50
  13. package/server/plugins/crud/funcs/getAccess.js +48 -48
  14. package/server/plugins/crud/funcs/getOpt.js +13 -12
  15. package/server/plugins/crud/funcs/getToken.js +4 -2
  16. package/server/plugins/crud/funcs/setOpt.js +21 -19
  17. package/server/plugins/crud/funcs/setToken.js +44 -56
  18. package/server/plugins/crud/funcs/utils/getFolder.js +10 -10
  19. package/server/plugins/crud/funcs/utils/logChanges.js +62 -62
  20. package/server/plugins/crud/index.js +23 -23
  21. package/server/plugins/hook/index.js +8 -8
  22. package/server/plugins/migration/funcs/exec.migrations.js +79 -79
  23. package/server/plugins/migration/index.js +7 -7
  24. package/server/plugins/pg/funcs/getMeta.js +28 -28
  25. package/server/plugins/pg/funcs/getPG.js +30 -30
  26. package/server/plugins/pg/pgClients.js +21 -21
  27. package/server/plugins/policy/funcs/checkPolicy.js +92 -92
  28. package/server/plugins/policy/index.js +12 -12
  29. package/server/plugins/policy/sqlInjection.js +33 -33
  30. package/server/plugins/redis/client.js +8 -8
  31. package/server/plugins/redis/funcs/redisClients.js +3 -3
  32. package/server/plugins/redis/index.js +15 -15
  33. package/server/plugins/table/funcs/addTemplateDir.js +8 -8
  34. package/server/plugins/table/funcs/getFilterSQL/index.js +96 -96
  35. package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +170 -170
  36. package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  37. package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +66 -66
  38. package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  39. package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  40. package/server/plugins/table/funcs/getSelect.js +19 -19
  41. package/server/plugins/table/funcs/getSelectMeta.js +57 -57
  42. package/server/plugins/table/funcs/getTemplates.js +19 -19
  43. package/server/plugins/table/funcs/gisIRColumn.js +72 -69
  44. package/server/plugins/table/funcs/loadTemplate.js +1 -1
  45. package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
  46. package/server/plugins/table/funcs/metaFormat/getSelectVal.js +23 -23
  47. package/server/plugins/table/funcs/metaFormat/index.js +30 -30
  48. package/server/plugins/table/funcs/userTemplateDir.js +1 -1
  49. package/server/plugins/table/index.js +13 -13
  50. package/server/plugins/util/funcs/eventStream.js +28 -28
  51. package/server/plugins/util/index.js +7 -7
  52. package/server/routes/cron/index.js +14 -14
  53. package/server/routes/crud/controllers/deleteCrud.js +36 -36
  54. package/server/routes/crud/controllers/insert.js +73 -73
  55. package/server/routes/{table → crud}/controllers/table.js +87 -81
  56. package/server/routes/crud/controllers/update.js +76 -76
  57. package/server/routes/crud/index.js +21 -19
  58. package/server/routes/logger/controllers/logger.file.js +92 -92
  59. package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
  60. package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
  61. package/server/routes/logger/index.js +17 -17
  62. package/server/routes/properties/controllers/properties.add.js +55 -55
  63. package/server/routes/properties/controllers/properties.get.js +17 -17
  64. package/server/routes/properties/index.js +16 -16
  65. package/server/routes/table/controllers/data.js +142 -153
  66. package/server/routes/table/controllers/filter.js +60 -60
  67. package/server/routes/table/controllers/form.js +42 -42
  68. package/server/routes/table/controllers/search.js +74 -74
  69. package/server/routes/table/controllers/suggest.js +84 -84
  70. package/server/routes/table/index.js +28 -28
  71. package/server/routes/table/schema.js +64 -64
  72. package/server/routes/util/controllers/status.monitor.js +8 -8
  73. package/server/routes/util/index.js +11 -11
  74. package/utils.js +120 -119
@@ -1,73 +1,73 @@
1
- import {
2
- applyHook, getAccess, getTemplate, checkXSS, dataInsert, getToken, config,
3
- } from '../../../../utils.js';
4
-
5
- export default async function insert(req) {
6
- const {
7
- user, params = {}, body = {},
8
- } = req || {};
9
- const hookData = await applyHook('preInsert', { table: params?.table, user });
10
- if (hookData?.message && hookData?.status) {
11
- return { message: hookData?.message, status: hookData?.status };
12
- }
13
- const tokenData = await getToken({
14
- uid: user.uid, token: params.table, mode: 'a', json: 1,
15
- });
16
-
17
- const { form, table: add } = hookData || tokenData || (config.auth?.disable ? req.params : {});
18
-
19
- const { actions = [] } = await getAccess({ table: add, user }) || {};
20
-
21
- if (!actions.includes('add') && !config?.local) {
22
- return { message: 'access restricted', status: 403 };
23
- }
24
-
25
- if (!add) {
26
- return { message: 'table is required', status: 400 };
27
- }
28
-
29
- const loadTemplate = await getTemplate('table', add);
30
- const { table } = loadTemplate || hookData || req.params || {};
31
- if (!table) {
32
- return { message: 'table not found', status: 404 };
33
- }
34
-
35
- const formData = form || loadTemplate?.form ? (await getTemplate('form', form || loadTemplate?.form) || {}) : {};
36
-
37
- const xssCheck = checkXSS({ body, schema: formData?.schema || formData });
38
-
39
- if (xssCheck.error && formData?.xssCheck !== false) {
40
- req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
41
- return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
42
- }
43
-
44
- const uid = user?.uid;
45
- if (![add, table].filter((el) => el !== 'admin.users')?.length) {
46
- Object.assign(body, { uid, editor_id: uid });
47
- }
48
- const res = await dataInsert({
49
- table: loadTemplate?.table, data: body, uid,
50
- });
51
-
52
- // admin.custom_column
53
- await applyHook('afterInsert', {
54
- table, body, payload: res, user,
55
- });
56
- // form DataTable
57
- const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && body[key].length);
58
- if (extraKeys?.length) {
59
- res.extra = {};
60
- await Promise.all(extraKeys?.map(async (key) => {
61
- const objId = body[formData[key].parent_id] || req.body?.id;
62
- const extraRows = await Promise.all(body[key].map(async (row) => {
63
- const extraRes = await dataInsert({
64
- table: formData[key].table, data: { ...row, [formData[key].parent_id]: objId }, uid,
65
- });
66
- return extraRes?.rows?.[0];
67
- }));
68
- Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
69
- }));
70
- }
71
-
72
- return { rows: res.rows, extra: res.extra };
73
- }
1
+ import {
2
+ applyHook, getAccess, getTemplate, checkXSS, dataInsert, getToken, config,
3
+ } from '../../../../utils.js';
4
+
5
+ export default async function insert(req) {
6
+ const {
7
+ user, params = {}, body = {},
8
+ } = req || {};
9
+ const hookData = await applyHook('preInsert', { table: params?.table, user });
10
+ if (hookData?.message && hookData?.status) {
11
+ return { message: hookData?.message, status: hookData?.status };
12
+ }
13
+ const tokenData = await getToken({
14
+ uid: user.uid, token: params.table, mode: 'a', json: 1,
15
+ });
16
+
17
+ const { form, table: add } = hookData || tokenData || (config.auth?.disable ? req.params : {});
18
+
19
+ const { actions = [] } = await getAccess({ table: add, user }) || {};
20
+
21
+ if (!actions.includes('add') && !config?.local) {
22
+ return { message: 'access restricted', status: 403 };
23
+ }
24
+
25
+ if (!add) {
26
+ return { message: 'table is required', status: 400 };
27
+ }
28
+
29
+ const loadTemplate = await getTemplate('table', add);
30
+ const { table } = loadTemplate || hookData || req.params || {};
31
+ if (!table) {
32
+ return { message: 'table not found', status: 404 };
33
+ }
34
+
35
+ const formData = form || loadTemplate?.form ? (await getTemplate('form', form || loadTemplate?.form) || {}) : {};
36
+
37
+ const xssCheck = checkXSS({ body, schema: formData?.schema || formData });
38
+
39
+ if (xssCheck.error && formData?.xssCheck !== false) {
40
+ req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
41
+ return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
42
+ }
43
+
44
+ const uid = user?.uid;
45
+ if (![add, table].filter((el) => el !== 'admin.users')?.length) {
46
+ Object.assign(body, { uid, editor_id: uid });
47
+ }
48
+ const res = await dataInsert({
49
+ table: loadTemplate?.table, data: body, uid,
50
+ });
51
+
52
+ // admin.custom_column
53
+ await applyHook('afterInsert', {
54
+ table, body, payload: res, user,
55
+ });
56
+ // form DataTable
57
+ const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && body[key].length);
58
+ if (extraKeys?.length) {
59
+ res.extra = {};
60
+ await Promise.all(extraKeys?.map(async (key) => {
61
+ const objId = body[formData[key].parent_id] || req.body?.id;
62
+ const extraRows = await Promise.all(body[key].map(async (row) => {
63
+ const extraRes = await dataInsert({
64
+ table: formData[key].table, data: { ...row, [formData[key].parent_id]: objId }, uid,
65
+ });
66
+ return extraRes?.rows?.[0];
67
+ }));
68
+ Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
69
+ }));
70
+ }
71
+
72
+ return { rows: res.rows, extra: res.extra };
73
+ }
@@ -1,81 +1,87 @@
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, 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 (!tableName && !id) {
32
+ return { message: 'not enough params', status: 400 };
33
+ }
34
+
35
+ const { actions = [], query: accessQuery } = await getAccess({
36
+ table: tableName,
37
+ id,
38
+ user,
39
+ }) || {};
40
+
41
+ if (!actions.includes('edit') && !config?.local) {
42
+ return { message: 'access restricted', status: 403 };
43
+ }
44
+
45
+ const { pk, columns: dbColumns = [] } = await getMeta(tableName);
46
+ if (!pk) return { message: `table not found: ${table}`, status: 404 };
47
+
48
+ // const cols = columns.map((el) => el.name || el).join(',');
49
+ const schema = await getTemplate('form', hookData?.form || form) || {};
50
+ // skip DataTable from another table
51
+ const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
52
+ // skip non-existing columns
53
+ const columnList = dbColumns.map((el) => el.name || el).join(',');
54
+
55
+ const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${tableName} limit 0`) : {};
56
+ const cols = loadTable?.table
57
+ ? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.map((col) => (col?.includes('geom') ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
58
+ : fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
59
+ const where = [`"${pk}" = $1`, loadTable.query, accessQuery].filter((el) => el);
60
+ const geom = columnList.includes('geom') ? ',st_asgeojson(geom)::json as geom' : '';
61
+ const q = `select "${pk}" as id, ${cols || '*'} ${geom} from ${tableName} t where ${where.join(' and ') || 'true'} limit 1`;
62
+
63
+ if (query?.sql === '1') return q;
64
+
65
+ const data = await pg.query(q, [id]).then(el => el.rows[0]);
66
+ if (!data) return { message: 'not found', status: 404 };
67
+
68
+ if (extraKeys?.length) {
69
+ await Promise.all(extraKeys?.map(async (key) => {
70
+ const { colModel, table: extraTable, parent_id: parentId } = schema[key];
71
+ 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]);
72
+ Object.assign(data, { [key]: extraRows });
73
+ }));
74
+ }
75
+ if (user.uid) {
76
+ const [token] = setToken({
77
+ ids: [JSON.stringify({ id, table, form: loadTable.form })],
78
+ uid: user.uid,
79
+ array: 1,
80
+ });
81
+ data.token = token;
82
+ }
83
+ const res = await applyHook('afterTable', {
84
+ table: tableName, payload: [data], user,
85
+ });
86
+ return res || data || {};
87
+ }
@@ -1,76 +1,76 @@
1
- import {
2
- pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate, logger, getToken,
3
- } from '../../../../utils.js';
4
- import config from '../../../../config.js';
5
-
6
- export default async function update(req) {
7
- const { user, params = {}, body = {} } = req;
8
- const hookData = await applyHook('preUpdate', {
9
- table: params?.table, id: params?.id, user,
10
- });
11
-
12
- if (hookData?.message && hookData?.status) {
13
- return { message: hookData?.message, status: hookData?.status };
14
- }
15
- const tokenData = await getToken({
16
- uid: user.uid, token: body.token || params.table, mode: 'w', json: 1,
17
- });
18
-
19
- const { form, table: edit, id } = hookData || tokenData || (config.auth?.disable ? params : {});
20
-
21
- const { actions = [] } = await getAccess({ table: edit, id, user }) || {};
22
-
23
- if (!actions.includes('edit') && !config?.local) {
24
- return { message: 'access restricted', status: 403 };
25
- }
26
-
27
- if (!edit) {
28
- return { message: 'table is required', status: 400 };
29
- }
30
-
31
- if (!id) {
32
- return { message: 'id is required', status: 404 };
33
- }
34
-
35
- const loadTemplate = await getTemplate('table', edit);
36
- const { table } = loadTemplate || hookData || params || {};
37
-
38
- const uid = user?.uid;
39
-
40
- const formData = form || loadTemplate?.form ? await getTemplate('form', form || loadTemplate?.form) : {};
41
-
42
- const xssCheck = checkXSS({ body, schema: formData?.schema || formData });
43
-
44
- if (xssCheck.error && formData?.xssCheck !== false) {
45
- logger.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
46
- return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
47
- }
48
-
49
- const res = await dataUpdate({
50
- table: loadTemplate?.table || table, id, data: body, uid,
51
- });
52
-
53
- // admin.custom_column
54
- await applyHook('afterUpdate', {
55
- table: params?.table, body, payload: res, user,
56
- });
57
-
58
- // form DataTable
59
- const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && body[key].length);
60
- if (extraKeys?.length) {
61
- res.extra = {};
62
- await Promise.all(extraKeys?.map(async (key) => {
63
- const objId = body[formData[key].parent_id] || body?.id;
64
- // delete old extra data
65
- await pgClients.client.query(`delete from ${formData[key].table} where ${formData[key].parent_id}=$1`, [objId]); // rewrite?
66
- // insert new extra data
67
- const extraRows = await Promise.all(body[key].map(async (row) => {
68
- const extraRes = await dataInsert({ table: formData[key].table, data: { ...row, [formData[key].parent_id]: objId }, uid });
69
- return extraRes?.rows?.[0];
70
- }));
71
- Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
72
- }));
73
- }
74
-
75
- return res;
76
- }
1
+ import {
2
+ pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate, logger, getToken,
3
+ } from '../../../../utils.js';
4
+ import config from '../../../../config.js';
5
+
6
+ export default async function update(req) {
7
+ const { user, params = {}, body = {} } = req;
8
+ const hookData = await applyHook('preUpdate', {
9
+ table: params?.table, id: params?.id, user,
10
+ });
11
+
12
+ if (hookData?.message && hookData?.status) {
13
+ return { message: hookData?.message, status: hookData?.status };
14
+ }
15
+ const tokenData = await getToken({
16
+ uid: user.uid, token: body.token || params.table, mode: 'w', json: 1,
17
+ });
18
+
19
+ const { form, table: edit, id } = hookData || tokenData || (config.auth?.disable ? params : {});
20
+
21
+ const { actions = [] } = await getAccess({ table: edit, id, user }) || {};
22
+
23
+ if (!actions.includes('edit') && !config?.local) {
24
+ return { message: 'access restricted', status: 403 };
25
+ }
26
+
27
+ if (!edit) {
28
+ return { message: 'table is required', status: 400 };
29
+ }
30
+
31
+ if (!id) {
32
+ return { message: 'id is required', status: 404 };
33
+ }
34
+
35
+ const loadTemplate = await getTemplate('table', edit);
36
+ const { table } = loadTemplate || hookData || params || {};
37
+
38
+ const uid = user?.uid;
39
+
40
+ const formData = form || loadTemplate?.form ? await getTemplate('form', form || loadTemplate?.form) : {};
41
+
42
+ const xssCheck = checkXSS({ body, schema: formData?.schema || formData });
43
+
44
+ if (xssCheck.error && formData?.xssCheck !== false) {
45
+ logger.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
46
+ return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
47
+ }
48
+
49
+ const res = await dataUpdate({
50
+ table: loadTemplate?.table || table, id, data: body, uid,
51
+ });
52
+
53
+ // admin.custom_column
54
+ await applyHook('afterUpdate', {
55
+ table: params?.table, body, payload: res, user,
56
+ });
57
+
58
+ // form DataTable
59
+ const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && body[key].length);
60
+ if (extraKeys?.length) {
61
+ res.extra = {};
62
+ await Promise.all(extraKeys?.map(async (key) => {
63
+ const objId = body[formData[key].parent_id] || body?.id;
64
+ // delete old extra data
65
+ await pgClients.client.query(`delete from ${formData[key].table} where ${formData[key].parent_id}=$1`, [objId]); // rewrite?
66
+ // insert new extra data
67
+ const extraRows = await Promise.all(body[key].map(async (row) => {
68
+ const extraRes = await dataInsert({ table: formData[key].table, data: { ...row, [formData[key].parent_id]: objId }, uid });
69
+ return extraRes?.rows?.[0];
70
+ }));
71
+ Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
72
+ }));
73
+ }
74
+
75
+ return res;
76
+ }
@@ -1,19 +1,21 @@
1
- import update from './controllers/update.js';
2
- import insert from './controllers/insert.js';
3
- import deleteCrud from './controllers/deleteCrud.js';
4
-
5
- const tableSchema = {
6
- params: {
7
- id: { type: 'string', pattern: '^([\\d\\w]+)$' },
8
- table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
9
- },
10
- };
11
-
12
- async function plugin(fastify, config = {}) {
13
- const prefix = config.prefix || '/api';
14
- fastify.put(`${prefix}/table/:table/:id?`, { schema: tableSchema }, update);
15
- fastify.delete(`${prefix}/table/:table/:id?`, { schema: tableSchema }, deleteCrud);
16
- fastify.post(`${prefix}/table/:table?`, { schema: tableSchema }, insert);
17
- }
18
-
19
- export default plugin;
1
+ import update from './controllers/update.js';
2
+ import insert from './controllers/insert.js';
3
+ import deleteCrud from './controllers/deleteCrud.js';
4
+ import table from './controllers/table.js';
5
+
6
+ const tableSchema = {
7
+ params: {
8
+ id: { type: 'string', pattern: '^([\\d\\w]+)$' },
9
+ table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
10
+ },
11
+ };
12
+
13
+ async function plugin(fastify, config = {}) {
14
+ const prefix = config.prefix || '/api';
15
+ fastify.put(`${prefix}/table/:table/:id?`, { schema: tableSchema }, update);
16
+ fastify.delete(`${prefix}/table/:table/:id?`, { schema: tableSchema }, deleteCrud);
17
+ fastify.post(`${prefix}/table/:table?`, { schema: tableSchema }, insert);
18
+ fastify.get(`${prefix}/table/:table/:id?`, { schema: tableSchema }, table);
19
+ }
20
+
21
+ export default plugin;