@opengis/fastify-table 1.2.75 → 1.2.77

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.2.75",
3
+ "version": "1.2.77",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,8 +1,8 @@
1
1
  import routeData from '../../../routes/table/controllers/data.js';
2
2
 
3
- export default async function getData({ id, table, pg, filter, limit, page, search, user, sql }, reply, called) {
3
+ export default async function getData({ id, table, pg, filter, limit, page, search, user, sql, contextQuery }, reply, called) {
4
4
  const params = { table, id };
5
5
  const query = { filter, limit, page, search, sql };
6
- const result = await routeData({ pg, params, query, user, sql }, reply, called);
6
+ const result = await routeData({ pg, params, query, user, contextQuery }, reply, called);
7
7
  return result;
8
8
  }
@@ -44,7 +44,7 @@ export default async function getFilterSQL({
44
44
  // console.log('extra getFilterSQL', extraDataTable, pg.pk?.[extraDataTable]);
45
45
 
46
46
  // check sql inline fields count
47
- if (!checkInline[body?.table] && body?.sql?.length && body.table) {
47
+ if (body?.table && !checkInline[body?.table] && body?.sql?.length) {
48
48
  const filterSql = body.sql.filter(el => !el?.disabled && (el.inline ?? true));
49
49
  const sqlTable = filterSql.map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
50
50
  const d = await Promise.all(filterSql.map((el, i) => pg.query(`select ${el.name || `t${i}`}.* from(select * from ${body.table})t ${sqlTable} limit 0`).then(el => el.fields)))
@@ -15,7 +15,7 @@ const checkInline = {};
15
15
  const maxLimit = 100;
16
16
  export default async function dataAPI(req, reply, called) {
17
17
  const {
18
- pg = pgClients.client, params, query = {}, user = {},
18
+ pg = pgClients.client, params, query = {}, user = {}, contextQuery,
19
19
  } = req;
20
20
 
21
21
  const time = Date.now();
@@ -78,7 +78,7 @@ export default async function dataAPI(req, reply, called) {
78
78
  const cardSqlFiltered = hookData?.id || params.id ? (cardSql?.filter?.((el) => !el?.disabled && el?.name && el?.sql?.replace) || []) : [];
79
79
  const cardSqlTable = cardSqlFiltered.length ? cardSqlFiltered.map((el, i) => ` left join lateral (${el.sql.replace('{{uid}}', uid)}) ct${i} on 1=1 `).join('\n') || '' : '';
80
80
 
81
- const sqlInline = loadTable.sql?.filter?.(el => el.inline).map(el => `,(${el.sql})`).join('') || '';
81
+ const sqlInline = loadTable?.sql?.filter?.(el => el.inline)?.map(el => `,(${el.sql})`)?.join('') || '';
82
82
  const { fields = [] } = pg.queryCache ? await pg.queryCache(`select * ${sqlInline} from ${table} t ${sqlTable} ${cardSqlTable} limit 0`) : {};
83
83
  const dbColumnsTable = fields.map(el => el.name);
84
84
  const cols = columns.filter((el) => el.name !== 'geom' && dbColumnsTable.includes(el.name)).map((el) => el.name || el).join(',');
@@ -102,7 +102,7 @@ export default async function dataAPI(req, reply, called) {
102
102
  const checkFilter = [query.filter, query.search, query.state, query.custom].filter((el) => el).length;
103
103
  const fData = checkFilter ? await getFilterSQL({
104
104
  pg,
105
- table: params.table,
105
+ table: loadTable ? params.table : table,
106
106
  filter: query.filter,
107
107
  search: query.search,
108
108
  state: query.state,
@@ -130,7 +130,7 @@ export default async function dataAPI(req, reply, called) {
130
130
  const bbox = meta?.bbox && queryBbox.filter((el) => !Number.isNaN(el))?.length === 4 ? `${meta.bbox} && 'box(${queryBbox[0]} ${queryBbox[1]},${queryBbox[2]} ${queryBbox[3]})'::box2d ` : undefined;
131
131
 
132
132
  const interfaceQuery = params?.query ? await handlebars.compile(params?.query)({ user, uid }) : undefined;
133
- const where = [(tokenData?.id || hookData?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable?.query, tokenData?.query, fData.q, search, accessQuery || '1=1', bbox, queryPolyline, interfaceQuery].filter((el) => el).filter((el) => (user?.user_type === 'superadmin' ? !el.includes('{{uid}}') : true));
133
+ const where = [(tokenData?.id || hookData?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable?.query, tokenData?.query, fData.q, search, accessQuery || '1=1', contextQuery, bbox, queryPolyline, interfaceQuery].filter((el) => el).filter((el) => (user?.user_type === 'superadmin' ? !el.includes('{{uid}}') : true));
134
134
 
135
135
  // const cardColumns = cardSqlFiltered.length ? `,${cardSqlFiltered.map((el) => el.name)}` : '';
136
136
  const q = `select ${pk ? `"${pk}" as id,` : ''}
@@ -146,7 +146,7 @@ export default async function dataAPI(req, reply, called) {
146
146
  .replace(/{{uid}}/g, uid);
147
147
 
148
148
  // if (user?.user_type === 'superadmin') console.log(q);
149
-
149
+ if (config.trace) console.log(q);
150
150
  if (query.sql === '1') { return q; }
151
151
 
152
152
  const { rows = [] } = await pg.query(q, (tokenData?.id || hookData?.id || params.id ? [tokenData?.id || hookData?.id || params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
@@ -172,7 +172,7 @@ export default async function dataAPI(req, reply, called) {
172
172
  count(*) FILTER(WHERE ${filterWhere.join(' and ') || 'true'})::int as filtered
173
173
  ${aggregates.length ? `,${aggregates.map((el) => `${aggColumns[el.name]}(${el.name}) FILTER(WHERE ${filterWhere.join(' and ') || 'true'}) as ${el.name}`).join(',')}` : ''}
174
174
  from (select * ${sql?.filter(el => el.inline).map(el => `,(${el.sql})`).join('') || ''} from ${table} t ${sqlTable})q
175
- where ${[loadTable?.query, tokenData?.query, accessQuery].filter(el => el).join(' and ') || 'true'} `
175
+ where ${[loadTable?.query, tokenData?.query, accessQuery, contextQuery].filter(el => el).join(' and ') || 'true'} `
176
176
  .replace(/{{uid}}/g, uid);
177
177
 
178
178
  if (query.sql === '2') { return qCount; }