@opengis/fastify-table 1.1.140 → 1.1.142

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.140",
3
+ "version": "1.1.142",
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, template, id, pg: pg1, uid,
10
+ table, tokenData, referer, 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, template, id, uid, type: 'DELETE',
19
+ pg, table, tokenData, referer, 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, template, data, pg: pg1, uid,
10
+ id, table, tokenData, referer, 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, template, data, id: res.rows?.[0]?.[pg.pk[table]], uid, type: 'INSERT',
40
+ pg, table, tokenData, referer, 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, template, id, data, pg: pg1, uid,
21
+ table, tokenData, referer, 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, template, data, id, uid, type: 'UPDATE',
60
+ pg, table, tokenData, referer, data, id, uid, type: 'UPDATE',
61
61
  });
62
62
 
63
63
  rclient.incr(`pg:${table}:crud`);
@@ -1,16 +1,17 @@
1
- import getMeta from '../../../pg/funcs/getMeta.js';
2
1
  import getTemplate from '../../..//table/funcs/getTemplate.js';
3
2
  import { metaFormat } from '@opengis/fastify-table/utils.js';
4
3
 
5
- const getValue = function (val) {
4
+ function getValue(val) {
6
5
  if (!val) return null;
7
6
  return typeof val === 'object'
8
7
  ? JSON.stringify(val)?.substring?.(0, 30)
9
8
  : val?.toString?.()?.substring?.(0, 30);
10
9
  }
11
10
 
11
+ // extract titles and cls from form schema
12
+ // alt: extract table template from referer -> form
12
13
  export default async function logChanges({
13
- pg, table, template, id, data, uid = 1, type,
14
+ pg, table, tokenData, referer, id, data, uid = 1, type,
14
15
  }) {
15
16
  if (!id) {
16
17
  console.error('param id is required');
@@ -53,13 +54,13 @@ export default async function logChanges({
53
54
 
54
55
  const old = type !== 'INSERT' ? await pg.query(q, [id, table]).then(el => el.rows?.[0]?.json_object_agg || {}) : {};
55
56
 
56
- const { columns = [] } = await getMeta({ table: 'log.table_changes_data' });
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 }), {});
57
+ const body = await getTemplate('form', tokenData?.form);
58
+ const schema = body?.schema || body || {};
59
+ const titles = Object.keys(schema)
60
+ .reduce((acc, curr) => Object.assign(acc, { [curr]: schema[curr].title || schema[curr].ua }), {});
61
+ const cls = Object.keys(schema)
62
+ .filter(el => schema[el]?.data)
63
+ .reduce((acc, curr) => Object.assign(acc, { [curr]: schema[curr].data }), {});
63
64
 
64
65
  if (data) {
65
66
  await metaFormat({ rows: [data], cls, sufix: false });
@@ -68,7 +69,7 @@ export default async function logChanges({
68
69
  const newObj = Object.fromEntries(Object.entries(data || {}).map(el => ([[titles[el[0]] || el[0]], el[1]])));
69
70
  const changesData = Object.keys(newObj || {}).map(el => ({
70
71
  change_id: changeId,
71
- entity_key: titles[el] || el,
72
+ entity_key: titles[el[0]] || el,
72
73
  value_old: getValue(old?.[el]),
73
74
  value_new: type === 'DELETE' ? null : getValue(newObj?.[el]),
74
75
  uid,
@@ -7,7 +7,7 @@ export default async function getMeta(opt) {
7
7
  const pg = opt?.pg || getPG({ name: 'client' });
8
8
  const table = opt?.table || opt;
9
9
 
10
- if (data[table]) return data[table];
10
+ if (data[pg.options.database]?.[table]) return data[pg.options.database][table];
11
11
 
12
12
  if (!pg.tlist?.includes(table?.replace?.(/"/g, ''))) {
13
13
  return { error: `${table} - not found`, status: 400 };
@@ -25,9 +25,23 @@ export default async function getMeta(opt) {
25
25
 
26
26
  const geomAttr = fields.find((el) => pg.pgType?.[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
27
27
 
28
+ const dbColumns = await pg.query(`select json_object_agg(
29
+ attname,
30
+ json_build_object(
31
+ 'title', pg_catalog.col_description(attrelid,attnum)
32
+ /*,'type', atttypid::regtype */
33
+ )
34
+ ) from pg_catalog.pg_attribute a
35
+ where attrelid=$1::regclass and attnum>0`, [table]).then(el => el.rows?.[0]?.json_object_agg || {});
36
+
37
+ fields.forEach(el => Object.assign(el, { ...dbColumns[el.name] || {} }));
38
+
28
39
  const res = {
29
40
  pk, columns: fields, geom: geomAttr, view: pg.relkinds?.[table] === 'v',
30
41
  };
31
- data[table] = res;
42
+ if (!data[pg.options.database]) {
43
+ data[pg.options.database] = {};
44
+ }
45
+ data[pg.options.database][table] = res;
32
46
  return res;
33
47
  }
@@ -3,14 +3,17 @@ import {
3
3
  } from '../../../../utils.js';
4
4
 
5
5
  export default async function deleteCrud(req) {
6
- const { user, params = {} } = req || {};
6
+ const { user, params = {}, headers = {} } = req || {};
7
+
7
8
  const hookData = await applyHook('preDelete', {
8
9
  table: params?.table, id: params?.id, user,
9
10
  });
11
+
10
12
  if (hookData?.message && hookData?.status) {
11
13
  return { message: hookData?.message, status: hookData?.status };
12
14
  }
13
15
 
16
+ const { referer } = headers;
14
17
  const tokenData = await getToken({
15
18
  uid: user.uid, token: params.table, json: 1,
16
19
  });
@@ -29,7 +32,7 @@ export default async function deleteCrud(req) {
29
32
  if (!id) return { status: 404, message: 'id is required' };
30
33
 
31
34
  const data = await dataDelete({
32
- table, id, uid: user?.uid, template: tokenData?.template || tokenData?.table,
35
+ table, id, uid: user?.uid, tokenData, referer,
33
36
  });
34
37
 
35
38
  return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
@@ -5,13 +5,17 @@ import {
5
5
 
6
6
  export default async function insert(req) {
7
7
  const {
8
- user = {}, params = {}, body = {},
8
+ user = {}, params = {}, body = {}, headers = {},
9
9
  } = req || {};
10
10
  if (!user) return { message: 'access restricted', status: 403 };
11
+
11
12
  const hookData = await applyHook('preInsert', { table: params?.table, user });
13
+
12
14
  if (hookData?.message && hookData?.status) {
13
15
  return { message: hookData?.message, status: hookData?.status };
14
16
  }
17
+
18
+ const { referer } = headers;
15
19
  const tokenData = await getToken({
16
20
  uid: user?.uid, token: params.table, mode: 'a', json: 1,
17
21
  });
@@ -57,7 +61,8 @@ export default async function insert(req) {
57
61
  table: loadTemplate?.table || table,
58
62
  data: body,
59
63
  uid: user?.uid,
60
- template: tokenData?.template || tokenData?.table,
64
+ tokenData,
65
+ referer,
61
66
  });
62
67
  if (!res) return { message: 'nothing added ' };
63
68
 
@@ -75,7 +80,7 @@ export default async function insert(req) {
75
80
  const objId = body[schema[key].parent_id] || req.body?.id || res?.rows?.[0]?.[schema[key].parent_id] || pkey;
76
81
  const extraRows = await Promise.all(body[key].map(async (row) => {
77
82
  const extraRes = await dataInsert({
78
- table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid: user?.uid, template: tokenData?.template,
83
+ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid: user?.uid, tokenData, referer,
79
84
  });
80
85
  return extraRes?.rows?.[0];
81
86
  }));
@@ -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, template: params?.table })],
82
+ ids: [JSON.stringify({ id, table: tableName, form: loadTable.form })],
83
83
  uid: user.uid,
84
84
  array: 1,
85
85
  })[0];
@@ -6,8 +6,9 @@ import insert from './insert.js';
6
6
 
7
7
  export default async function update(req) {
8
8
  const {
9
- pg = pgClients.client, user, params = {}, body = {}, unittest,
9
+ pg = pgClients.client, user, params = {}, body = {}, headers = {}, unittest,
10
10
  } = req;
11
+
11
12
  if (!user) return { message: 'access restricted', status: 403 };
12
13
  const hookData = await applyHook('preUpdate', {
13
14
  table: params?.table, id: params?.id, user,
@@ -16,6 +17,8 @@ export default async function update(req) {
16
17
  if (hookData?.message && hookData?.status) {
17
18
  return { message: hookData?.message, status: hookData?.status };
18
19
  }
20
+
21
+ const { referer } = headers;
19
22
  const tokenData = await getToken({
20
23
  uid: user.uid, token: body.token || params.table, mode: 'w', json: 1,
21
24
  });
@@ -64,7 +67,8 @@ export default async function update(req) {
64
67
  id,
65
68
  data: body,
66
69
  uid,
67
- template: tokenData?.template || tokenData?.table,
70
+ tokenData,
71
+ referer,
68
72
  });
69
73
 
70
74
  // admin.custom_column
@@ -83,7 +87,7 @@ export default async function update(req) {
83
87
  // insert new extra data
84
88
  if (Array.isArray(body[key]) && body[key]?.length) {
85
89
  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 || tokenData?.table });
90
+ const extraRes = await dataInsert({ table: schema[key].table, data: { ...row, [schema[key].parent_id]: objId }, uid, tokenData, referer });
87
91
  return extraRes?.rows?.[0];
88
92
  }));
89
93
  Object.assign(res.extra, { [key]: extraRows.filter((el) => el) });