@opengis/fastify-table 1.1.102 → 1.1.104

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.1.102",
3
+ "version": "1.1.104",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -8,7 +8,8 @@ import cronList from '../cronList.js';
8
8
 
9
9
  const md5 = (string) => createHash('md5').update(string).digest('hex');
10
10
 
11
- async function verifyUnique(name, rclient) {
11
+ const rclient = getRedis();
12
+ async function verifyUnique(name) {
12
13
  const cronId = config.port || 3000 + md5(name);
13
14
  // one per node check
14
15
  const key = `cron:unique:${cronId}`;
@@ -59,23 +60,27 @@ const interval2ms = {
59
60
  };
60
61
 
61
62
  async function runCron({
62
- pg, func, name, rclient,
63
+ func, name,
63
64
  }) {
64
- const unique = await verifyUnique(name, rclient);
65
+ const pg = getPG();
66
+
67
+ const unique = await verifyUnique(name);
65
68
 
66
69
  if (!unique) return;
67
70
  const db = pg.options.database;
68
71
 
69
72
  try {
73
+ // console.log(`cron:${name}`);
70
74
  const data = await func({ pg });
71
75
  logger.file('cron', {
72
- level: 'INFO', db, name, result: data,
76
+ db, name, result: data,
73
77
  });
74
78
  }
75
79
  catch (err) {
76
80
  logger.file('cron', {
77
- level: 'ERROR', db, name, error: err.toString(),
81
+ db, name, error: err.toString(),
78
82
  });
83
+ logger.error(err);
79
84
  }
80
85
  }
81
86
 
@@ -93,15 +98,13 @@ async function runCron({
93
98
 
94
99
  export default async function addCron(func, interval) {
95
100
  const { time = {}, disabled = [] } = config.cron || {};
96
- const pg = getPG();
97
- const rclient = getRedis();
98
101
 
99
102
  const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
100
103
 
101
104
  // if (!config.isServer) return;
102
105
 
103
106
  if (disabled.includes(name)) {
104
- logger.debug('cron', { name, message: 'cron disabled' });
107
+ logger.file('cron', { name, message: 'cron disabled' });
105
108
  return;
106
109
  }
107
110
 
@@ -111,20 +114,16 @@ export default async function addCron(func, interval) {
111
114
  const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
112
115
 
113
116
  if (intervalMs < 1000) {
114
- logger.warn('cron', { name, error: `interval ${interval} to small` });
117
+ logger.file('cron', { name, error: `interval ${interval} to small` });
115
118
  return;
116
119
  }
117
120
 
118
121
  // setTimeout to w8 for the time to start
119
122
  setTimeout(() => {
120
- runCron({
121
- pg, func, name, rclient, logger,
122
- });
123
+ runCron({ func, name });
123
124
  // interval
124
125
  setInterval(() => {
125
- runCron({
126
- pg, func, name, rclient, logger,
127
- });
126
+ runCron({ func, name });
128
127
  }, intervalMs);
129
128
  }, waitMs);
130
129
  }
@@ -22,13 +22,13 @@ async function getFilterSQL({
22
22
  return ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `;
23
23
  }).join(' ')
24
24
  : '';
25
- const fieldQuery = `select * from ${body?.table || table} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 limit 0`;
25
+ const fieldQuery = `select * from ${body?.table || table} ${sqlList ? ` t ${sqlList}` : ''} limit 0`;
26
26
  const { fields = [] } = await pg.query(fieldQuery);
27
27
 
28
28
  const { fields: fieldsModel } = body?.table && pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
29
29
 
30
30
  const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
31
- const searchColumn = body?.search_column || autoSearchColumn;
31
+ const searchColumn = body?.search_column || body?.meta?.search || autoSearchColumn;
32
32
  const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
33
33
  try {
34
34
  const tableSQL = await getTableSql({
@@ -38,7 +38,7 @@ async function getFilterSQL({
38
38
  const searchQuery = search && searchColumn
39
39
  ? ` (${searchColumn.split(',')?.map((name) => {
40
40
  const { pk } = tableSQL.find((el) => el.name === name) || {};
41
- return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery})q where ${name} ${sval})` : `${name} ${sval}`;
41
+ return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery.replace(/limit 0/g, '')} where ${name} ${sval} )q where 1=1)` : `${name} ${sval}`;
42
42
  }).join(' or ')} )` : '';
43
43
 
44
44
  const filterList1 = await Promise.all((filterList || (body?.filter_list || []).concat(body?.filterInline || []).concat(body?.filterCustom || []).concat(body?.filterState || []).concat(body?.filterList || [])