@opengis/fastify-table 1.1.78 → 1.1.79
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 +76 -76
- package/package.json +45 -45
- package/server/migrations/0.sql +80 -80
- package/server/migrations/cls.sql +39 -39
- package/server/migrations/context.sql +95 -0
- package/server/migrations/properties.sql +144 -144
- package/server/migrations/roles.sql +179 -175
- package/server/migrations/users.sql +170 -170
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +21 -21
- package/server/plugins/crud/funcs/dataInsert.js +38 -38
- 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 +13 -13
- package/server/plugins/crud/funcs/setOpt.js +21 -21
- package/server/plugins/crud/funcs/setToken.js +44 -44
- package/server/plugins/crud/funcs/utils/getFolder.js +10 -10
- package/server/plugins/crud/funcs/utils/logChanges.js +62 -62
- package/server/plugins/crud/index.js +23 -23
- package/server/plugins/hook/index.js +8 -8
- package/server/plugins/logger/errorStatus.js +19 -19
- package/server/plugins/logger/index.js +21 -21
- package/server/plugins/migration/funcs/exec.migrations.js +79 -79
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/funcs/getMeta.js +28 -28
- package/server/plugins/pg/funcs/getPG.js +33 -33
- package/server/plugins/pg/pgClients.js +21 -21
- package/server/plugins/policy/funcs/checkPolicy.js +92 -92
- package/server/plugins/policy/funcs/checkXSS.js +1 -1
- package/server/plugins/policy/index.js +12 -12
- package/server/plugins/policy/sqlInjection.js +33 -33
- package/server/plugins/redis/client.js +8 -8
- package/server/plugins/redis/funcs/redisClients.js +3 -3
- package/server/plugins/redis/index.js +17 -17
- package/server/plugins/table/funcs/addTemplateDir.js +8 -8
- package/server/plugins/table/funcs/getFilterSQL/index.js +96 -96
- package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +170 -170
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +66 -66
- package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/plugins/table/funcs/getSelect.js +19 -19
- package/server/plugins/table/funcs/getSelectMeta.js +60 -60
- package/server/plugins/table/funcs/getTemplates.js +19 -19
- package/server/plugins/table/funcs/gisIRColumn.js +72 -72
- 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 +38 -22
- package/server/plugins/table/funcs/metaFormat/index.js +31 -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 +80 -80
- package/server/routes/crud/controllers/table.js +91 -91
- package/server/routes/crud/controllers/update.js +82 -82
- package/server/routes/crud/index.js +21 -21
- package/server/routes/logger/controllers/logger.file.js +92 -92
- package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
- package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
- package/server/routes/logger/index.js +17 -17
- package/server/routes/properties/controllers/properties.add.js +55 -55
- package/server/routes/properties/controllers/properties.get.js +17 -17
- package/server/routes/properties/index.js +16 -16
- package/server/routes/table/controllers/data.js +144 -144
- package/server/routes/table/controllers/filter.js +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/index.js +28 -28
- package/server/routes/table/schema.js +64 -64
- package/server/routes/util/controllers/status.monitor.js +8 -8
- package/server/routes/util/index.js +11 -11
- package/utils.js +122 -122
|
@@ -1,80 +1,80 @@
|
|
|
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
|
-
if (!user) return { message: 'access restricted', status: 403 };
|
|
10
|
-
const hookData = await applyHook('preInsert', { table: params?.table, user });
|
|
11
|
-
if (hookData?.message && hookData?.status) {
|
|
12
|
-
return { message: hookData?.message, status: hookData?.status };
|
|
13
|
-
}
|
|
14
|
-
const tokenData = await getToken({
|
|
15
|
-
uid: user?.uid, token: params.table, mode: 'a', json: 1,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const { form, table: add } = hookData || tokenData || (config.auth?.disable ? req.params : {});
|
|
19
|
-
|
|
20
|
-
const { actions = [] } = await getAccess({ table: add, user }) || {};
|
|
21
|
-
|
|
22
|
-
if (!actions.includes('add') && !config?.local && !tokenData) {
|
|
23
|
-
return { message: 'access restricted', status: 403 };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (!add) {
|
|
27
|
-
return { message: 'table is required', status: 400 };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const loadTemplate = await getTemplate('table', add);
|
|
31
|
-
const { table } = loadTemplate || hookData || tokenData || req.params || {};
|
|
32
|
-
if (!table) {
|
|
33
|
-
return { message: 'table not found', status: 404 };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const formData = form || loadTemplate?.form ? (await getTemplate('form', form || loadTemplate?.form) || {}) : {};
|
|
37
|
-
const schema = formData?.schema || formData;
|
|
38
|
-
|
|
39
|
-
const xssCheck = checkXSS({ body, schema });
|
|
40
|
-
|
|
41
|
-
if (xssCheck.error && formData?.xssCheck !== false) {
|
|
42
|
-
req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
|
|
43
|
-
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (![add, table].includes('admin.users')) {
|
|
47
|
-
Object.assign(body, { uid: user?.uid, editor_id: user?.uid });
|
|
48
|
-
}
|
|
49
|
-
if (tokenData?.obj) {
|
|
50
|
-
const objData = tokenData.obj?.split('#').reduce((p, el) => ({ ...p, [el.split('=')[0]]: el.split('=')[1] }), {}) || {};
|
|
51
|
-
Object.assign(body, objData);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const res = await dataInsert({
|
|
55
|
-
table: loadTemplate?.table || table, data: body, uid: user?.uid,
|
|
56
|
-
});
|
|
57
|
-
if (!res) return { message: 'nothing added ' };
|
|
58
|
-
|
|
59
|
-
// admin.custom_column
|
|
60
|
-
await applyHook('afterInsert', {
|
|
61
|
-
table, body, payload: res, user,
|
|
62
|
-
});
|
|
63
|
-
// form DataTable
|
|
64
|
-
const extraKeys = Object.keys(schema || {})?.filter((key) => schema?.[key]?.type === 'DataTable' && schema?.[key]?.table && schema?.[key]?.parent_id && body[key].length);
|
|
65
|
-
if (extraKeys?.length) {
|
|
66
|
-
res.extra = {};
|
|
67
|
-
await Promise.all(extraKeys?.map(async (key) => {
|
|
68
|
-
const objId = body[schema[key].parent_id] || req.body?.id || res?.rows?.[0]?.[schema[key].parent_id];
|
|
69
|
-
const extraRows = await Promise.all(body[key].map(async (row) => {
|
|
70
|
-
const extraRes = await dataInsert({
|
|
71
|
-
table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid: user?.uid,
|
|
72
|
-
});
|
|
73
|
-
return extraRes?.rows?.[0];
|
|
74
|
-
}));
|
|
75
|
-
Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return { rows: res.rows, extra: res.extra };
|
|
80
|
-
}
|
|
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
|
+
if (!user) return { message: 'access restricted', status: 403 };
|
|
10
|
+
const hookData = await applyHook('preInsert', { table: params?.table, user });
|
|
11
|
+
if (hookData?.message && hookData?.status) {
|
|
12
|
+
return { message: hookData?.message, status: hookData?.status };
|
|
13
|
+
}
|
|
14
|
+
const tokenData = await getToken({
|
|
15
|
+
uid: user?.uid, token: params.table, mode: 'a', json: 1,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const { form, table: add } = hookData || tokenData || (config.auth?.disable ? req.params : {});
|
|
19
|
+
|
|
20
|
+
const { actions = [] } = await getAccess({ table: add, user }) || {};
|
|
21
|
+
|
|
22
|
+
if (!actions.includes('add') && !config?.local && !tokenData) {
|
|
23
|
+
return { message: 'access restricted', status: 403 };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!add) {
|
|
27
|
+
return { message: 'table is required', status: 400 };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const loadTemplate = await getTemplate('table', add);
|
|
31
|
+
const { table } = loadTemplate || hookData || tokenData || req.params || {};
|
|
32
|
+
if (!table) {
|
|
33
|
+
return { message: 'table not found', status: 404 };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const formData = form || loadTemplate?.form ? (await getTemplate('form', form || loadTemplate?.form) || {}) : {};
|
|
37
|
+
const schema = formData?.schema || formData;
|
|
38
|
+
|
|
39
|
+
const xssCheck = checkXSS({ body, schema });
|
|
40
|
+
|
|
41
|
+
if (xssCheck.error && formData?.xssCheck !== false) {
|
|
42
|
+
req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
|
|
43
|
+
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (![add, table].includes('admin.users')) {
|
|
47
|
+
Object.assign(body, { uid: user?.uid, editor_id: user?.uid });
|
|
48
|
+
}
|
|
49
|
+
if (tokenData?.obj) {
|
|
50
|
+
const objData = tokenData.obj?.split('#').reduce((p, el) => ({ ...p, [el.split('=')[0]]: el.split('=')[1] }), {}) || {};
|
|
51
|
+
Object.assign(body, objData);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const res = await dataInsert({
|
|
55
|
+
table: loadTemplate?.table || table, data: body, uid: user?.uid,
|
|
56
|
+
});
|
|
57
|
+
if (!res) return { message: 'nothing added ' };
|
|
58
|
+
|
|
59
|
+
// admin.custom_column
|
|
60
|
+
await applyHook('afterInsert', {
|
|
61
|
+
table, body, payload: res, user,
|
|
62
|
+
});
|
|
63
|
+
// form DataTable
|
|
64
|
+
const extraKeys = Object.keys(schema || {})?.filter((key) => schema?.[key]?.type === 'DataTable' && schema?.[key]?.table && schema?.[key]?.parent_id && body[key].length);
|
|
65
|
+
if (extraKeys?.length) {
|
|
66
|
+
res.extra = {};
|
|
67
|
+
await Promise.all(extraKeys?.map(async (key) => {
|
|
68
|
+
const objId = body[schema[key].parent_id] || req.body?.id || res?.rows?.[0]?.[schema[key].parent_id];
|
|
69
|
+
const extraRows = await Promise.all(body[key].map(async (row) => {
|
|
70
|
+
const extraRes = await dataInsert({
|
|
71
|
+
table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid: user?.uid,
|
|
72
|
+
});
|
|
73
|
+
return extraRes?.rows?.[0];
|
|
74
|
+
}));
|
|
75
|
+
Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { rows: res.rows, extra: res.extra };
|
|
80
|
+
}
|
|
@@ -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: tableName,
|
|
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') ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
|
|
61
|
-
: fields.map((el) => (el?.name?.includes('geom') ? `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 = columnList.includes('geom') ? ',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) {
|
|
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: tableName,
|
|
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') ? `st_asgeojson(${col})::json as "${col}"` : `"${col}"`))?.join(',')
|
|
61
|
+
: fields.map((el) => (el?.name?.includes('geom') ? `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 = columnList.includes('geom') ? ',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) {
|
|
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,82 +1,82 @@
|
|
|
1
|
-
import {
|
|
2
|
-
pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate, logger, getToken,
|
|
3
|
-
} from '../../../../utils.js';
|
|
4
|
-
import config from '../../../../config.js';
|
|
5
|
-
import insert from './insert.js';
|
|
6
|
-
|
|
7
|
-
export default async function update(req) {
|
|
8
|
-
const { user, params = {}, body = {} } = req;
|
|
9
|
-
if (!user) return { message: 'access restricted', status: 403 };
|
|
10
|
-
const hookData = await applyHook('preUpdate', {
|
|
11
|
-
table: params?.table, id: params?.id, user,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
if (hookData?.message && hookData?.status) {
|
|
15
|
-
return { message: hookData?.message, status: hookData?.status };
|
|
16
|
-
}
|
|
17
|
-
const tokenData = await getToken({
|
|
18
|
-
uid: user.uid, token: body.token || params.table, mode: 'w', json: 1,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const { form, table: edit, id } = hookData || tokenData || (config.auth?.disable ? params : {});
|
|
22
|
-
|
|
23
|
-
const { actions = [] } = await getAccess({ table: edit, id, user }) || {};
|
|
24
|
-
|
|
25
|
-
if (!actions.includes('edit') && !config?.local && !tokenData) {
|
|
26
|
-
return { message: 'access restricted', status: 403 };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!edit) {
|
|
30
|
-
return { message: 'table is required', status: 400 };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (!id && tokenData?.table) {
|
|
34
|
-
return insert(req);
|
|
35
|
-
}
|
|
36
|
-
if (!id) {
|
|
37
|
-
return { message: 'id is required', status: 404 };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const loadTemplate = await getTemplate('table', edit);
|
|
41
|
-
const { table } = loadTemplate || hookData || tokenData || params || {};
|
|
42
|
-
|
|
43
|
-
const uid = user?.uid;
|
|
44
|
-
|
|
45
|
-
const formData = form || loadTemplate?.form ? await getTemplate('form', form || loadTemplate?.form) : {};
|
|
46
|
-
const schema = formData?.schema || formData;
|
|
47
|
-
|
|
48
|
-
const xssCheck = checkXSS({ body, schema });
|
|
49
|
-
|
|
50
|
-
if (xssCheck.error && formData?.xssCheck !== false) {
|
|
51
|
-
logger.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
|
|
52
|
-
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const res = await dataUpdate({
|
|
56
|
-
table: loadTemplate?.table || table, id, data: body, uid,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// admin.custom_column
|
|
60
|
-
await applyHook('afterUpdate', {
|
|
61
|
-
table: params?.table, body, payload: res, user,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// form DataTable
|
|
65
|
-
const extraKeys = Object.keys(schema || {})?.filter((key) => schema?.[key]?.type === 'DataTable' && schema?.[key]?.table && schema?.[key]?.parent_id && body[key].length);
|
|
66
|
-
if (extraKeys?.length) {
|
|
67
|
-
res.extra = {};
|
|
68
|
-
await Promise.all(extraKeys?.map(async (key) => {
|
|
69
|
-
const objId = body[schema[key].parent_id] || body?.id || res?.[schema[key]?.parent_id];
|
|
70
|
-
// delete old extra data
|
|
71
|
-
await pgClients.client.query(`delete from ${schema[key].table} where ${schema[key].parent_id}=$1`, [objId]); // rewrite?
|
|
72
|
-
// insert new extra data
|
|
73
|
-
const extraRows = await Promise.all(body[key].map(async (row) => {
|
|
74
|
-
const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid });
|
|
75
|
-
return extraRes?.rows?.[0];
|
|
76
|
-
}));
|
|
77
|
-
Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return res;
|
|
82
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate, logger, getToken,
|
|
3
|
+
} from '../../../../utils.js';
|
|
4
|
+
import config from '../../../../config.js';
|
|
5
|
+
import insert from './insert.js';
|
|
6
|
+
|
|
7
|
+
export default async function update(req) {
|
|
8
|
+
const { user, params = {}, body = {} } = req;
|
|
9
|
+
if (!user) return { message: 'access restricted', status: 403 };
|
|
10
|
+
const hookData = await applyHook('preUpdate', {
|
|
11
|
+
table: params?.table, id: params?.id, user,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (hookData?.message && hookData?.status) {
|
|
15
|
+
return { message: hookData?.message, status: hookData?.status };
|
|
16
|
+
}
|
|
17
|
+
const tokenData = await getToken({
|
|
18
|
+
uid: user.uid, token: body.token || params.table, mode: 'w', json: 1,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const { form, table: edit, id } = hookData || tokenData || (config.auth?.disable ? params : {});
|
|
22
|
+
|
|
23
|
+
const { actions = [] } = await getAccess({ table: edit, id, user }) || {};
|
|
24
|
+
|
|
25
|
+
if (!actions.includes('edit') && !config?.local && !tokenData) {
|
|
26
|
+
return { message: 'access restricted', status: 403 };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!edit) {
|
|
30
|
+
return { message: 'table is required', status: 400 };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!id && tokenData?.table) {
|
|
34
|
+
return insert(req);
|
|
35
|
+
}
|
|
36
|
+
if (!id) {
|
|
37
|
+
return { message: 'id is required', status: 404 };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const loadTemplate = await getTemplate('table', edit);
|
|
41
|
+
const { table } = loadTemplate || hookData || tokenData || params || {};
|
|
42
|
+
|
|
43
|
+
const uid = user?.uid;
|
|
44
|
+
|
|
45
|
+
const formData = form || loadTemplate?.form ? await getTemplate('form', form || loadTemplate?.form) : {};
|
|
46
|
+
const schema = formData?.schema || formData;
|
|
47
|
+
|
|
48
|
+
const xssCheck = checkXSS({ body, schema });
|
|
49
|
+
|
|
50
|
+
if (xssCheck.error && formData?.xssCheck !== false) {
|
|
51
|
+
logger.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
|
|
52
|
+
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const res = await dataUpdate({
|
|
56
|
+
table: loadTemplate?.table || table, id, data: body, uid,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// admin.custom_column
|
|
60
|
+
await applyHook('afterUpdate', {
|
|
61
|
+
table: params?.table, body, payload: res, user,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// form DataTable
|
|
65
|
+
const extraKeys = Object.keys(schema || {})?.filter((key) => schema?.[key]?.type === 'DataTable' && schema?.[key]?.table && schema?.[key]?.parent_id && body[key].length);
|
|
66
|
+
if (extraKeys?.length) {
|
|
67
|
+
res.extra = {};
|
|
68
|
+
await Promise.all(extraKeys?.map(async (key) => {
|
|
69
|
+
const objId = body[schema[key].parent_id] || body?.id || res?.[schema[key]?.parent_id];
|
|
70
|
+
// delete old extra data
|
|
71
|
+
await pgClients.client.query(`delete from ${schema[key].table} where ${schema[key].parent_id}=$1`, [objId]); // rewrite?
|
|
72
|
+
// insert new extra data
|
|
73
|
+
const extraRows = await Promise.all(body[key].map(async (row) => {
|
|
74
|
+
const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid });
|
|
75
|
+
return extraRes?.rows?.[0];
|
|
76
|
+
}));
|
|
77
|
+
Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return res;
|
|
82
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
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;
|
|
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;
|