@opengis/fastify-table 1.2.71 → 1.2.73

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.71",
3
+ "version": "1.2.73",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -128,7 +128,7 @@ export default async function getFilterSQL({
128
128
 
129
129
  // table
130
130
 
131
- const obj = { table: modelQuery, orderby: body?.order || body?.orderby, sqlTable, sqlInline, extraSqlColumns, q };
131
+ const obj = { body, extraSqlColumns, table, q };
132
132
  const optimizedSQL = `select * from ${getOptimizedQuery(obj)} `;
133
133
  const tableCount = getOptimizedQuery(obj, true);
134
134
 
@@ -71,7 +71,7 @@ function formatValue({
71
71
  if (!name || !value || !fieldType) return {};
72
72
 
73
73
  // Date filter
74
- if (filterType === 'date' /* && dateTypeList.includes(fieldType) */) {
74
+ if (['date', 'datepicker'].includes(filterType) /* && dateTypeList.includes(fieldType) */) {
75
75
  const [min, max] = getDates(value, name);
76
76
 
77
77
  // check date is valid
@@ -147,8 +147,22 @@ function formatValue({
147
147
  const matchMulti = value?.indexOf(',') !== -1 ? `= any('{${value}}'::text[])` : null;
148
148
  const match = matchNull || matchBoolean || matchMulti || (operator === '=' ? `='${value}'` : `ilike '%${value}%'`);
149
149
 
150
+ const formatType = {
151
+ float8: 'numeric',
152
+ numeric: 'numeric',
153
+ int4: 'numeric',
154
+ integer: 'integer',
155
+ int8: 'numeric',
156
+ "double precision": 'double precision',
157
+ varchar: 'text',
158
+ "character varying": 'character varying',
159
+ bool: 'boolean',
160
+ boolean: 'boolean',
161
+ geometry: 'geom',
162
+ }[fieldType] || 'text';
163
+
150
164
  /* select query - from admin.cls / filter options */
151
- if (['check', 'autocomplete', 'text', 'tags', 'avatar', 'radio'].includes(filterType)) {
165
+ if (['check', 'autocomplete', 'text', 'tags', 'avatar', 'radio'].includes(filterType)) {
152
166
 
153
167
  // extra table, example: crm.extra_data
154
168
  if (extra?.table && pk) {
@@ -162,7 +176,7 @@ function formatValue({
162
176
  // default
163
177
  const query = select && filterType === 'text'
164
178
  ? `${name}::text in ( ( with q(id,name) as (${select}) select id from q where name::text ${match}) )` // filter with cls
165
- : `${name}::text ${match}`; // simple filter
179
+ : `${name}::${formatType} ${match}`; // simple filter
166
180
  return { op: operator, query };
167
181
  }
168
182
 
@@ -178,15 +192,6 @@ function formatValue({
178
192
  return { op: '=', query };
179
193
  }
180
194
 
181
- const formatType = {
182
- float8: 'numeric',
183
- int4: 'numeric',
184
- int8: 'numeric',
185
- varchar: 'text',
186
- bool: 'boolean',
187
- geometry: 'geom',
188
- }[fieldType] || 'text';
189
-
190
195
  // sql column?
191
196
  if (optimize && optimize.name !== optimize.pk) {
192
197
  const val = filterType === 'text' ? `ilike '%${value}%'` : `= any('{${value}}')`;
@@ -1,6 +1,10 @@
1
- export default function getOptimizedQuery({ table, orderby, sqlTable, sqlInline, extraSqlColumns, q }, count) {
2
- if (sqlInline) {
3
- return `(select * ${extraSqlColumns || ''} ${sqlInline ? sqlInline : ''} from ${table} t ${sqlTable && count ? ` ${sqlTable}` : ''})q where 1=1 and ${q ? q.replace('q.', 't.') : '1=1'} ${orderby ? `order by ${orderby}` : ''}`;
4
- }
5
- return `(select * ${extraSqlColumns || ''} ${sqlInline ? sqlInline : ''} from ${table} t ${sqlTable && count ? ` ${sqlTable}` : ''} where 1=1 and ${q ? q.replace('q.', 't.') : '1=1'} ${orderby ? `order by ${orderby}` : ''})q`;
1
+ export default function getOptimizedQuery({ body, extraSqlColumns, table, q }, count) {
2
+ const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
3
+
4
+ const tableName = body?.table || body?.model || table;
5
+
6
+ const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
7
+ .map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
8
+
9
+ return `(select * ${extraSqlColumns || ''} from ${tableName} t ${sqlList ? ` ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
6
10
  }
@@ -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(',');
@@ -138,8 +138,8 @@ export default async function dataAPI(req, reply, called) {
138
138
  ${metaCols}
139
139
 
140
140
  ${dbColumns.find((el) => el.name === 'geom' && pg.pgType?.[el.dataTypeID] === 'geometry') ? ',st_asgeojson(geom)::json as geom' : ''}
141
- from (select * ${sql?.filter(el => el.inline).map(el => `,(${el.sql})`).join('') || ''} from ${table} t ) t
142
- ${sqlTable}
141
+ from (select * ${sql?.filter(el => el.inline).map(el => `,(${el.sql})`).join('') || ''} from ${table} t ${sqlTable} ) t
142
+
143
143
  ${params.id ? cardSqlTable : ''}
144
144
  where ${where.join(' and ') || 'true'}
145
145
  ${order} ${offset} limit ${limit}`