@opengis/fastify-table 1.1.96 → 1.1.97

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.96",
3
+ "version": "1.1.97",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -14,7 +14,7 @@ export default async function getSelect(name, pg = pgClients.client) {
14
14
  const clsDataGIT = await getTemplate(['cls', 'select'], name);
15
15
  const { type } = !clsDataGIT && pg.pk?.['admin.cls'] ? await pg.query('select type from admin.cls where parent is null and name=$1 limit 1', [name]).then(el => el.rows?.[0] || {}) : {};
16
16
  const q = !clsDataGIT && type && ['sql', 'json'].includes(type) ? sqls[type] : undefined;
17
- const clsDataDB = q ? await pg.query(q, [name]).then(el => el.rows?.[0]?.data) : {};
17
+ const clsDataDB = q ? await pg.query(q, [name]).then(el => el.rows?.[0]?.data) : undefined;
18
18
  const clsData = clsDataGIT || clsDataDB;
19
19
  if (!clsData) return null;
20
20
 
@@ -63,8 +63,15 @@ export default async function gisIRColumn({
63
63
  pg, name: cls, values: rows.map((el) => el.id), ar: 1,
64
64
  });
65
65
  rows.forEach((el) => {
66
- const data = select?.arr ? select.arr?.find((item) => item.id?.toString() === el.id?.toString()) : undefined;
67
- Object.assign(el, data || {});
66
+ if (Array.isArray(select)) {
67
+ Object.assign(el, select.find((item) => item.id?.toString() === el.id?.toString()) || {});
68
+ }
69
+ else if (typeof select?.[el.id] === 'string') {
70
+ Object.assign(el, { text: select?.[el.id] });
71
+ }
72
+ else {
73
+ Object.assign(el, select?.[el.id] || {});
74
+ }
68
75
  });
69
76
  return {
70
77
  time: Date.now() - time,
@@ -3,15 +3,18 @@ import pgClients from '../../../pg/pgClients.js';
3
3
  import redis from '../../../redis/client.js';
4
4
 
5
5
  const selectIds = {};
6
- export default async function getSelectVal({ pg = pgClients.client, name, values: valuesOrigin }) {
6
+ export default async function getSelectVal({
7
+ pg = pgClients.client, name, values: valuesOrigin, ar = false,
8
+ }) {
7
9
  if (!valuesOrigin?.length) return null;
8
10
  const values = valuesOrigin.map(el => el.toString());
9
11
  const cls = await getSelect(name, pg);
10
12
 
11
13
  // === array ===
12
14
  if (cls?.arr && Array.isArray(cls?.arr)) {
13
- // console.log(name, cls?.arr, values);
14
- return cls.arr.filter(el => values.includes(el.id.toString())).reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {});
15
+ const resultArr = cls.arr.filter(el => values.includes(el.id.toString()));
16
+ if (ar) return resultArr;
17
+ return resultArr.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {});
15
18
  }
16
19
 
17
20
  // === sql ===
@@ -30,9 +33,18 @@ export default async function getSelectVal({ pg = pgClients.client, name, values
30
33
  const q = `with c(id,text) as (select * from (${cls.sql})q where ${id} = any('{${filteredValues}}')) select * from c`;
31
34
  const data = filteredValues.length ? await pg.query(q).then(el => el.rows) : [];
32
35
 
33
- const clsAr = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
36
+ const clsObj = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
37
+
34
38
  if (data?.length) {
35
- redis.hmset(key, clsAr);
39
+ redis.hmset(key, clsObj);
40
+ }
41
+
42
+ if (ar) {
43
+ return Object.keys(clsObj).reduce((acc, el) => {
44
+ acc.push({ id: el, text: typeof clsObj[el] === 'string' ? clsObj[el] : clsObj[el]?.text, color: clsObj[el]?.color });
45
+ return acc;
46
+ }, []);
36
47
  }
37
- return clsAr;
48
+
49
+ return clsObj;
38
50
  }