@opengis/fastify-table 1.3.15 → 1.3.17

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.3.15",
3
+ "version": "1.3.17",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,7 +1,10 @@
1
1
  import path from 'node:path';
2
+ import { createHash } from 'node:crypto';
2
3
  import { existsSync, readFileSync } from 'node:fs';
3
4
 
4
- import { logger, pgClients } from '../../../utils.js';
5
+ import { config, logger, pgClients, getRedis } from '../../../utils.js';
6
+
7
+ const rclient = getRedis();
5
8
 
6
9
  export default async function execSql(filepath, pg = pgClients.client) {
7
10
  const start = Date.now();
@@ -21,9 +24,18 @@ export default async function execSql(filepath, pg = pgClients.client) {
21
24
 
22
25
  const sql = readFileSync(filepath, 'utf-8');
23
26
 
27
+ const hash = createHash('md5').update(sql).digest('hex');
28
+ const hashes = await rclient.hgetall(`${pg.options?.database}:migration-hashes`).then(obj => Object.keys(obj));
29
+
30
+ if (hashes.includes(hash) && !config.disableCache) {
31
+ console.log(filename, 'skip equal hash', Date.now() - start);
32
+ return null;
33
+ }
34
+
24
35
  try {
25
36
  console.log(filename, 'start', Date.now() - start);
26
37
  await pg.query(sql);
38
+ if (!config.disableCache) await rclient.hset(`${pg.options?.database}:migration-hashes`, hash, 1);
27
39
  console.log(filename, 'finish', Date.now() - start);
28
40
  logger.file('migration/success', {
29
41
  filepath,
@@ -120,8 +120,8 @@ export default async function dataAPI(req, reply, called) {
120
120
  const [orderColumn, orderDir] = (query.order || loadTable?.order || '').split(/[- ]/);
121
121
 
122
122
  const order = query.order && columnList.includes(orderColumn) && orderColumn?.length
123
- ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}`
124
- : `order by ${(loadTable?.order || 'true::boolean')}`;
123
+ ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''} nulls last`
124
+ : `order by ${(loadTable?.order || 'true::boolean')} nulls last`;
125
125
  const search = loadTable?.meta?.search && query.search
126
126
  ? `(${loadTable?.meta?.search?.split(',')?.map(el => `${el} ilike '%${query.search.replace(/%/g, '\\%').replace(/'/g, "''")}%'`).join(' or ')})`
127
127
  : null;
@@ -142,7 +142,7 @@ export default async function dataAPI(req, reply, called) {
142
142
 
143
143
  ${params.id ? cardSqlTable : ''}
144
144
  where ${where.join(' and ') || 'true'}
145
- ${order} ${offset} limit ${limit}`
145
+ ${order} ${offset} limit ${limit}`
146
146
  .replace(/{{uid}}/g, uid);
147
147
 
148
148
  // if (user?.user_type === 'superadmin') console.log(q);