@opengis/fastify-table 1.0.57 → 1.0.59

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,13 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.59 - 12.07.2024
4
+
5
+ - code optimization
6
+
7
+ ## 1.0.58 - 08.07.2024
8
+
9
+ - code optimization
10
+
3
11
  ## 1.0.57 - 04.07.2024
4
12
 
5
13
  - fix crud update boolean column
package/config.js CHANGED
@@ -1,12 +1,10 @@
1
- import fs from 'fs';
2
-
3
- import { readFile } from 'fs/promises';
4
-
5
- const fileName = ['/data/local/config.json', 'config.json'].find(el => (fs.existsSync(el) ? el : null));
6
- const config = fileName ? await readFile(fileName).then(el => JSON.parse(el)) : {};
7
-
8
- Object.assign(config, {
9
- allTemplates: config?.allTemplates || {},
10
- });
11
-
12
- export default config;
1
+ import fs from 'fs';
2
+
3
+ const fileName = ['/data/local/config.json', 'config.json'].find(el => (fs.existsSync(el) ? el : null));
4
+ const config = fileName ? JSON.parse(fs.readFileSync(fileName)) : {};
5
+
6
+ Object.assign(config, {
7
+ allTemplates: config?.allTemplates || {},
8
+ });
9
+
10
+ export default config;
@@ -15,7 +15,7 @@ export default async function dataInsert({ table, data, pg: pg1 }) {
15
15
 
16
16
  ( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
17
17
 
18
- values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
18
+ values (${filterData?.map((key, i) => key[0] === 'geom' ? `st_setsrid(st_geomfromgeojson($${i + 1}::json),4326)` : `$${i + 1}`).join(',')})
19
19
 
20
20
  returning *`;
21
21
 
@@ -18,7 +18,7 @@ export default async function dataUpdate({
18
18
 
19
19
  const filterValue = filterData.map((el) => [el, data[el]]).map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]));
20
20
 
21
- const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => `"${key}"=$${i + 2}`).join(',')}
21
+ const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => key === 'geom' ? `"${key}"=st_setsrid(st_geomfromgeojson($${i + 2}::json),4326)` : `"${key}"=$${i + 2}`).join(',')}
22
22
  WHERE ${pk} = $1 returning *`;
23
23
  // console.log(updateDataset);
24
24
  const res = await pg.query(updateQuery, [id, ...filterValue]).then(el => el?.rows?.[0]) || {};
package/index.js CHANGED
@@ -1,88 +1,87 @@
1
- import path from 'path';
2
- import { readdir, readFile } from 'fs/promises';
3
- import { existsSync } from 'fs';
4
-
5
- import fp from 'fastify-plugin';
6
- import config from './config.js';
7
- // import rclient from './redis/client.js';
8
-
9
- import redisPlugin from './redis/index.js';
10
- import pgPlugin from './pg/index.js';
11
- import tablePlugin from './table/index.js';
12
- import notificationPlugin from './notification/index.js';
13
- import widgetPlugin from './widget/index.js';
14
- import crudPlugin from './crud/index.js';
15
- import policyPlugin from './policy/index.js';
16
- import utilPlugin from './util/index.js';
17
-
18
- import pgClients from './pg/pgClients.js';
19
-
20
- import execMigrations from './migration/exec.migrations.js';
21
-
22
- async function plugin(fastify, opt) {
23
- // console.log(opt);
24
- config.pg = opt.pg;
25
- config.redis = opt.redis;
26
- config.root = opt.root;
27
- config.mapServerRoot = opt.mapServerRoot;
28
-
29
- // independent npm start / unit test
30
- if (!fastify.config) {
31
- fastify.decorate('config', config);
32
- }
33
-
34
- fastify.register(import('@opengis/fastify-hb'));
35
- fastify.decorate('getFolder', (req, type = 'server') => {
36
- if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
37
- const types = { local: req.root, server: req.mapServerRoot };
38
- const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
39
- return filepath;
40
- });
41
-
42
- fastify.addHook('onListen', async () => {
43
- const { client } = pgClients;
44
- if (client?.pk?.['crm.cls']) {
45
- const clsDir = path.join(process.cwd(), 'server/templates/cls');
46
- const files = existsSync(clsDir) ? await readdir(clsDir) : [];
47
- if (files.length) {
48
- const res = await Promise.all(files.map(async (filename) => {
49
- const filepath = path.join(clsDir, filename);
50
- const data = JSON.parse(await readFile(filepath));
51
- return { name: path.parse(filename).name, data };
52
- }));
53
- await client.query('truncate table crm.cls');
54
- const { rows } = await client.query(`insert into crm.cls(name, type)
55
- select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
56
- rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
57
- const sql = `insert into crm.cls(code, name, parent)
58
- select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
59
- await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
60
- }
61
- }
62
- // call from another repo / project
63
- fastify.execMigrations = execMigrations;
64
- // execute core migrations
65
- await fastify.execMigrations();
66
- });
67
- if (!fastify.funcs) {
68
- fastify.addHook('onRequest', async (req) => {
69
- req.funcs = fastify;
70
- if (!req.user && req.session?.passport?.user) {
71
- const { user } = req.session?.passport || {};
72
- req.user = user;
73
- }
74
- });
75
- // fastify.decorateRequest('funcs', fastify);
76
- }
77
-
78
- policyPlugin(fastify);
79
- redisPlugin(fastify);
80
- await pgPlugin(fastify, opt);
81
- tablePlugin(fastify, opt);
82
- crudPlugin(fastify, opt);
83
- notificationPlugin(fastify, opt);
84
- widgetPlugin(fastify, opt);
85
- utilPlugin(fastify, opt);
86
- }
87
- export default fp(plugin);
88
- // export { rclient };
1
+ import path from 'path';
2
+ import { existsSync, readdirSync, readFileSync } from 'fs';
3
+
4
+ import fp from 'fastify-plugin';
5
+ import config from './config.js';
6
+ // import rclient from './redis/client.js';
7
+
8
+ import redisPlugin from './redis/index.js';
9
+ import pgPlugin from './pg/index.js';
10
+ import tablePlugin from './table/index.js';
11
+ import notificationPlugin from './notification/index.js';
12
+ import widgetPlugin from './widget/index.js';
13
+ import crudPlugin from './crud/index.js';
14
+ import policyPlugin from './policy/index.js';
15
+ import utilPlugin from './util/index.js';
16
+
17
+ import pgClients from './pg/pgClients.js';
18
+
19
+ import execMigrations from './migration/exec.migrations.js';
20
+
21
+ async function plugin(fastify, opt) {
22
+ // console.log(opt);
23
+ config.pg = opt.pg;
24
+ config.redis = opt.redis;
25
+ config.root = opt.root;
26
+ config.mapServerRoot = opt.mapServerRoot;
27
+
28
+ // independent npm start / unit test
29
+ if (!fastify.config) {
30
+ fastify.decorate('config', config);
31
+ }
32
+
33
+ fastify.register(import('@opengis/fastify-hb'));
34
+ fastify.decorate('getFolder', (req, type = 'server') => {
35
+ if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
36
+ const types = { local: req.root, server: req.mapServerRoot };
37
+ const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
38
+ return filepath;
39
+ });
40
+
41
+ fastify.addHook('onListen', async () => {
42
+ const { client } = pgClients;
43
+ if (client?.pk?.['crm.cls']) {
44
+ const clsDir = path.join(process.cwd(), 'server/templates/cls');
45
+ const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
46
+ if (files.length) {
47
+ const res = await Promise.all(files.map(async (filename) => {
48
+ const filepath = path.join(clsDir, filename);
49
+ const data = JSON.parse(readFileSync(filepath));
50
+ return { name: path.parse(filename).name, data };
51
+ }));
52
+ await client.query('truncate table crm.cls');
53
+ const { rows } = await client.query(`insert into crm.cls(name, type)
54
+ select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
55
+ rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
56
+ const sql = `insert into crm.cls(code, name, parent)
57
+ select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
58
+ await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
59
+ }
60
+ }
61
+ // call from another repo / project
62
+ fastify.execMigrations = execMigrations;
63
+ // execute core migrations
64
+ await fastify.execMigrations();
65
+ });
66
+ if (!fastify.funcs) {
67
+ fastify.addHook('onRequest', async (req) => {
68
+ req.funcs = fastify;
69
+ if (!req.user && req.session?.passport?.user) {
70
+ const { user } = req.session?.passport || {};
71
+ req.user = user;
72
+ }
73
+ });
74
+ // fastify.decorateRequest('funcs', fastify);
75
+ }
76
+
77
+ policyPlugin(fastify);
78
+ redisPlugin(fastify);
79
+ await pgPlugin(fastify, opt);
80
+ tablePlugin(fastify, opt);
81
+ crudPlugin(fastify, opt);
82
+ notificationPlugin(fastify, opt);
83
+ widgetPlugin(fastify, opt);
84
+ utilPlugin(fastify, opt);
85
+ }
86
+ export default fp(plugin);
87
+ // export { rclient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -35,7 +35,7 @@ export default async function data(req) {
35
35
 
36
36
  const keyQuery = query.key && loadTable.key && !params.id ? `${loadTable.key}=$1` : null;
37
37
 
38
- const limit = Math.min(maxLimit, +(query.limit || 10));
38
+ const limit = Math.min(maxLimit, +(query.limit || 20));
39
39
 
40
40
  const offset = query.page && query.page > 0 ? ` offset ${(query.page - 1) * limit}` : '';
41
41
  // id, query, filter