@opengis/fastify-table 1.1.131 → 1.1.132

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.131",
3
+ "version": "1.1.132",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -133,7 +133,8 @@ function formatValue({
133
133
 
134
134
  if (['~', '='].includes(operator)) {
135
135
  const operator1 = (filterType === 'text' && (filter?.id || filter?.name) && operator === '=' ? '~' : operator);
136
- const match = (operator1 === '=' || filterType === 'autocomplete') ? `='${value}'` : `ilike '%${value}%'`;
136
+ const matchNull = { null: 'is null', notnull: 'is not null' }[value];
137
+ const match = matchNull || ((operator1 === '=' || filterType === 'autocomplete') ? `='${value}'` : `ilike '%${value}%'`);
137
138
  if (extra && pk) {
138
139
  const query = data && sql
139
140
  ? `${pk} in (select object_id from crm.extra_data where property_key='${name}' and value_text in ( ( with q(id,name) as (${sql}) select id from q where ${filterType === 'autocomplete' ? 'id' : 'name'} ${match})))`
@@ -1,5 +1,5 @@
1
1
  import {
2
- config, getPG, getTemplate, getSelectMeta, getMeta,
2
+ config, getPG, getTemplate, getSelectMeta, getMeta, handlebarsSync,
3
3
  } from '../../../../utils.js';
4
4
 
5
5
  const limit = 50;
@@ -61,6 +61,42 @@ export default async function suggest(req) {
61
61
 
62
62
  const { arr, searchQuery } = meta;
63
63
 
64
+ if (arr && query.token && query.column) {
65
+ const loadTable = await getTemplate('table', query.token);
66
+ const { columns = [] } = await getMeta({ table: loadTable?.table || query.token });
67
+
68
+ const column = columns.find(el => el.name === query.column);
69
+ const args = { table: loadTable?.table || query.token };
70
+
71
+ const sqlCls = `select array_agg(distinct value) from (select ${pg.pgType[column.dataTypeID].includes('[]') ? `unnest(${column.name})` : `${column.name}`} as value from ${(loadTable?.table || query.token).replace(/'/g, "''")} where ${loadTable?.query || '1=1'})q`;
72
+
73
+ if (query.sql && (config.local || user?.user_type?.includes?.('admin'))) {
74
+ return sqlCls;
75
+ }
76
+
77
+ if (pg.pk?.[loadTable?.table || query.token] && column?.name) {
78
+ const vals = await pg.queryCache(sqlCls, args).then(el => el.rows?.[0]?.array_agg || []);
79
+ // console.log(vals);
80
+
81
+ const lower = query.key?.toLowerCase();
82
+ const data1 = query.key || query.val
83
+ ? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
84
+ : arr;
85
+
86
+ const data2 = data1.filter((el) => vals.includes(el.id));
87
+ const data = data2.slice(0, Math.min(query.limit || limit, limit));
88
+ return {
89
+ time: Date.now() - time,
90
+ limit: Math.min(query.limit || limit, limit),
91
+ count: data.length,
92
+ total: arr.length,
93
+ mode: 'array',
94
+ sql: (config.local || user?.user_type?.includes?.('admin')) ? sqlCls : undefined,
95
+ data,
96
+ };
97
+ };
98
+ }
99
+
64
100
  if (arr) {
65
101
  const lower = query.key?.toLowerCase();
66
102
  const data1 = query.key || query.val
@@ -97,7 +133,7 @@ export default async function suggest(req) {
97
133
 
98
134
  const sqlSuggest = `with c(id,text) as ( ${meta.original.replace(/{{parent}}/gi, parent)} where ${where} order by 2) select * from c where ${filter} limit ${Math.min(query.limit || meta.limit || limit, limit)}`.replace(/{{uid}}/g, user?.uid || '0');
99
135
 
100
- if (query.sql && config.local) {
136
+ if (query.sql && (config.local || user?.user_type?.includes?.('admin'))) {
101
137
  return sqlSuggest;
102
138
  }
103
139
 
@@ -105,7 +141,7 @@ export default async function suggest(req) {
105
141
  const { rows: dataNew } = await pg.query(sqlSuggest, query.key ? [`${query.key.toLowerCase()}`] : []);
106
142
  // const { rows: dataNew1 } = dataNew.length < limit ? await pg.query(sqlSuggest, query.key ? [`${query.key}`] : []) : {};
107
143
  const ids = dataNew.map((el) => el.id);
108
- const data = dataNew.concat((/* dataNew1 || */ []).filter((el) => !ids?.includes(el.id)));
144
+ const data = dataNew.concat((/* dataNew1 || */[]).filter((el) => !ids?.includes(el.id)));
109
145
 
110
146
  const message = {
111
147
  time: Date.now() - time,
@@ -114,7 +150,7 @@ export default async function suggest(req) {
114
150
  total: meta.count - 0,
115
151
  mode: 'sql',
116
152
  db: meta.db,
117
- sql: config.local ? sqlSuggest : undefined,
153
+ sql: (config.local || user?.user_type?.includes?.('admin')) ? sqlSuggest : undefined,
118
154
  data,
119
155
  };
120
156