@opengis/fastify-table 1.0.89 → 1.0.91
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/Changelog.md +8 -0
- package/crud/controllers/deleteCrud.js +4 -1
- package/crud/controllers/insert.js +6 -2
- package/crud/controllers/update.js +5 -3
- package/crud/funcs/dataDelete.js +19 -15
- package/crud/funcs/dataInsert.js +7 -1
- package/crud/funcs/dataUpdate.js +7 -2
- package/crud/funcs/utils/logChanges.js +71 -0
- package/crud/index.js +36 -36
- package/package.json +1 -1
- package/server/migrations/log.sql +63 -26
- package/table/controllers/table.js +4 -1
- package/test/api/widget.test.js +117 -114
- package/test/funcs/crud.test.js +122 -76
- package/util/controllers/logger.file.js +90 -0
- package/util/controllers/properties.add.js +57 -51
- package/util/controllers/utils/checkUserAccess.js +19 -0
- package/util/controllers/utils/getRootDir.js +21 -0
- package/util/index.js +23 -21
- package/widget/controllers/widget.get.js +5 -3
- package/widget/controllers/widget.set.js +9 -3
- package/widget/index.js +40 -40
package/Changelog.md
CHANGED
|
@@ -13,7 +13,10 @@ export default async function deleteCrud(req) {
|
|
|
13
13
|
|
|
14
14
|
if (!table) return { status: 404, message: 'table is required' };
|
|
15
15
|
|
|
16
|
-
const
|
|
16
|
+
const { user = {} } = req;
|
|
17
|
+
const data = await dataDelete({
|
|
18
|
+
table, id, uid: user?.uid,
|
|
19
|
+
});
|
|
17
20
|
|
|
18
21
|
return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
|
|
19
22
|
}
|
|
@@ -36,14 +36,18 @@ export default async function insert(req) {
|
|
|
36
36
|
|
|
37
37
|
const { uid } = funcs.config?.auth?.disable || ispublic ? { uid: '1' } : user || {};
|
|
38
38
|
Object.assign(req.body, { uid, editor_id: uid });
|
|
39
|
-
const res = await dataInsert({
|
|
39
|
+
const res = await dataInsert({
|
|
40
|
+
table: add || table, data: req.body, uid,
|
|
41
|
+
});
|
|
40
42
|
|
|
41
43
|
const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && req.body[key].length);
|
|
42
44
|
if (extraKeys?.length) {
|
|
43
45
|
res.extra = {};
|
|
44
46
|
await Promise.all(extraKeys?.map(async (key) => {
|
|
45
47
|
const extraRows = await Promise.all(req.body[key].map(async (row) => {
|
|
46
|
-
const extraRes = await dataInsert({
|
|
48
|
+
const extraRes = await dataInsert({
|
|
49
|
+
table: formData[key].table, data: { ...row, [formData[key].parent_id]: req.body[formData[key].parent_id] }, uid,
|
|
50
|
+
});
|
|
47
51
|
return extraRes?.rows?.[0];
|
|
48
52
|
}));
|
|
49
53
|
Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
|
|
@@ -38,17 +38,19 @@ export default async function update(req) {
|
|
|
38
38
|
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
const res = await dataUpdate({
|
|
41
|
+
const res = await dataUpdate({
|
|
42
|
+
table: tokenData?.table || table, id: tokenData?.id || id, data: req.body, uid,
|
|
43
|
+
});
|
|
42
44
|
|
|
43
45
|
const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && req.body[key].length);
|
|
44
46
|
if (extraKeys?.length) {
|
|
45
47
|
res.extra = {};
|
|
46
48
|
await Promise.all(extraKeys?.map(async (key) => {
|
|
47
49
|
// delete old extra data
|
|
48
|
-
await pgClients.client.query(`delete from ${formData[key].table} where ${formData[key].parent_id}=$1`, [req.body[formData[key].parent_id]]);
|
|
50
|
+
await pgClients.client.query(`delete from ${formData[key].table} where ${formData[key].parent_id}=$1`, [req.body[formData[key].parent_id]]); // rewrite?
|
|
49
51
|
// insert new extra data
|
|
50
52
|
const extraRows = await Promise.all(req.body[key].map(async (row) => {
|
|
51
|
-
const extraRes = await dataInsert({ table: formData[key].table, data: { ...row, [formData[key].parent_id]: req.body[formData[key].parent_id] } });
|
|
53
|
+
const extraRes = await dataInsert({ table: formData[key].table, data: { ...row, [formData[key].parent_id]: req.body[formData[key].parent_id] }, uid });
|
|
52
54
|
return extraRes?.rows?.[0];
|
|
53
55
|
}));
|
|
54
56
|
Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
|
package/crud/funcs/dataDelete.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import getPG from '../../pg/funcs/getPG.js';
|
|
2
|
-
|
|
3
|
-
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
2
|
+
|
|
3
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
+
import logChanges from './utils/logChanges.js';
|
|
5
|
+
|
|
6
|
+
export default async function dataDelete({
|
|
7
|
+
table, id, pg: pg1, uid,
|
|
8
|
+
}) {
|
|
9
|
+
const pg = pg1 || getPG({ name: 'client' });
|
|
10
|
+
const { pk } = await getMeta(table);
|
|
11
|
+
if (!pg.tlist?.includes(table)) return 'table not exist';
|
|
12
|
+
const delQuery = `delete from ${table} WHERE ${pk} = $1 returning *`;
|
|
13
|
+
// console.log(updateDataset);
|
|
14
|
+
const res = await pg.one(delQuery, [id]) || {};
|
|
15
|
+
await logChanges({
|
|
16
|
+
pg, table, id, uid, type: 'DELETE',
|
|
17
|
+
});
|
|
18
|
+
return res;
|
|
19
|
+
}
|
package/crud/funcs/dataInsert.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import getPG from '../../pg/funcs/getPG.js';
|
|
2
2
|
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
+
import logChanges from './utils/logChanges.js';
|
|
3
4
|
|
|
4
|
-
export default async function dataInsert({
|
|
5
|
+
export default async function dataInsert({
|
|
6
|
+
table, data, pg: pg1, uid,
|
|
7
|
+
}) {
|
|
5
8
|
const pg = pg1 || getPG({ name: 'client' });
|
|
6
9
|
if (!data) return null;
|
|
7
10
|
const { columns } = await getMeta(table);
|
|
@@ -20,5 +23,8 @@ export default async function dataInsert({ table, data, pg: pg1 }) {
|
|
|
20
23
|
returning *`;
|
|
21
24
|
|
|
22
25
|
const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]))]) || {};
|
|
26
|
+
await logChanges({
|
|
27
|
+
pg, table, data, id: res.rows?.[0]?.[pg.pk[table]], uid, type: 'INSERT',
|
|
28
|
+
});
|
|
23
29
|
return res;
|
|
24
30
|
}
|
package/crud/funcs/dataUpdate.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import getPG from '../../pg/funcs/getPG.js';
|
|
2
2
|
|
|
3
3
|
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
+
import logChanges from './utils/logChanges.js';
|
|
4
5
|
|
|
5
6
|
export default async function dataUpdate({
|
|
6
|
-
table, id, data, pg: pg1,
|
|
7
|
+
table, id, data, pg: pg1, uid,
|
|
7
8
|
}) {
|
|
8
9
|
if (!data || !table || !id) return null;
|
|
9
10
|
|
|
@@ -14,7 +15,11 @@ export default async function dataUpdate({
|
|
|
14
15
|
const filterData = Object.keys(data)
|
|
15
16
|
.filter((el) => (/* typeof data[el] === 'boolean' ? true : data[el] && */ names?.includes(el)));
|
|
16
17
|
|
|
17
|
-
const filterValue = filterData.map((el) => [el, data[el]]).map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]));
|
|
18
|
+
const filterValue = filterData.map((el) => [el, data[el]]).map((el) => (typeof el[1] === 'object' && el[1] && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]));
|
|
19
|
+
|
|
20
|
+
await logChanges({
|
|
21
|
+
pg, table, data, id, uid, type: 'UPDATE',
|
|
22
|
+
});
|
|
18
23
|
|
|
19
24
|
const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => (key === 'geom' ? `"${key}"=st_setsrid(st_geomfromgeojson($${i + 2}::json),4326)` : `"${key}"=$${i + 2}`)).join(',')}
|
|
20
25
|
WHERE ${pk} = $1 returning *`;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
function formatData(fieldType = 'text', value = null) {
|
|
2
|
+
if (!value) return null;
|
|
3
|
+
if (fieldType === 'geometry') {
|
|
4
|
+
return typeof value === 'object' ? `st_astext(st_geomfromgeojson('${JSON.stringify(value)}'::json))` : `st_astext('${value}'::geometry)`;
|
|
5
|
+
}
|
|
6
|
+
if (['integer', 'numeric', 'double precision'].includes(fieldType)) {
|
|
7
|
+
return value || null;
|
|
8
|
+
}
|
|
9
|
+
if (fieldType.includes('timestamp') || fieldType === 'date') {
|
|
10
|
+
if (typeof value === 'object') {
|
|
11
|
+
return value ? `'${value.toISOString()}'::${fieldType}` : null;
|
|
12
|
+
}
|
|
13
|
+
return value ? `'${value}'::${fieldType}` : null;
|
|
14
|
+
}
|
|
15
|
+
/* if (fieldType.includes('json') && Array.isArray(value)) {
|
|
16
|
+
return value;
|
|
17
|
+
} */
|
|
18
|
+
if (Array.isArray(value)) {
|
|
19
|
+
return `'{ ${value.join(',')} }'::${fieldType}`;
|
|
20
|
+
}
|
|
21
|
+
return typeof value === 'object' ? JSON.stringify(value) : `'${value || null}'`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default async function logChanges({
|
|
25
|
+
pg, table, id, data, uid = 1, type,
|
|
26
|
+
}) {
|
|
27
|
+
if (!id) {
|
|
28
|
+
console.error('param id is required');
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (!table || !pg.pk?.[table]) {
|
|
32
|
+
console.error('table not found');
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (!pg.pk?.['log.table_changes'] || !pg.pk?.['log.table_changes_data']) {
|
|
36
|
+
console.error('log table not found');
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (!type) {
|
|
40
|
+
console.error('invalid type');
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const { change_id: changeId } = await pg.query(`insert into log.table_changes(change_date,change_type,change_user_id,entity_type,entity_id)
|
|
46
|
+
values(CURRENT_DATE, $1, $2, $3, $4) returning change_id`, [type, uid, table, id]).then((res) => res.rows?.[0] || {});
|
|
47
|
+
|
|
48
|
+
const q = `select ${Object.keys(data || {}).join(',') || '*'} from ${table} where ${pg.pk?.[table]}=$1`;
|
|
49
|
+
// console.log(q, type, id);
|
|
50
|
+
const { fields = [] } = await pg.query(`select * from ${table} limit 0`);
|
|
51
|
+
const old = type !== 'INSERT' ? await pg.query(q, [id]).then((res) => res.rows?.[0] || {}) : {};
|
|
52
|
+
|
|
53
|
+
const fieldTypes = fields?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: pg.pgType[curr.dataTypeID] }), {}) || {};
|
|
54
|
+
const q1 = Object.keys(data || {}).map((el) => `insert into log.table_changes_data(change_id,entity_key,value_old,value_new)
|
|
55
|
+
values('${changeId}', '${el}', ${formatData(fieldTypes[el], old[el])}, ${formatData(fieldTypes[el], data[el])}) returning *`).join(';\n');
|
|
56
|
+
// console.log(q1);
|
|
57
|
+
const res = await pg.query(q1);
|
|
58
|
+
|
|
59
|
+
const newData = type === 'DELETE' ? {} : (Array.isArray(res) ? res : [res]).reduce((acc, curr) => Object.assign(acc, { [curr.rows?.[0].entity_key]: curr.rows?.[0].value_new }), {});
|
|
60
|
+
// console.log('logChanges OK', type);
|
|
61
|
+
return {
|
|
62
|
+
change_id: changeId, entity_type: table, entity_id: id, uid, change_type: type, old, new: newData,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
console.error('logChanges error', type, table, id, data, err.toString());
|
|
67
|
+
return {
|
|
68
|
+
error: err.toString(), entity_type: table, entity_id: id, uid, change_type: type,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
package/crud/index.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import getOpt from './funcs/getOpt.js';
|
|
2
|
-
import setOpt from './funcs/setOpt.js';
|
|
3
|
-
import isFileExists from './funcs/isFileExists.js';
|
|
4
|
-
import dataUpdate from './funcs/dataUpdate.js';
|
|
5
|
-
import dataInsert from './funcs/dataInsert.js';
|
|
6
|
-
|
|
7
|
-
import update from './controllers/update.js';
|
|
8
|
-
import insert from './controllers/insert.js';
|
|
9
|
-
import deleteCrud from './controllers/deleteCrud.js';
|
|
10
|
-
import getAccessFunc from './funcs/getAccess.js';
|
|
11
|
-
|
|
12
|
-
const tableSchema = {
|
|
13
|
-
params: {
|
|
14
|
-
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
15
|
-
table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
async function plugin(fastify, config = {}) {
|
|
20
|
-
const prefix = config.prefix || '/api';
|
|
21
|
-
// funcs
|
|
22
|
-
fastify.decorate('setOpt', setOpt);
|
|
23
|
-
fastify.decorate('getOpt', getOpt);
|
|
24
|
-
fastify.decorate('dataUpdate', dataUpdate);
|
|
25
|
-
fastify.decorate('dataInsert', dataInsert);
|
|
26
|
-
fastify.decorate('getAccess', getAccessFunc);
|
|
27
|
-
|
|
28
|
-
fastify.decorate('isFileExists', isFileExists);
|
|
29
|
-
|
|
30
|
-
// api
|
|
31
|
-
fastify.put(`${prefix}/table/:table/:id`, { schema: tableSchema }, update);
|
|
32
|
-
fastify.delete(`${prefix}/table/:table/:id`, { schema: tableSchema }, deleteCrud);
|
|
33
|
-
fastify.post(`${prefix}/table/:table`, { schema: tableSchema }, insert);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export default plugin;
|
|
1
|
+
import getOpt from './funcs/getOpt.js';
|
|
2
|
+
import setOpt from './funcs/setOpt.js';
|
|
3
|
+
import isFileExists from './funcs/isFileExists.js';
|
|
4
|
+
import dataUpdate from './funcs/dataUpdate.js';
|
|
5
|
+
import dataInsert from './funcs/dataInsert.js';
|
|
6
|
+
|
|
7
|
+
import update from './controllers/update.js';
|
|
8
|
+
import insert from './controllers/insert.js';
|
|
9
|
+
import deleteCrud from './controllers/deleteCrud.js';
|
|
10
|
+
import getAccessFunc from './funcs/getAccess.js';
|
|
11
|
+
|
|
12
|
+
const tableSchema = {
|
|
13
|
+
params: {
|
|
14
|
+
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
15
|
+
table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
async function plugin(fastify, config = {}) {
|
|
20
|
+
const prefix = config.prefix || '/api';
|
|
21
|
+
// funcs
|
|
22
|
+
fastify.decorate('setOpt', setOpt);
|
|
23
|
+
fastify.decorate('getOpt', getOpt);
|
|
24
|
+
fastify.decorate('dataUpdate', dataUpdate);
|
|
25
|
+
fastify.decorate('dataInsert', dataInsert);
|
|
26
|
+
fastify.decorate('getAccess', getAccessFunc);
|
|
27
|
+
|
|
28
|
+
fastify.decorate('isFileExists', isFileExists);
|
|
29
|
+
|
|
30
|
+
// api
|
|
31
|
+
fastify.put(`${prefix}/table/:table/:id`, { schema: tableSchema }, update);
|
|
32
|
+
fastify.delete(`${prefix}/table/:table/:id`, { schema: tableSchema }, deleteCrud);
|
|
33
|
+
fastify.post(`${prefix}/table/:table`, { schema: tableSchema }, insert);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default plugin;
|
package/package.json
CHANGED
|
@@ -1,44 +1,81 @@
|
|
|
1
1
|
create schema if not exists log;
|
|
2
2
|
|
|
3
|
+
-- DROP TABLE IF EXISTS log.table_changes cascade;
|
|
3
4
|
CREATE TABLE IF NOT EXISTS log.table_changes();
|
|
4
|
-
ALTER TABLE log.table_changes
|
|
5
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS table_change_id text NOT NULL DEFAULT next_id();
|
|
6
|
-
|
|
7
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_type text;
|
|
8
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
|
|
5
|
+
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_id text NOT NULL DEFAULT next_id();
|
|
9
6
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_type text;
|
|
10
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_key text;
|
|
11
7
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_date date;
|
|
12
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS
|
|
13
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS
|
|
14
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS
|
|
15
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS date_new timestamp without time zone;
|
|
16
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS number_old numeric;
|
|
17
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS number_new numeric;
|
|
18
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS bool_old boolean;
|
|
19
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS bool_new boolean;
|
|
20
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS text_old text;
|
|
21
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS text_new text;
|
|
22
|
-
|
|
8
|
+
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_user_id text;
|
|
9
|
+
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_type text; -- table_name
|
|
10
|
+
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
|
|
23
11
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS uid text;
|
|
24
12
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
|
|
25
13
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS editor_id text;
|
|
26
14
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
27
|
-
ALTER TABLE log.table_changes ADD CONSTRAINT log_table_changes_pkey PRIMARY KEY (table_change_id);
|
|
28
15
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
ALTER TABLE log.
|
|
16
|
+
-- DROP TABLE IF EXISTS log.table_changes_data;
|
|
17
|
+
CREATE TABLE IF NOT EXISTS log.table_changes_data();
|
|
18
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS change_data_id text NOT NULL DEFAULT next_id();
|
|
19
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS change_id text not null;
|
|
20
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS entity_key text; -- column_name
|
|
21
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS value_old text;
|
|
22
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS value_new text;
|
|
23
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS uid text;
|
|
24
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
|
|
25
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS editor_id text;
|
|
26
|
+
ALTER TABLE log.table_changes_data ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
32
27
|
|
|
28
|
+
-- DROP TABLE IF EXISTS log.user_auth;
|
|
29
|
+
CREATE TABLE IF NOT EXISTS log.user_auth();
|
|
33
30
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS user_auth_id text NOT NULL DEFAULT next_id();
|
|
34
|
-
|
|
35
31
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS user_id text;
|
|
36
|
-
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS
|
|
37
|
-
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS
|
|
38
|
-
|
|
32
|
+
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS auth_date timestamp without time zone;
|
|
33
|
+
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS auth_type text;
|
|
39
34
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS uid text;
|
|
40
35
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
|
|
41
36
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS editor_id text;
|
|
42
37
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
38
|
+
|
|
39
|
+
COMMENT ON TABLE log.table_changes IS 'Логи подій змін в БД';
|
|
40
|
+
COMMENT ON COLUMN log.table_changes.change_type IS 'Тип події (insert / update / delete)';
|
|
41
|
+
COMMENT ON COLUMN log.table_changes.change_date IS 'Дата внесення змін до БД';
|
|
42
|
+
COMMENT ON COLUMN log.table_changes.entity_type IS 'Таблиця, до якої вносяться зміни';
|
|
43
|
+
COMMENT ON COLUMN log.table_changes.entity_id IS 'ID строки, до якої вносяться зміни';
|
|
44
|
+
COMMENT ON COLUMN log.table_changes.change_user_id IS 'Ініціатор внесення змін';
|
|
45
|
+
|
|
46
|
+
COMMENT ON TABLE log.table_changes_data IS 'Логи змін в таблицях БД';
|
|
47
|
+
COMMENT ON COLUMN log.table_changes_data.change_id IS 'ID події зміни в БД';
|
|
48
|
+
COMMENT ON COLUMN log.table_changes_data.entity_key IS 'Колонка таблиці, до якої вносяться зміни';
|
|
49
|
+
COMMENT ON COLUMN log.table_changes_data.value_old IS 'Старе значення';
|
|
50
|
+
COMMENT ON COLUMN log.table_changes_data.value_new IS 'Нове значення';
|
|
51
|
+
|
|
52
|
+
COMMENT ON TABLE log.user_auth IS 'Логи авторизації';
|
|
53
|
+
COMMENT ON COLUMN log.user_auth.user_id IS 'ID користувача';
|
|
54
|
+
COMMENT ON COLUMN log.user_auth.auth_date IS 'Дата авторизації';
|
|
55
|
+
COMMENT ON COLUMN log.user_auth.auth_type IS 'Тип авторизації';
|
|
56
|
+
|
|
57
|
+
ALTER TABLE log.table_changes DROP CONSTRAINT IF EXISTS log_table_changes_pkey cascade;
|
|
58
|
+
ALTER TABLE log.table_changes_data DROP CONSTRAINT IF EXISTS log_table_changes_data_pkey;
|
|
59
|
+
ALTER TABLE log.table_changes_data DROP CONSTRAINT IF EXISTS log_table_changes_data_change_id_fkey;
|
|
60
|
+
ALTER TABLE log.user_auth DROP CONSTRAINT IF EXISTS log_user_auth_pkey;
|
|
61
|
+
ALTER TABLE log.user_auth DROP CONSTRAINT IF EXISTS log_user_auth_user_id_fkey;
|
|
62
|
+
|
|
63
|
+
ALTER TABLE log.table_changes ADD CONSTRAINT log_table_changes_pkey PRIMARY KEY (change_id);
|
|
64
|
+
ALTER TABLE log.table_changes_data ADD CONSTRAINT log_table_changes_data_pkey PRIMARY KEY (change_data_id);
|
|
65
|
+
ALTER TABLE log.table_changes_data ADD CONSTRAINT log_table_changes_data_change_id_fkey FOREIGN KEY (change_id)
|
|
66
|
+
REFERENCES log.table_changes (change_id);
|
|
43
67
|
ALTER TABLE log.user_auth ADD CONSTRAINT log_user_auth_pkey PRIMARY KEY (user_auth_id);
|
|
44
|
-
-- ALTER TABLE log.user_auth ADD CONSTRAINT log_user_auth_user_id_fkey FOREIGN KEY (user_id) REFERENCES admin.users (uid) MATCH SIMPLE;
|
|
68
|
+
-- ALTER TABLE log.user_auth ADD CONSTRAINT log_user_auth_user_id_fkey FOREIGN KEY (user_id) REFERENCES admin.users (uid) MATCH SIMPLE;
|
|
69
|
+
|
|
70
|
+
/* drop old columns */
|
|
71
|
+
alter table log.table_changes drop column if exists date_new;
|
|
72
|
+
alter table log.table_changes drop column if exists date_old;
|
|
73
|
+
alter table log.table_changes drop column if exists number_new;
|
|
74
|
+
alter table log.table_changes drop column if exists number_old;
|
|
75
|
+
alter table log.table_changes drop column if exists json_new;
|
|
76
|
+
alter table log.table_changes drop column if exists json_old;
|
|
77
|
+
alter table log.table_changes drop column if exists text_new;
|
|
78
|
+
alter table log.table_changes drop column if exists text_old;
|
|
79
|
+
alter table log.table_changes drop column if exists bool_new;
|
|
80
|
+
alter table log.table_changes drop column if exists bool_old;
|
|
81
|
+
alter table log.table_changes drop column if exists table_change_id;
|
|
@@ -26,7 +26,10 @@ export default async function tableAPI(req) {
|
|
|
26
26
|
// skip non-existing columns
|
|
27
27
|
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${table || opt?.table || params.table} limit 0`) : {};
|
|
30
|
+
const cols = loadTable?.table
|
|
31
|
+
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.join(',')
|
|
32
|
+
: fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
30
33
|
const where = [`"${pk}" = $1`, loadTable.query].filter((el) => el);
|
|
31
34
|
const geom = columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : '';
|
|
32
35
|
const q = `select "${pk}" as id, ${geom} ${cols || '*'} from ${table || opt?.table || params.table} t where ${where.join(' and ') || 'true'} limit 1`;
|