@opengis/fastify-table 1.0.58 → 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 +4 -0
- package/config.js +10 -12
- package/index.js +87 -88
- package/package.json +1 -1
package/Changelog.md
CHANGED
package/config.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
package/index.js
CHANGED
|
@@ -1,88 +1,87 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
config.
|
|
25
|
-
config.
|
|
26
|
-
config.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
fastify.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
await client.query(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
req.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
export
|
|
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 };
|