@opengis/fastify-table 1.1.137 → 1.1.138

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.1.137",
3
+ "version": "1.1.138",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -7,7 +7,7 @@ import logChanges from './utils/logChanges.js';
7
7
  const rclient = getRedis();
8
8
 
9
9
  export default async function dataDelete({
10
- table, id, pg: pg1, uid,
10
+ table, template, id, pg: pg1, uid,
11
11
  }) {
12
12
  const pg = pg1 || getPG({ name: 'client' });
13
13
  const { pk } = await getMeta({ pg, table });
@@ -16,7 +16,7 @@ export default async function dataDelete({
16
16
  // console.log(updateDataset);
17
17
  const res = await pg.query(delQuery, [id]).then(el => el.rows?.[0] || {});
18
18
  await logChanges({
19
- pg, table, id, uid, type: 'DELETE',
19
+ pg, table, template, id, uid, type: 'DELETE',
20
20
  });
21
21
  rclient.incr(`pg:${table}:crud`);
22
22
  return res;
@@ -7,7 +7,7 @@ import logChanges from './utils/logChanges.js';
7
7
  const rclient = getRedis();
8
8
 
9
9
  export default async function dataInsert({
10
- id, table, data, pg: pg1, uid,
10
+ id, table, template, data, pg: pg1, uid,
11
11
  }) {
12
12
  const pg = pg1 || getPG({ name: 'client' });
13
13
  if (!data) return null;
@@ -37,7 +37,7 @@ export default async function dataInsert({
37
37
  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]))]) || {};
38
38
 
39
39
  await logChanges({
40
- pg, table, data, id: res.rows?.[0]?.[pg.pk[table]], uid, type: 'INSERT',
40
+ pg, table, template, data, id: res.rows?.[0]?.[pg.pk[table]], uid, type: 'INSERT',
41
41
  });
42
42
 
43
43
  rclient.incr(`pg:${table}:crud`);
@@ -18,7 +18,7 @@ function assignValue(key, i, srid = 4326, columnType = 'text') {
18
18
  }
19
19
 
20
20
  export default async function dataUpdate({
21
- table, id, data, pg: pg1, uid,
21
+ table, template, id, data, pg: pg1, uid,
22
22
  }) {
23
23
  if (!data || !table || !id) return null;
24
24
 
@@ -57,7 +57,7 @@ export default async function dataUpdate({
57
57
  const res = await pg.query(updateQuery, [id, ...filterValue]).then(el => el?.rows?.[0]) || {};
58
58
 
59
59
  await logChanges({
60
- pg, table, data, id, uid, type: 'UPDATE',
60
+ pg, table, template, data, id, uid, type: 'UPDATE',
61
61
  });
62
62
 
63
63
  rclient.incr(`pg:${table}:crud`);
@@ -1,7 +1,16 @@
1
1
  import getMeta from '../../../pg/funcs/getMeta.js';
2
+ import getTemplate from '../../..//table/funcs/getTemplate.js';
3
+ import { metaFormat } from '@opengis/fastify-table/utils.js';
4
+
5
+ const getValue = function (val) {
6
+ if (!val) return null;
7
+ return typeof val === 'object'
8
+ ? JSON.stringify(val)?.substring?.(0, 30)
9
+ : val?.toString?.()?.substring?.(0, 30);
10
+ }
2
11
 
3
12
  export default async function logChanges({
4
- pg, table, id, data, uid = 1, type,
13
+ pg, table, template, id, data, uid = 1, type,
5
14
  }) {
6
15
  if (!id) {
7
16
  console.error('param id is required');
@@ -24,21 +33,46 @@ export default async function logChanges({
24
33
  const { change_id: changeId } = await pg.query(`insert into log.table_changes(change_date,change_type,change_user_id,entity_type,entity_id)
25
34
  values(CURRENT_DATE, $1, $2, $3, $4) returning change_id`, [type, uid, table, id]).then((res) => res.rows?.[0] || {});
26
35
 
27
- const { fields = [] } = await pg.query(`select * from ${table} limit 0`);
28
- const columnList = fields.map((el) => el?.name);
29
- const q = `select ${Object.keys(data || {}).filter((el) => columnList.includes(el)).map(el => `"${el.replace(/'/g, "''")}"`).join(',') || '*'} from ${table} where ${pg.pk?.[table]}=$1`;
36
+ const q = `select json_object_agg(entity_key, value_new) from (
37
+ select
38
+ entity_key,
39
+ value_new,
40
+ ( rank() over (partition by entity_key order by cdate desc) = 1 ) as is_latest
41
+ from log.table_changes_data
42
+
43
+ where change_id in (
44
+ select
45
+ change_id
46
+ from log.table_changes
47
+ where entity_id=$1
48
+ and entity_type=$2
49
+ )
50
+
51
+ )q where is_latest`;
30
52
  // console.log(q, type, id);
31
53
 
32
- const old = type !== 'INSERT' ? await pg.query(q, [id]).then((res) => res.rows?.[0] || {}) : {};
54
+ const old = type !== 'INSERT' ? await pg.query(q, [id, table]).then(el => el.rows?.[0]?.json_object_agg || {}) : {};
33
55
 
34
56
  const { columns = [] } = await getMeta({ table: 'log.table_changes_data' });
35
- const names = columns.map((el) => el.name);
57
+ // const names = columns.map((el) => el.name);
58
+
59
+ const body = template ? await getTemplate('table', template) : null;
60
+ const cls = body?.columns?.filter(el => el.data)?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.data }), {});
61
+ Object.keys(data || {}).filter(key => typeof data[key] === 'boolean').forEach(key => Object.assign(cls, { [key]: 'yes_no' }));
62
+ const titles = (body?.columns || columns)?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.title || curr.ua }), {});
63
+
64
+ await metaFormat({ rows: [data], cls, sufix: false });
65
+ const newObj = Object.fromEntries(Object.entries(data || {}).map(el => ([[titles[el[0]] || el[0]], el[1]])));
66
+ const changesData = Object.keys(newObj || {}).map(el => ({
67
+ change_id: changeId,
68
+ entity_key: titles[el] || el,
69
+ value_old: getValue(old?.[el]),
70
+ value_new: getValue(newObj?.[el]),
71
+ uid,
72
+ })).filter(el => el?.value_new !== el?.value_old);
36
73
 
37
- const res = await Promise.all(Object.keys(data || {}).map(async (el) => {
38
- const filterData = Object.entries({
39
- change_id: changeId, entity_key: el, value_old: old[el], value_new: data[el], uid,
40
- })
41
- .filter((el1) => el1[1] && names.includes(el1[0]));
74
+ const res = await Promise.all(changesData.map(async (el) => {
75
+ const filterData = Object.entries(el);
42
76
 
43
77
  const insertQuery = `insert into log.table_changes_data (${filterData?.map((key) => `"${key[0]}"`).join(',')})
44
78
  values (${filterData?.map((key, i) => `$${i + 1}`).join(',')}) returning *`;
@@ -29,7 +29,7 @@ export default async function deleteCrud(req) {
29
29
  if (!id) return { status: 404, message: 'id is required' };
30
30
 
31
31
  const data = await dataDelete({
32
- table, id, uid: user?.uid,
32
+ table, id, uid: user?.uid, template: tokenData?.template,
33
33
  });
34
34
 
35
35
  return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
@@ -53,7 +53,11 @@ export default async function insert(req) {
53
53
  }
54
54
 
55
55
  const res = await dataInsert({
56
- id: params?.id, table: loadTemplate?.table || table, data: body, uid: user?.uid,
56
+ id: params?.id,
57
+ table: loadTemplate?.table || table,
58
+ data: body,
59
+ uid: user?.uid,
60
+ template: tokenData?.template,
57
61
  });
58
62
  if (!res) return { message: 'nothing added ' };
59
63
 
@@ -71,7 +75,7 @@ export default async function insert(req) {
71
75
  const objId = body[schema[key].parent_id] || req.body?.id || res?.rows?.[0]?.[schema[key].parent_id] || pkey;
72
76
  const extraRows = await Promise.all(body[key].map(async (row) => {
73
77
  const extraRes = await dataInsert({
74
- table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid: user?.uid,
78
+ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid: user?.uid, template: tokenData?.template,
75
79
  });
76
80
  return extraRes?.rows?.[0];
77
81
  }));
@@ -79,7 +79,7 @@ export default async function tableAPI(req) {
79
79
  }
80
80
  if (user?.uid && actions?.includes?.('edit')) {
81
81
  data.token = tokenData?.table ? params.table : setToken({
82
- ids: [JSON.stringify({ id, table: tableName, form: loadTable.form })],
82
+ ids: [JSON.stringify({ id, table: tableName, form: loadTable.form, template: params?.table })],
83
83
  uid: user.uid,
84
84
  array: 1,
85
85
  })[0];
@@ -60,7 +60,11 @@ export default async function update(req) {
60
60
  }
61
61
 
62
62
  const res = await dataUpdate({
63
- table: loadTemplate?.table || table, id, data: body, uid,
63
+ table: loadTemplate?.table || table,
64
+ id,
65
+ data: body,
66
+ uid,
67
+ template: tokenData?.template,
64
68
  });
65
69
 
66
70
  // admin.custom_column
@@ -79,7 +83,7 @@ export default async function update(req) {
79
83
  // insert new extra data
80
84
  if (Array.isArray(body[key]) && body[key]?.length) {
81
85
  const extraRows = await Promise.all(body[key]?.map?.(async (row) => {
82
- const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid });
86
+ const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid, template: tokenData?.template });
83
87
  return extraRes?.rows?.[0];
84
88
  }));
85
89
  Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });
package/utils.js CHANGED
@@ -62,6 +62,8 @@ import eventStream from './server/plugins/util/funcs/eventStream.js';
62
62
  import isFileExists from './server/plugins/crud/funcs/isFileExists.js';
63
63
  import getFolder from './server/plugins/crud/funcs/utils/getFolder.js';
64
64
 
65
+ import logChanges from './server/plugins/crud/funcs/utils/logChanges.js';
66
+
65
67
  export default null;
66
68
  export {
67
69
  config,
@@ -119,4 +121,5 @@ export {
119
121
  getSelectMeta,
120
122
  getSelect,
121
123
 
124
+ logChanges,
122
125
  };