@opengis/fastify-table 1.1.138 → 1.1.139

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.138",
3
+ "version": "1.1.139",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -57,7 +57,7 @@ export default async function logChanges({
57
57
  // const names = columns.map((el) => el.name);
58
58
 
59
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 }), {});
60
+ const cls = body?.columns?.filter(el => el.data)?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.data }), {}) || {};
61
61
  Object.keys(data || {}).filter(key => typeof data[key] === 'boolean').forEach(key => Object.assign(cls, { [key]: 'yes_no' }));
62
62
  const titles = (body?.columns || columns)?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.title || curr.ua }), {});
63
63
 
@@ -67,17 +67,15 @@ export default async function logChanges({
67
67
  change_id: changeId,
68
68
  entity_key: titles[el] || el,
69
69
  value_old: getValue(old?.[el]),
70
- value_new: getValue(newObj?.[el]),
70
+ value_new: type === 'DELETE' ? null : getValue(newObj?.[el]),
71
71
  uid,
72
72
  })).filter(el => el?.value_new !== el?.value_old);
73
73
 
74
74
  const res = await Promise.all(changesData.map(async (el) => {
75
- const filterData = Object.entries(el);
75
+ const insertQuery = `insert into log.table_changes_data (${Object.entries(el)?.map((key) => `"${key[0]}"`).join(',')})
76
+ values (${Object.entries(el)?.map((key, i) => `$${i + 1}`).join(',')}) returning *`;
76
77
 
77
- const insertQuery = `insert into log.table_changes_data (${filterData?.map((key) => `"${key[0]}"`).join(',')})
78
- values (${filterData?.map((key, i) => `$${i + 1}`).join(',')}) returning *`;
79
-
80
- const { rows = [] } = await pg.query(insertQuery, [...filterData.map((el1) => (el1[1] && typeof el1[1] === 'object' && (!Array.isArray(el1[1]) || typeof el1[1]?.[0] === 'object') ? JSON.stringify(el1[1]) : el1[1]))]) || {};
78
+ 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]))]) || {};
81
79
  return rows[0];
82
80
  }));
83
81
 
@@ -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, template: tokenData?.template,
32
+ table, id, uid: user?.uid, template: tokenData?.template || tokenData?.table,
33
33
  });
34
34
 
35
35
  return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
@@ -57,7 +57,7 @@ export default async function insert(req) {
57
57
  table: loadTemplate?.table || table,
58
58
  data: body,
59
59
  uid: user?.uid,
60
- template: tokenData?.template,
60
+ template: tokenData?.template || tokenData?.table,
61
61
  });
62
62
  if (!res) return { message: 'nothing added ' };
63
63
 
@@ -64,7 +64,7 @@ export default async function update(req) {
64
64
  id,
65
65
  data: body,
66
66
  uid,
67
- template: tokenData?.template,
67
+ template: tokenData?.template || tokenData?.table,
68
68
  });
69
69
 
70
70
  // admin.custom_column
@@ -83,7 +83,7 @@ export default async function update(req) {
83
83
  // insert new extra data
84
84
  if (Array.isArray(body[key]) && body[key]?.length) {
85
85
  const extraRows = await Promise.all(body[key]?.map?.(async (row) => {
86
- const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid, template: tokenData?.template });
86
+ const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid, template: tokenData?.template || tokenData?.table });
87
87
  return extraRes?.rows?.[0];
88
88
  }));
89
89
  Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });