@opengis/fastify-table 1.0.83 → 1.0.84

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.84 - 22.08.2024
4
+
5
+ - suggest table:column support
6
+
3
7
  ## 1.0.83 - 20.08.2024
4
8
 
5
9
  - code optimization
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.83",
3
+ "version": "1.0.84",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -1,62 +1,79 @@
1
- import getSelectMeta from './utils/getSelectMeta.js';
2
- import getPG from '../../pg/funcs/getPG.js';
3
- import config from '../../config.js';
4
-
5
- const limit = 50;
6
- const headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Cache-Control': 'no-cache' };
7
-
8
- export default async function suggest(req) {
9
- const { params, query, pg: pg1 } = req;
10
-
11
- const lang = query.lang || 'ua';
12
- const time = Date.now();
13
- const parent = query.parent || '';
14
-
15
- const selectName = query.sel || query.name || params.data;
16
- if (!selectName) return { headers, status: 400, message: 'name is required' };
17
-
18
- const meta = await getSelectMeta({ name: selectName });
19
- const pg = meta?.db ? getPG(meta.db) : pg1;
20
- if (!meta) return { headers, status: 404, message: 'Not found query select ' };
21
- if (query.meta) return meta;
22
-
23
- const { arr, searchQuery } = meta;
24
-
25
- if (arr) {
26
- const lower = query.key?.toLowerCase();
27
- const data = query.key || query.val
28
- ? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
29
- : arr;
30
- return {
31
- limit, count: data.length, mode: 'array', time: Date.now() - time, data,
32
- };
33
- }
34
-
35
- // search
36
- const search = query.key ? searchQuery : null;
37
-
38
- // val
39
- // const pk = meta.originalCols.split(',')[0];
40
- const val = query.val ? ` id=any('{${query.val.replace(/'/g, "''")}}')` : '';
41
-
42
- const sqlSuggest = `${meta.original.replace(/{{parent}}/gi, parent)} where ${[search, val, 'id is not null'].filter((el) => el).join(' and ') || 'true'} limit ${limit}`;
43
- if (query.sql) return sqlSuggest;
44
-
45
- // query
46
- const { rows: dataNew } = meta.searchColumn ? { rows: [] } : await pg.query(sqlSuggest, query.key ? [`${query.key}%`] : []);
47
- const { rows: dataNew1 } = dataNew.length < limit ? await pg.query(sqlSuggest, query.key ? [`%${query.key}%`] : []) : {};
48
- const ids = dataNew.map((el) => el.id);
49
- const data = dataNew.concat((dataNew1 || []).filter((el) => !ids?.includes(el.id)));
50
-
51
- const message = {
52
- time: Date.now() - time,
53
- count: data.length,
54
- total: meta.count - 0,
55
- mode: 'sql',
56
- db: meta.db,
57
- sql: config.local ? sqlSuggest : undefined,
58
- data,
59
- };
60
-
61
- return message;
62
- }
1
+ import getSelectMeta from './utils/getSelectMeta.js';
2
+ import getPG from '../../pg/funcs/getPG.js';
3
+ import config from '../../config.js';
4
+ import getTemplate from './utils/getTemplate.js';
5
+ import getTableSql from '../funcs/getFilterSQL/util/getTableSql.js';
6
+
7
+ const limit = 50;
8
+ const headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Cache-Control': 'no-cache' };
9
+
10
+ export default async function suggest(req) {
11
+ const { params, query, pg: pg1 } = req;
12
+
13
+ const lang = query.lang || 'ua';
14
+ const time = Date.now();
15
+ const parent = query.parent || '';
16
+
17
+ const [table, column] = params.data?.includes(':') ? params.data.split(':') : [];
18
+ const selectName = query.sel || query.name || params.data;
19
+ if (!selectName) return { headers, status: 400, message: 'name is required' };
20
+
21
+ const body = await getTemplate('table', table);
22
+ if (table && !pg1.pk[body?.table || table]) {
23
+ return { headers, status: 400, message: 'param name is invalid: 1' };
24
+ }
25
+ const columnExists = true;
26
+ if (table && (!column || !columnExists)) {
27
+ return { headers, status: 400, message: 'param name is invalid: 2' };
28
+ }
29
+
30
+ const meta = table && column
31
+ ? {
32
+ original: `with c(id,text) as (select row_number() over(), ${column} from ${body?.table || table} group by ${column} ) select * from c`,
33
+ searchQuery: '("text" ilike $1 )',
34
+ }
35
+ : await getSelectMeta({ name: selectName });
36
+ const pg = meta?.db ? getPG(meta.db) : pg1;
37
+ if (!meta) return { headers, status: 404, message: 'Not found query select ' };
38
+ if (query.meta) return meta;
39
+
40
+ const { arr, searchQuery } = meta;
41
+
42
+ if (arr) {
43
+ const lower = query.key?.toLowerCase();
44
+ const data = query.key || query.val
45
+ ? arr?.filter((el) => !lower || (el[lang] || el.text)?.toLowerCase()?.indexOf(lower) !== -1)?.filter((el) => !query.val || el.id === query.val)
46
+ : arr;
47
+ return {
48
+ limit, count: data.length, mode: 'array', time: Date.now() - time, data,
49
+ };
50
+ }
51
+
52
+ // search
53
+ const search = query.key ? searchQuery : null;
54
+
55
+ // val
56
+ // const pk = meta.originalCols.split(',')[0];
57
+ const val = query.val ? ` id=any('{${query.val.replace(/'/g, "''")}}')` : '';
58
+
59
+ const sqlSuggest = `${meta.original.replace(/{{parent}}/gi, parent)} where ${[search, val, 'id is not null'].filter((el) => el).join(' and ') || 'true'} limit ${limit}`;
60
+ if (query.sql) return sqlSuggest;
61
+
62
+ // query
63
+ const { rows: dataNew } = meta.searchColumn ? { rows: [] } : await pg.query(sqlSuggest, query.key ? [`${query.key}%`] : []);
64
+ const { rows: dataNew1 } = dataNew.length < limit ? await pg.query(sqlSuggest, query.key ? [`%${query.key}%`] : []) : {};
65
+ const ids = dataNew.map((el) => el.id);
66
+ const data = dataNew.concat((dataNew1 || []).filter((el) => !ids?.includes(el.id)));
67
+
68
+ const message = {
69
+ time: Date.now() - time,
70
+ count: data.length,
71
+ total: meta.count - 0,
72
+ mode: 'sql',
73
+ db: meta.db,
74
+ sql: config.local ? sqlSuggest : undefined,
75
+ data,
76
+ };
77
+
78
+ return message;
79
+ }
@@ -1,34 +1,34 @@
1
- function getTable(table) {
2
- const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
3
- ?.map((el) => el.split(/[ )]/)[0]);
4
- return result;
5
- }
6
-
7
- /**
8
- * @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
9
- * @param {String} opt.query - запит до таблиці
10
- * @param {String} opt.hash - інформація з хешу по запиту
11
- */
12
- const tableSql = {};
13
- async function getTableSql({
14
- pg, body, table, fields,
15
- }) {
16
- if (tableSql[table]) return tableSql[table];
17
-
18
- const fieldList = fields.map((el) => el.name);
19
-
20
- const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
21
-
22
- if (!tableList) { tableSql[table] = []; return []; }
23
-
24
- const data = await Promise.all(tableList?.map(async (tableEl) => {
25
- const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`);
26
- return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
27
- }));
28
-
29
- tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
30
-
31
- return tableSql[table];
32
- }
33
-
34
- export default getTableSql;
1
+ function getTable(table) {
2
+ const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
3
+ ?.map((el) => el.split(/[ )]/)[0]);
4
+ return result;
5
+ }
6
+
7
+ /**
8
+ * @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
9
+ * @param {String} opt.query - запит до таблиці
10
+ * @param {String} opt.hash - інформація з хешу по запиту
11
+ */
12
+ const tableSql = {};
13
+ async function getTableSql({
14
+ pg, body, table, fields,
15
+ }) {
16
+ if (tableSql[table]) return tableSql[table];
17
+
18
+ const fieldList = fields.map((el) => el.name);
19
+
20
+ const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
21
+
22
+ if (!tableList) { tableSql[table] = []; return []; }
23
+
24
+ const data = await Promise.all(tableList?.map(async (tableEl) => {
25
+ const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`);
26
+ return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
27
+ }));
28
+
29
+ tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
30
+
31
+ return tableSql[table];
32
+ }
33
+
34
+ export default getTableSql;