@opengis/fastify-table 1.0.49 → 1.0.50

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/Changelog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.50 - 27.06.2024
4
+
5
+ - refactor search api
6
+
3
7
  ## 1.0.49 - 26.06.2024
4
8
 
5
9
  - refactor - npm settings
package/index.js CHANGED
@@ -28,6 +28,7 @@ async function plugin(fastify, opt) {
28
28
  fastify.decorate('config', config);
29
29
  }
30
30
 
31
+ fastify.register(import('@opengis/fastify-hb'));
31
32
  fastify.decorate('getFolder', (req, type = 'server') => {
32
33
  if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
33
34
  const types = { local: req.root, server: req.mapServerRoot };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.49",
3
+ "version": "1.0.50",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -9,9 +9,10 @@
9
9
  "test": "node --test"
10
10
  },
11
11
  "dependencies": {
12
- "ioredis": "^5.3.2",
12
+ "@opengis/fastify-hb": "^1.0.0",
13
13
  "fastify": "^4.26.1",
14
14
  "fastify-plugin": "^4.0.0",
15
+ "ioredis": "^5.3.2",
15
16
  "pg": "^8.11.3"
16
17
  },
17
18
  "devDependencies": {
@@ -20,4 +21,4 @@
20
21
  },
21
22
  "author": "Softpro",
22
23
  "license": "ISC"
23
- }
24
+ }
@@ -10,13 +10,14 @@ function sequence(tables, data, fn) {
10
10
  }
11
11
 
12
12
  async function getData({
13
- pg, tableName, query = {}, maxLimit, res,
13
+ pg, funcs, tableName, query = {}, maxLimit, res,
14
14
  }) {
15
15
  const loadTable = await getTemplate('table', tableName);
16
16
 
17
17
  if (!loadTable) { return { message: 'not found', status: 404 }; }
18
18
 
19
- const { table, columns, meta } = loadTable;
19
+ const { table, columns, meta, ua } = loadTable;
20
+
20
21
  const { pk } = await getMeta(table);
21
22
 
22
23
  const cols = columns.map((el) => el.name || el).join(',');
@@ -27,10 +28,10 @@ async function getData({
27
28
  // Math.max(query.offset - res.rows.length,0)
28
29
  const offset = query.page && query.page > 0 ? ` offset ${(query.page - 1) * limit}` : '';
29
30
 
30
- const search = meta?.search && query.key ? `(${meta?.search.concat(meta?.title ? `,${meta?.title}` : '').split(',').map(el => `${el} ilike '%${query.key}%'`).join(' or ')})` : null;
31
+ const search = meta?.search && query.key ? `(${meta?.search.concat(meta?.title ? `,${meta?.title}` : '').split(',').map(el => `${el} ilike '%${query.key}%'`).join(' or ')})` : 'false';
31
32
 
32
- const where = [loadTable.query, search].filter((el) => el);
33
- const q = `select ${pk ? `"${pk}" as id,` : ''} * from ${table} t where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
33
+ const where = [!pk ? 'false' : 'true', loadTable.query, search].filter((el) => el);
34
+ const q = `select ${[`"${pk}" as id`, meta?.title ? `${meta.title} as title` : ''].filter((el) => el).join(',')} from ${table} t where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
34
35
  if (query.sql) {
35
36
  res.sql.push(q);
36
37
  return;
@@ -42,11 +43,14 @@ async function getData({
42
43
 
43
44
  await metaFormat({ rows, table: tableName });
44
45
  res.total += +total;
45
- rows.forEach((row) => res.rows.push({ ...row, register: tableName }));
46
+ rows.forEach((row) => {
47
+ const href = meta?.href ? funcs.handlebars.compile(meta.href)({ ...row, [pk]: row.id }) : undefined;
48
+ res.rows.push({ ...row, register: tableName, register_title: loadTable.ua, href });
49
+ });
46
50
  }
47
51
 
48
- export default async function data({
49
- pg, query = {},
52
+ export default async function search({
53
+ pg, funcs, query = {},
50
54
  }) {
51
55
  const time = Date.now();
52
56
 
@@ -55,7 +59,7 @@ export default async function data({
55
59
  const res = { rows: [], sql: [], total: 0 };
56
60
 
57
61
  const maxLimit = Math.min(100, query.limit || '16');
58
- await sequence(tables, { pg, query, maxLimit, res }, getData);
62
+ await sequence(tables, { pg, funcs, query, maxLimit, res }, getData);
59
63
 
60
64
  if (query.sql) return res.sql.join(';\n');
61
65