@opengis/fastify-table 1.3.67 → 1.3.68

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.3.67",
3
+ "version": "1.3.68",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -12,7 +12,7 @@ import getDBParams from './getDBParams.js';
12
12
 
13
13
  function getPG(param) {
14
14
  const dbListParams = dblist.find(el => el.key === param?.key)
15
- || dblist.find(el => el.database === (param?.db || param?.database || param));
15
+ || dblist.find(el => el.database === (param?.db || param?.database || param) && el.port === param?.port);
16
16
  const {
17
17
  user, password, host, port, db, database, name: origin,
18
18
  } = dbListParams ?? (typeof param === 'string' ? getDBParams(param) : param || {});
@@ -12,7 +12,7 @@ import getDBParams from './getDBParams.js';
12
12
 
13
13
  async function getPGAsync(param) {
14
14
  const dbListParams = dblist.find(el => el.key === param?.key)
15
- || dblist.find(el => el.database === (param?.db || param?.database || param));
15
+ || dblist.find(el => el.database === (param?.db || param?.database || param) && el.port === param?.port);
16
16
  const {
17
17
  user, password, host, port, db, database, name: origin,
18
18
  } = dbListParams ?? (typeof param === 'string' ? getDBParams(param) : param || {});
@@ -29,8 +29,6 @@ async function getPGAsync(param) {
29
29
  statement_timeout: config.pg?.statement_timeout || 10000,
30
30
  };
31
31
 
32
- if (!dbConfig.database) { return null; }
33
-
34
32
  pgClients[name] = new pg.Pool(dbConfig);
35
33
 
36
34
  await init(pgClients[name]);
@@ -1,9 +1,8 @@
1
1
  import path from 'node:path';
2
- import { fileURLToPath } from 'url';
3
2
  import { existsSync, readFileSync } from 'node:fs';
4
3
 
5
4
  import {
6
- config, getPG, getTemplate, getSelectMeta, getMeta, applyHook, getSelectVal,
5
+ config, getPG, getTemplate, getSelectMeta, getMeta, applyHook, getSelectVal, logger,
7
6
  } from '../../../../utils.js';
8
7
 
9
8
  const limit = 50;
@@ -13,8 +12,6 @@ const headers = {
13
12
  'Cache-Control': 'no-cache',
14
13
  };
15
14
 
16
- const dirname = path.dirname(fileURLToPath(import.meta.url));
17
-
18
15
  function getTableColumnMeta(table, column, filtered) {
19
16
  const original = {
20
17
  true: `with c(id,text) as (select ${column}, ${column} from ${table} group by ${column}) select id, text from c`,
@@ -70,7 +67,9 @@ export default async function suggest(req) {
70
67
 
71
68
  const meta = table && column
72
69
  ? getTableColumnMeta(table, column, query?.key || query?.val)
73
- : await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache, parent });
70
+ : await getSelectMeta({
71
+ pg: pg1, name: selectName, nocache: query?.nocache, parent,
72
+ });
74
73
 
75
74
  if (meta?.minLength && query.key && query.key.length < meta?.minLength) {
76
75
  return { message: `min length: ${meta.minLength}` };
@@ -93,10 +92,10 @@ export default async function suggest(req) {
93
92
 
94
93
  const { columns = [] } = await getMeta({ pg, table: loadTable?.table || tableName });
95
94
 
96
- const column = columns.find(el => el.name === query.column);
95
+ const column1 = columns.find(el => el.name === query.column);
97
96
  const args = { table: tableName };
98
- if (!column) return [];
99
- const sqlCls = `select array_agg(distinct value)::text[] from (select ${pg.pgType?.[column.dataTypeID]?.includes('[]') ? `unnest(${column.name})` : `${column.name}`} as value from ${(loadTable?.table || tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || '1=1'})q`;
97
+ if (!column1) return [];
98
+ const sqlCls = `select array_agg(distinct value)::text[] from (select ${pg.pgType?.[column1.dataTypeID]?.includes('[]') ? `unnest(${column1.name})` : `${column1.name}`} as value from ${(loadTable?.table || tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || '1=1'})q`;
100
99
 
101
100
  if (query.sql && (config.local || user?.user_type?.includes?.('admin'))) {
102
101
  return sqlCls;
@@ -113,6 +112,20 @@ export default async function suggest(req) {
113
112
 
114
113
  const data2 = data1.filter((el) => el.id && vals.includes(el.id.toString()));
115
114
  const data = data2.slice(0, Math.min(query.limit || limit, limit));
115
+
116
+ if (config.debug) {
117
+ logger.file('suggest/debug', {
118
+ type: 1,
119
+ loadTable: loadTable?.table,
120
+ tableName,
121
+ column: column?.name,
122
+ data,
123
+ data1,
124
+ data2,
125
+ query,
126
+ });
127
+ }
128
+
116
129
  return {
117
130
  time: Date.now() - time,
118
131
  limit: Math.min(query.limit || limit, limit),
@@ -122,7 +135,7 @@ export default async function suggest(req) {
122
135
  sql: (config.local || user?.user_type?.includes?.('admin')) ? sqlCls : undefined,
123
136
  data,
124
137
  };
125
- };
138
+ }
126
139
  }
127
140
 
128
141
  if (arr) {
@@ -131,6 +144,16 @@ export default async function suggest(req) {
131
144
  ? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
132
145
  : arr;
133
146
  const data = data1.slice(0, Math.min(query.limit || limit, limit));
147
+
148
+ if (config.debug) {
149
+ logger.file('suggest/debug', {
150
+ type: 2,
151
+ key: query.key,
152
+ data,
153
+ data1,
154
+ });
155
+ }
156
+
134
157
  return {
135
158
  time: Date.now() - time,
136
159
  limit: Math.min(query.limit || limit, limit),
@@ -183,6 +206,17 @@ export default async function suggest(req) {
183
206
  data.forEach(el => Object.assign(el, { text: clsData?.[el.id || ''] || el.id }));
184
207
  }
185
208
 
209
+ if (config.debug) {
210
+ logger.file('suggest/debug', {
211
+ type: 3,
212
+ sel: query.sel,
213
+ data,
214
+ dataNew,
215
+ sqlSuggest,
216
+ ids,
217
+ });
218
+ }
219
+
186
220
  const message = {
187
221
  time: Date.now() - time,
188
222
  limit: Math.min(query.limit || meta.limit || limit, limit),