@opengis/fastify-table 1.1.146 → 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.146",
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,14 +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
33
 
32
- const tableMeta = await getMeta({ table: body?.table || table });
33
- const columnExists = (body?.columns || tableMeta?.columns)?.find((col) => col?.name === column);
34
+ const tableMeta = await getMeta({ pg: pg1, table: tableName });
35
+ const columnExists = (hookBody?.columns || body?.columns || tableMeta?.columns)?.find((col) => col?.name === column);
34
36
 
35
37
  if (table && (!column || !columnExists)) {
36
38
  return { status: 400, message: 'param name is invalid: 2' };
@@ -42,7 +44,7 @@ export default async function suggest(req) {
42
44
 
43
45
  const meta = table && column
44
46
  ? {
45
- 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`,
46
48
  searchQuery: '(lower("text") ~ $1 )',
47
49
  }
48
50
  : await getSelectMeta({ name: selectName, nocache: query?.nocache });
@@ -65,18 +67,19 @@ export default async function suggest(req) {
65
67
 
66
68
  if (arr && query.token && query.column) {
67
69
  const loadTable = await getTemplate('table', query.token);
68
- const { columns = [] } = await getMeta({ table: loadTable?.table || query.token });
70
+
71
+ const { columns = [] } = await getMeta({ pg, table: tableName });
69
72
 
70
73
  const column = columns.find(el => el.name === query.column);
71
- const args = { table: loadTable?.table || query.token };
74
+ const args = { table: tableName };
72
75
 
73
- 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`;
74
77
 
75
78
  if (query.sql && (config.local || user?.user_type?.includes?.('admin'))) {
76
79
  return sqlCls;
77
80
  }
78
81
 
79
- if (pg.pk?.[loadTable?.table || query.token] && column?.name) {
82
+ if (pg.pk?.[tableName] && column?.name) {
80
83
  const vals = await pg.queryCache(sqlCls, args).then(el => el.rows?.[0]?.array_agg || []);
81
84
  // console.log(vals);
82
85
 
@@ -126,7 +129,10 @@ export default async function suggest(req) {
126
129
  const where = [search, val, meta.pk ? `${meta.pk} is not null` : null].filter((el) => el).join(' and ') || 'true';
127
130
 
128
131
  const loadTable = await getTemplate('table', query.token);
129
- 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 });
130
136
 
131
137
  const filterColumn = query.column ? columns.find(el => el.name === query.column)?.name : null;
132
138
  const filter = query.token && pg.pk?.[loadTable?.table || query.token] && filterColumn