@opengis/fastify-table 1.1.145 → 1.1.147

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.145",
3
+ "version": "1.1.147",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,5 +1,5 @@
1
1
  import {
2
- config, getPG, getTemplate, getSelectMeta, getMeta, handlebarsSync,
2
+ config, getPG, getTemplate, getSelectMeta, getMeta, applyHook,
3
3
  } from '../../../../utils.js';
4
4
 
5
5
  const limit = 50;
@@ -23,12 +23,16 @@ export default async function suggest(req) {
23
23
 
24
24
  if (!selectName) return { status: 400, message: 'name is required' };
25
25
 
26
+ const { body: hookBody } = table || query.token ? await applyHook('preSuggest', { pg: pg1, table: table || query.token }) || {} : {};
26
27
  const body = await getTemplate('table', table);
28
+ const tableName = hookBody?.table || body?.table || table || query.token;
27
29
 
28
- if (table && !pg1.pk[body?.table || table]) {
30
+ if (table && !pg1.pk?.[tableName]) {
29
31
  return { status: 400, message: 'param name is invalid: 1' };
30
32
  }
31
- const columnExists = body?.columns?.find((col) => col?.name === column);
33
+
34
+ const tableMeta = await getMeta({ pg: pg1, table: tableName });
35
+ const columnExists = (hookBody?.columns || body?.columns || tableMeta?.columns)?.find((col) => col?.name === column);
32
36
 
33
37
  if (table && (!column || !columnExists)) {
34
38
  return { status: 400, message: 'param name is invalid: 2' };
@@ -40,7 +44,7 @@ export default async function suggest(req) {
40
44
 
41
45
  const meta = table && column
42
46
  ? {
43
- original: `with c(id,text) as (select row_number() over(), ${column} from ${body?.table || table} group by ${column} limit ${limit}) select * from c`,
47
+ original: `with c(id,text) as (select row_number() over(), ${column} from ${tableName} group by ${column} limit ${limit}) select * from c`,
44
48
  searchQuery: '(lower("text") ~ $1 )',
45
49
  }
46
50
  : await getSelectMeta({ name: selectName, nocache: query?.nocache });
@@ -63,18 +67,19 @@ export default async function suggest(req) {
63
67
 
64
68
  if (arr && query.token && query.column) {
65
69
  const loadTable = await getTemplate('table', query.token);
66
- const { columns = [] } = await getMeta({ table: loadTable?.table || query.token });
70
+
71
+ const { columns = [] } = await getMeta({ pg, table: tableName });
67
72
 
68
73
  const column = columns.find(el => el.name === query.column);
69
- const args = { table: loadTable?.table || query.token };
74
+ const args = { table: tableName };
70
75
 
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`;
76
+ const sqlCls = `select array_agg(distinct value) from (select ${pg.pgType[column.dataTypeID].includes('[]') ? `unnest(${column.name})` : `${column.name}`} as value from ${(tableName).replace(/'/g, "''")} where ${hookBody?.query || loadTable?.query || '1=1'})q`;
72
77
 
73
78
  if (query.sql && (config.local || user?.user_type?.includes?.('admin'))) {
74
79
  return sqlCls;
75
80
  }
76
81
 
77
- if (pg.pk?.[loadTable?.table || query.token] && column?.name) {
82
+ if (pg.pk?.[tableName] && column?.name) {
78
83
  const vals = await pg.queryCache(sqlCls, args).then(el => el.rows?.[0]?.array_agg || []);
79
84
  // console.log(vals);
80
85
 
@@ -121,10 +126,13 @@ export default async function suggest(req) {
121
126
  // return meta;
122
127
 
123
128
  const val = query.val ? ` ${meta.pk}=any('{${query.val.replace(/'/g, "''")}}')` : '';
124
- const where = [search, val, `${meta.pk} is not null`].filter((el) => el).join(' and ') || 'true';
129
+ const where = [search, val, meta.pk ? `${meta.pk} is not null` : null].filter((el) => el).join(' and ') || 'true';
125
130
 
126
131
  const loadTable = await getTemplate('table', query.token);
127
- const { columns = [] } = await getMeta({ table: loadTable?.table || query.token });
132
+
133
+ const tableName1 = hookBody?.table || loadTable?.table || query.token;
134
+
135
+ const { columns = [] } = await getMeta({ pg: pg1, table: tableName1 });
128
136
 
129
137
  const filterColumn = query.column ? columns.find(el => el.name === query.column)?.name : null;
130
138
  const filter = query.token && pg.pk?.[loadTable?.table || query.token] && filterColumn