@opengis/fastify-table 2.0.118 → 2.0.119

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.
@@ -1 +1 @@
1
- {"version":3,"file":"suggest.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/controllers/suggest.ts"],"names":[],"mappings":"AA+DA,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,gBAqXzD"}
1
+ {"version":3,"file":"suggest.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/controllers/suggest.ts"],"names":[],"mappings":"AAmEA,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,gBAsWzD"}
@@ -1,17 +1,21 @@
1
1
  import path from "node:path";
2
2
  import { existsSync, readFileSync } from "node:fs";
3
- import { config, getPG, getTemplate, getSelectMeta, getMeta, applyHook, getSelectVal, logger, getSelect, metaFormat, getColumnCLS, } from "../../../../utils.js";
3
+ import { config, getPGAsync, getTemplate, getSelectMeta, getMeta, applyHook, getSelectVal, logger, getSelect, metaFormat, getColumnCLS, pgClients, } from "../../../../utils.js";
4
4
  const defaultLimit = 50;
5
- async function getTableColumnMeta({ table, column, filtered, startsWith, key, pg, }) {
5
+ async function getTableColumnMeta({ table, template, column, selectName, filtered, startsWith, key, pg = pgClients.client, }) {
6
6
  if (!table || !column) {
7
7
  return null;
8
8
  }
9
- const loadTable = await getTemplate("table", table);
10
- const { data: clsName } = loadTable?.columns?.find?.((el) => el.name === column) || {};
11
- const { arr } = (await getSelect(clsName, pg)) || {};
9
+ const { columns, table: tableName } = template
10
+ ? await getTemplate("table", template)
11
+ : { table, columns: [] };
12
+ const { data: clsName } = selectName
13
+ ? { data: selectName }
14
+ : columns.find((el) => el.name === column) || {};
15
+ const { arr } = (await getSelect(clsName || column, pg)) || {};
12
16
  const original = filtered
13
- ? `with c(id,text) as (select ${column} as id, ${column} as text from ${loadTable?.table || table} group by ${column}) select id, text from c`
14
- : `with c(id,text) as (select ${column} as id, ${column} as text, count(*) from ${loadTable?.table || table} group by ${column} limit ${defaultLimit}) select * from c`;
17
+ ? `with c(id,text) as (select ${column} as id, ${column} as text from ${tableName} group by ${column}) select id, text from c`
18
+ : `with c(id,text) as (select ${column} as id, ${column} as text, count(*) from ${tableName} group by ${column} limit ${defaultLimit}) select * from c`;
15
19
  return {
16
20
  arr,
17
21
  original,
@@ -22,10 +26,13 @@ async function getTableColumnMeta({ table, column, filtered, startsWith, key, pg
22
26
  };
23
27
  }
24
28
  export default async function suggest(req, reply) {
25
- const { params, query, pg: pg1, user } = req;
26
- const lang = query.lang || "ua";
27
29
  const time = Date.now();
28
- const parent = query.parent || "";
30
+ const { params, user, query = {}, pg: pg1 = pgClients.client } = req;
31
+ const { lang = "ua", parent = "" } = query;
32
+ const debugMode = config.local ||
33
+ user?.user_type?.includes?.("admin") ||
34
+ process.env.NODE_ENV === "test" ||
35
+ process.env.VITEST;
29
36
  if (params?.data && params.data?.startsWith?.("hash-")) {
30
37
  const filepath = path.join(process.cwd(), `/log/suggest/${params.data.replace("hash-", "")}.json`);
31
38
  if (existsSync(filepath)) {
@@ -33,40 +40,26 @@ export default async function suggest(req, reply) {
33
40
  params.data = `${table}:${column}`;
34
41
  }
35
42
  }
36
- const [table, column] = params.data?.includes(":")
43
+ const [table1, column1] = params.data?.includes(":")
37
44
  ? params.data.split(":")
38
45
  : [query.token, query.column];
39
46
  const selectName = query.sel || params.data;
47
+ const table = table1 || query.token;
48
+ const column = column1 || query.column;
40
49
  if (!selectName) {
41
50
  return reply.status(400).send({
42
51
  error: "name is required",
43
52
  code: 400,
44
53
  });
45
54
  }
46
- const { body: hookBody } = table || query.token
55
+ const { body: hookBody } = table
47
56
  ? (await applyHook("preSuggest", {
48
57
  pg: pg1,
49
- table: table || query.token,
58
+ table,
50
59
  })) || {}
51
60
  : {};
52
- const body = await getTemplate("table", table || query.token);
53
- const tableName = hookBody?.table || body?.table || table || query.token;
54
- if (table && !pg1.pk?.[tableName]) {
55
- return reply.status(400).send({
56
- error: "param name is invalid: 1",
57
- code: 400,
58
- });
59
- }
60
- const tableMeta = await getMeta({ pg: pg1, table: tableName });
61
- const columnExists = (hookBody?.columns ||
62
- body?.columns ||
63
- tableMeta?.columns)?.find((col) => col?.name === (column || query.column));
64
- if (table && (!column || !columnExists)) {
65
- return reply.status(400).send({
66
- error: "param name is invalid: 2",
67
- code: 400,
68
- });
69
- }
61
+ const body = await getTemplate("table", table);
62
+ const tableName = hookBody?.table || body?.table || table;
70
63
  if (query.limit && query.limit < 0) {
71
64
  return reply.status(400).send({
72
65
  error: "param limit is invalid",
@@ -75,20 +68,20 @@ export default async function suggest(req, reply) {
75
68
  }
76
69
  const meta = tableName && column
77
70
  ? await getTableColumnMeta({
78
- table,
71
+ table: tableName,
72
+ template: tableName && table && tableName !== table ? table : undefined,
79
73
  column,
80
- filtered: query?.key || query?.val,
74
+ selectName,
75
+ filtered: query.key || query.val,
81
76
  startsWith: !!query.start,
82
77
  key: query.key,
83
- pg: pg1,
84
78
  })
85
79
  : await getSelectMeta({
86
80
  name: selectName,
87
- nocache: query?.nocache,
81
+ nocache: query.nocache,
88
82
  startsWith: !!query.start,
89
83
  key: query.key,
90
84
  parent,
91
- pg: pg1,
92
85
  });
93
86
  if (meta?.minLength && query.key && query.key.length < meta?.minLength) {
94
87
  return reply.status(400).send({
@@ -97,7 +90,32 @@ export default async function suggest(req, reply) {
97
90
  });
98
91
  }
99
92
  const limit = meta?.limit || defaultLimit;
100
- const pg = meta?.db ? getPG(meta.db) : pg1;
93
+ const pg = meta?.db ? await getPGAsync(meta.db) : pg1;
94
+ if (!pg || !pg.pk || !pg.pgType) {
95
+ return reply.status(400).send({
96
+ error: "pg connection not established",
97
+ code: 400,
98
+ });
99
+ }
100
+ if (table && !pg?.pk?.[tableName]) {
101
+ return reply.status(400).send({
102
+ error: "param name is invalid: 1",
103
+ code: 400,
104
+ });
105
+ }
106
+ const columns = hookBody?.columns || body?.columns
107
+ ? hookBody?.columns || body?.columns
108
+ : await getMeta({
109
+ pg,
110
+ table: tableName,
111
+ }).then((el) => el?.columns || []);
112
+ const { name: columnName, dataTypeID } = (columns || []).find((col) => col?.name === column) || {};
113
+ if (table && (!column || !columnName)) {
114
+ return reply.status(400).send({
115
+ error: "param name is invalid: 2",
116
+ code: 400,
117
+ });
118
+ }
101
119
  if (!meta) {
102
120
  return reply.status(404).send({
103
121
  error: "Not found query select",
@@ -108,28 +126,19 @@ export default async function suggest(req, reply) {
108
126
  return meta;
109
127
  }
110
128
  const { arr, searchQuery } = meta;
111
- if (arr && query.token && query.column) {
112
- const loadTable = await getTemplate("table", query.token);
113
- const { columns = [] } = await getMeta({
114
- pg,
115
- table: loadTable?.table || tableName,
116
- });
117
- const column1 = columns.find((el) => el.name === query.column);
118
- const args = { table: tableName };
119
- if (!column1)
120
- return [];
129
+ if (arr && table && column) {
121
130
  const sqlCls = query.count
122
- ? `select value, count(*) from (select ${pg.pgType?.[column1.dataTypeID]?.includes("[]")
123
- ? `unnest(${column1.name})`
124
- : `${column1.name}`} as value from ${(loadTable?.table || tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || "1=1"})q group by value`
125
- : `select array_agg(distinct value)::text[] from (select ${pg.pgType?.[column1.dataTypeID]?.includes("[]")
126
- ? `unnest(${column1.name})`
127
- : `${column1.name}`} as value from ${(loadTable?.table || tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || "1=1"})q`;
128
- if (query.sql && (config.local || user?.user_type?.includes?.("admin"))) {
131
+ ? `select value, count(*) from (select ${pg.pgType?.[dataTypeID]?.includes("[]")
132
+ ? `unnest(${columnName})`
133
+ : `${columnName}`} as value from ${tableName.replace(/'/g, "''")} where ${hookBody?.query || body?.query || "1=1"})q group by value`
134
+ : `select array_agg(distinct value)::text[] from (select ${pg.pgType?.[dataTypeID]?.includes("[]")
135
+ ? `unnest(${columnName})`
136
+ : `${columnName}`} as value from ${tableName.replace(/'/g, "''")} where ${hookBody?.query || body?.query || "1=1"})q`;
137
+ if (query.sql && debugMode) {
129
138
  return sqlCls;
130
139
  }
131
- if (pg.pk?.[loadTable?.table || tableName] && column1?.name) {
132
- const qRes = await pg.queryCache(sqlCls, args);
140
+ if (tableName && pg?.pk?.[tableName] && columnName) {
141
+ const qRes = await pg.queryCache(sqlCls, { table: tableName });
133
142
  const vals = (query.count
134
143
  ? qRes.rows?.map?.((el) => el.value?.toString?.())
135
144
  : qRes.rows?.[0]?.array_agg) || [];
@@ -152,9 +161,9 @@ export default async function suggest(req, reply) {
152
161
  if (config.debug) {
153
162
  logger.file("suggest/debug", {
154
163
  type: 1,
155
- loadTable: loadTable?.table,
164
+ table,
156
165
  tableName,
157
- column: column?.name,
166
+ column: columnName,
158
167
  data,
159
168
  data1,
160
169
  data2,
@@ -167,9 +176,7 @@ export default async function suggest(req, reply) {
167
176
  count: data.length,
168
177
  total: arr.length,
169
178
  mode: "array",
170
- sql: config.local || user?.user_type?.includes?.("admin")
171
- ? sqlCls
172
- : undefined,
179
+ sql: debugMode ? sqlCls : undefined,
173
180
  data,
174
181
  };
175
182
  }
@@ -211,22 +218,13 @@ export default async function suggest(req, reply) {
211
218
  const where = [search, val, meta.pk ? `${meta.pk} is not null` : null]
212
219
  .filter(Boolean)
213
220
  .join(" and ") || "true";
214
- const loadTable = await getTemplate("table", query.token);
215
- const tableName1 = hookBody?.table || loadTable?.table || query.token;
216
- const { columns = [] } = await getMeta({ pg: pg1, table: tableName1 });
217
- const { name: filterColumn, dataTypeID } = query.column
218
- ? columns.find((el) => el.name === query.column) || {}
219
- : {};
220
- const filter = query.token &&
221
- pg.pk?.[loadTable?.table || query.token] &&
222
- filterColumn &&
223
- dataTypeID
221
+ const filter = table && pg.pk?.[table] && columnName && dataTypeID
224
222
  ? `id in (select ${pg.pgType[dataTypeID]?.includes("[]")
225
- ? `unnest(${filterColumn})`
226
- : filterColumn} from ${(loadTable?.table || query.token).replace(/'/g, "''")})`
223
+ ? `unnest(${columnName})`
224
+ : columnName} from ${table.replace(/'/g, "''")})`
227
225
  : "true";
228
226
  const sqlSuggest = `with c(id,text) as ( ${meta.original.replace(/{{parent}}/gi, parent)} where ${where} ${meta.original.includes("order by") ? "" : "order by 2"}) select * from c where ${filter} limit ${Math.min(query.limit || meta.limit || limit, limit)}`.replace(/{{uid}}/g, user?.uid || "0");
229
- if (query.sql && (config.local || user?.user_type?.includes?.("admin"))) {
227
+ if (query.sql && debugMode) {
230
228
  return sqlSuggest;
231
229
  }
232
230
  // query
@@ -248,9 +246,7 @@ export default async function suggest(req, reply) {
248
246
  total: meta.count - 0,
249
247
  mode: type === "cls" ? "array" : "sql",
250
248
  db: meta.db,
251
- sql: config.local || user?.user_type?.includes?.("admin")
252
- ? sqlSuggest
253
- : undefined,
249
+ sql: debugMode ? sqlSuggest : undefined,
254
250
  data: data.map((el) => ({
255
251
  count: el.count,
256
252
  id: el.id,
@@ -284,9 +280,7 @@ export default async function suggest(req, reply) {
284
280
  total: meta.count - 0,
285
281
  mode: "sql",
286
282
  db: meta.db,
287
- sql: config.local || user?.user_type?.includes?.("admin")
288
- ? sqlSuggest
289
- : undefined,
283
+ sql: debugMode ? sqlSuggest : undefined,
290
284
  data,
291
285
  };
292
286
  return message;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.0.118",
3
+ "version": "2.0.119",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [