@opengis/fastify-table 2.5.15 → 2.5.16

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":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAwBpD,wBAAsB,kBAAkB,CAAC,EACvC,KAAK,EACL,QAAQ,EACR,MAAM,EACN,UAAU,EACV,QAAQ,EACR,UAAU,EACV,GAAG,EACH,EAAqB,GACtB,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,UAAU,CAAC;CACjB;;;;;UAwFA;AAED,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,gBAuX7C"}
1
+ {"version":3,"file":"suggest.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/controllers/suggest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAwBpD,wBAAsB,kBAAkB,CAAC,EACvC,KAAK,EACL,QAAQ,EACR,MAAM,EACN,UAAU,EACV,QAAQ,EACR,UAAU,EACV,GAAG,EACH,EAAqB,GACtB,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,UAAU,CAAC;CACjB;;;;;UAwFA;AAED,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,gBAuY7C"}
@@ -141,9 +141,6 @@ export default async function suggest(req) {
141
141
  (metaColumns || []).find((col) => col?.name === column) ||
142
142
  {};
143
143
  const { dataTypeID } = (metaColumns || []).find((col) => col?.name === column) || {};
144
- // const filters =
145
- // hookBody?.filters ||
146
- // (body ? await getFilter({ pg, table }).then((el) => el.list) : null);
147
144
  if (table && (!column || !columnName)) {
148
145
  throw BadRequestError("param name is invalid: 2");
149
146
  }
@@ -239,32 +236,57 @@ export default async function suggest(req) {
239
236
  data,
240
237
  };
241
238
  }
239
+ const args = [];
240
+ const where = [];
242
241
  // select, with search by text support
243
- const search = query.key && typeof query.key === "string" ? searchQuery : null;
242
+ if (query.key && typeof query.key === "string") {
243
+ args.push(query.key.toLowerCase());
244
+ where.push(searchQuery);
245
+ }
246
+ // pk
247
+ if (meta.pk) {
248
+ where.push(`${meta.pk} is not null`);
249
+ }
244
250
  // filter by value
245
- const val = query.val
246
- ? `${meta.pk}=any('{${query.val.replace(/'/g, "''")}}')`
247
- : "";
248
- const filter = tableName && pg.pk?.[tableName] && columnName && dataTypeID
249
- ? `id in (select ${pg.pgType[dataTypeID]?.includes("[]")
251
+ if (meta.pk && typeof query.val === "string") {
252
+ args.push(query.val.split(","));
253
+ where.push(`${meta.pk}=any($${args.length})`);
254
+ }
255
+ // filter
256
+ if (tableName && pg.pk?.[tableName] && columnName && dataTypeID) {
257
+ where.push(`id in (select ${pg.pgType[dataTypeID]?.includes("[]")
250
258
  ? `unnest(${columnName})`
251
- : columnName} from ${tableName.replace(/'/g, "''")})`
252
- : "true";
253
- const where = [search, val, filter, meta.pk ? `${meta.pk} is not null` : null]
254
- .filter(Boolean)
255
- .join(" and ") || "true";
256
- const sqlSuggest = tableName && column
259
+ : columnName} from ${tableName.replace(/'/g, "''")})`);
260
+ }
261
+ // order by value if not specified explicitly
262
+ const order = meta.original.toLowerCase().includes("order by")
263
+ ? ""
264
+ : "order by 2";
265
+ const whereQuery = where.filter(Boolean).join(" and ") || "true";
266
+ args.push(Math.min(query.limit || meta.limit || limit, limit));
267
+ let sqlSuggest = tableName && column
257
268
  ? `${meta.original}
258
- WHERE EXISTS ( SELECT 1 FROM ${tableName} o WHERE o.${column} IS NOT DISTINCT FROM c.id ) and ${val || "true"} and ${search || "true"}
259
- ${meta.original.toLowerCase().includes("order by") ? "" : "order by 2"}
260
- LIMIT ${Math.min(query.limit || meta.limit || limit, limit)}`
261
- .replace(/{{parent}}/gi, parent)
262
- .replace(/{{uid}}/g, user?.uid || "0")
263
- : `with c(id,text) as ( ${meta.original.replace(/{{parent}}/gi, parent)} where ${where} ${meta.original.toLowerCase().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");
269
+ WHERE EXISTS ( SELECT 1 FROM ${tableName} o
270
+ WHERE o.${column} IS NOT DISTINCT FROM c.id ) and ${whereQuery || "true"}
271
+ ${order} LIMIT $${args.length}::int`
272
+ : `with c(id,text) as ( ${meta.original} where ${whereQuery} ${order}) select * from c LIMIT $${args.length}::int`;
273
+ if (sqlSuggest.includes("{{parent}}")) {
274
+ args.push(parent || "");
275
+ sqlSuggest = sqlSuggest.replace(/(['"]?){{parent}}\1/gi, `$${args.length}`);
276
+ }
277
+ if (sqlSuggest.includes("{{uid}}")) {
278
+ args.push(user?.uid || "0");
279
+ sqlSuggest = sqlSuggest.replace(/(['"]?){{uid}}\1/gi, `$${args.length}`);
280
+ }
264
281
  if (query.sql && debugMode) {
265
282
  return sqlSuggest;
266
283
  }
267
- const { rows: dataNew } = await pg.query(sqlSuggest, query.key && typeof query.key === "string" ? [query.key.toLowerCase()] : []);
284
+ if (config.trace) {
285
+ console.log("suggest", sqlSuggest, where, args);
286
+ }
287
+ const dataNew = await pg
288
+ .query(sqlSuggest, args)
289
+ .then((r) => r.rows || []);
268
290
  // in case id / text = Boolean
269
291
  const data = dataNew.filter((el) => Object.hasOwn(el, "id") && Object.hasOwn(el, "text"));
270
292
  data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.5.15",
3
+ "version": "2.5.16",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [