@opengis/fastify-table 1.2.1 → 1.2.3

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.2.1",
3
+ "version": "1.2.3",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -35,8 +35,8 @@ if (_returnType != 'text') then
35
35
 
36
36
  DROP FUNCTION IF EXISTS next_id();
37
37
 
38
- CREATE EXTENSION if not exists "uuid-ossp";
39
- ALTER EXTENSION "uuid-ossp" SET SCHEMA public;
38
+ /* CREATE EXTENSION if not exists "uuid-ossp";
39
+ ALTER EXTENSION "uuid-ossp" SET SCHEMA public; */
40
40
 
41
41
  CREATE OR REPLACE FUNCTION next_id()
42
42
  RETURNS text AS
@@ -44,7 +44,9 @@ if (_returnType != 'text') then
44
44
  DECLARE
45
45
 
46
46
  BEGIN
47
- return replace(public.uuid_generate_v4()::text, '-', '');
47
+ -- return replace(public.uuid_generate_v4()::text, '-', '');
48
+ -- return replace(gen_random_uuid()::text, '-', ''); -- native from postgres 13 onward
49
+ return encode(public.gen_random_bytes(6), 'hex');
48
50
  END;
49
51
  $BODY$
50
52
  LANGUAGE plpgsql VOLATILE
@@ -61,8 +63,8 @@ if (_returnType != 'text') then
61
63
  else
62
64
  raise notice 'skip default reassign';
63
65
 
64
- CREATE EXTENSION if not exists "uuid-ossp";
65
- ALTER EXTENSION "uuid-ossp" SET SCHEMA public;
66
+ /* CREATE EXTENSION if not exists "uuid-ossp";
67
+ ALTER EXTENSION "uuid-ossp" SET SCHEMA public; */
66
68
 
67
69
  CREATE OR REPLACE FUNCTION next_id()
68
70
  RETURNS text AS
@@ -70,7 +72,9 @@ else
70
72
  DECLARE
71
73
 
72
74
  BEGIN
73
- return replace(public.uuid_generate_v4()::text, '-', '');
75
+ -- return replace(public.uuid_generate_v4()::text, '-', '');
76
+ -- return replace(gen_random_uuid()::text, '-', ''); -- native from postgres 13 onward
77
+ return encode(public.gen_random_bytes(6), 'hex');
74
78
  END;
75
79
  $BODY$
76
80
  LANGUAGE plpgsql VOLATILE
@@ -11,9 +11,10 @@ export default async function dataDelete({
11
11
  }) {
12
12
  const pg = pg1 || getPG({ name: 'client' });
13
13
  const { pk } = await getMeta({ pg, table });
14
- if (!pg.tlist?.includes(table)) return 'table not exist';
14
+ const table1 = table.replace(/"/g, '');
15
+ if (!pg.tlist?.includes(table1)) return 'table not exist';
15
16
  const delQuery = `delete from ${table} WHERE ${pk} = $1 returning *`;
16
- // console.log(updateDataset);
17
+
17
18
  const res = await pg.query(delQuery, [id]).then(el => el.rows?.[0] || {});
18
19
  await logChanges({
19
20
  pg, table, tokenData, referer, id, uid, type: 'DELETE',
@@ -36,8 +36,16 @@ export default async function dataInsert({
36
36
 
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
+ const table1 = pg.pk[table] ? table : table.replace(/"/g, '');
39
40
  await logChanges({
40
- pg, table, tokenData, referer, data, id: res.rows?.[0]?.[pg.pk[table]], uid, type: 'INSERT',
41
+ pg,
42
+ table,
43
+ tokenData,
44
+ referer,
45
+ data,
46
+ id: res.rows?.[0]?.[pg.pk[table1]],
47
+ uid,
48
+ type: 'INSERT',
41
49
  });
42
50
 
43
51
  rclient.incr(`pg:${table}:crud`);
@@ -56,12 +56,17 @@ export default async function getAccess({ table, user = {} }) {
56
56
  return { actions: [], query: '1=1' };
57
57
  }
58
58
 
59
- const userAccess = await pgClients.client.query(q, [table, uid])
60
- .then(el => ({
61
- ...el.rows[0] || {},
62
- roles: el.rows?.map?.(row => row.role_id) || [],
63
- actions: el.rows?.map?.(row => row.actions).flat() || [],
64
- }));
59
+ const userAccess = pgClients.client?.pk?.['admin.routes']
60
+ && pgClients.client?.pk?.['admin.role_access']
61
+ && pgClients.client?.pk?.['admin.roles']
62
+ && pgClients.client?.pk?.['admin.user_roles']
63
+ ? await pgClients.client.query(q, [table, uid])
64
+ .then(el => ({
65
+ ...el.rows[0] || {},
66
+ roles: el.rows?.map?.(row => row.role_id) || [],
67
+ actions: el.rows?.map?.(row => row.actions).flat() || [],
68
+ }))
69
+ : {};
65
70
 
66
71
  const query = userAccess?.scope === 'my' ? `uid='${uid}'` : '1=1';
67
72
  const actions = userAccess?.actions?.filter?.((el, idx, arr) => arr.indexOf(el) === idx && tableActions.includes(el));
@@ -1,6 +1,18 @@
1
1
  import getTemplate from '../../..//table/funcs/getTemplate.js';
2
2
  import { metaFormat } from '@opengis/fastify-table/utils.js';
3
3
 
4
+ const defaultTitles = {
5
+ // editor_date: 'Дата оновлення',
6
+ // editor_id: 'Редактор',
7
+ // сdate: 'Дата створення',
8
+ // uid: 'Автор',
9
+ body: 'Зміст',
10
+ // entity_id: 'ID Сутності',
11
+ // entity_type: 'Таблиця сутності',
12
+ file_path: 'Шлях до файлу',
13
+ uploaded_name: 'Назва файлу',
14
+ };
15
+
4
16
  function getValue(val) {
5
17
  if (!val) return null;
6
18
  return typeof val === 'object'
@@ -11,8 +23,9 @@ function getValue(val) {
11
23
  // extract titles and cls from form schema
12
24
  // alt: extract table template from referer -> form
13
25
  export default async function logChanges({
14
- pg, table, tokenData, referer, id, data, uid = 1, type,
26
+ pg, table: table1, tokenData, referer, id, data, uid = 1, type,
15
27
  }) {
28
+ const table = table1.replace(/"/g, '');
16
29
  if (!id) {
17
30
  console.error('param id is required');
18
31
  return null;
@@ -66,10 +79,10 @@ export default async function logChanges({
66
79
  await metaFormat({ rows: [data], cls, sufix: false });
67
80
  }
68
81
 
69
- const newObj = Object.fromEntries(Object.entries(data || {}).map(el => ([[titles[el[0]] || el[0]], el[1]])));
82
+ const newObj = Object.fromEntries(Object.entries(data || {}).map(el => ([[titles[el[0]] || defaultTitles[el[0]] || el[0]], el[1]])));
70
83
  const changesData = Object.keys(newObj || {}).map(el => ({
71
84
  change_id: changeId,
72
- entity_key: titles[el[0]] || el,
85
+ entity_key: el,
73
86
  value_old: getValue(old?.[el]),
74
87
  value_new: type === 'DELETE' ? null : getValue(newObj?.[el]),
75
88
  uid,