@opengis/fastify-table 1.0.52 → 1.0.54
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 +8 -0
- package/index.js +86 -80
- package/migration/exec.migrations.js +76 -0
- package/package.json +1 -1
- package/table/controllers/data.js +1 -1
package/Changelog.md
CHANGED
package/index.js
CHANGED
|
@@ -1,80 +1,86 @@
|
|
|
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
|
-
|
|
17
|
-
import pgClients from './pg/pgClients.js';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
config.
|
|
24
|
-
config.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
rows
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
|
|
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) ? await readdir(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(await readFile(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
|
+
}
|
|
85
|
+
export default fp(plugin);
|
|
86
|
+
// export { rclient };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
const time = Date.now();
|
|
5
|
+
|
|
6
|
+
import getPG from '../pg/funcs/getPG.js';
|
|
7
|
+
|
|
8
|
+
export default async function execMigrations(opt) {
|
|
9
|
+
try {
|
|
10
|
+
const pg = opt?.pg || getPG({ name: 'client' });
|
|
11
|
+
const rootDir = getCallerDir();
|
|
12
|
+
const dir = path.join(rootDir.replace(/\\/g, '/').replace(/^file:\/\/\//, ''), rootDir.endsWith('plugins') ? '../..' : '', 'server/migrations');
|
|
13
|
+
|
|
14
|
+
console.log('migrations start', dir, Date.now() - time);
|
|
15
|
+
const exists = fs.existsSync(dir);
|
|
16
|
+
if (exists) {
|
|
17
|
+
// get directory sql file list
|
|
18
|
+
const content = fs.readdirSync(dir, { withFileTypes: true })
|
|
19
|
+
?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
|
|
20
|
+
?.map((el) => el.name) || [];
|
|
21
|
+
|
|
22
|
+
// execute sql files
|
|
23
|
+
if (content?.length) {
|
|
24
|
+
await sequence(content, { pg, dir }, execSql);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
console.log('migrations finish', dir, exists, Date.now() - time);
|
|
28
|
+
} catch(err) {
|
|
29
|
+
console.error('migrations error', err.toString(), Date.now() - time);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getCallerDir() {
|
|
34
|
+
const originalFunc = Error.prepareStackTrace;
|
|
35
|
+
|
|
36
|
+
let callerfile;
|
|
37
|
+
try {
|
|
38
|
+
const err = new Error();
|
|
39
|
+
let currentfile;
|
|
40
|
+
|
|
41
|
+
Error.prepareStackTrace = function (err, stack) { return stack; };
|
|
42
|
+
|
|
43
|
+
currentfile = err.stack.shift().getFileName();
|
|
44
|
+
|
|
45
|
+
while (err.stack.length) {
|
|
46
|
+
callerfile = err.stack.shift().getFileName();
|
|
47
|
+
|
|
48
|
+
if(currentfile !== callerfile) break;
|
|
49
|
+
}
|
|
50
|
+
} catch (err) { }
|
|
51
|
+
|
|
52
|
+
Error.prepareStackTrace = originalFunc;
|
|
53
|
+
|
|
54
|
+
return path.dirname(callerfile);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function sequence(files, data, fn) {
|
|
58
|
+
return files.reduce((promise, filename) => promise.then(() => fn({
|
|
59
|
+
...data, filename,
|
|
60
|
+
})), Promise.resolve());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function execSql({
|
|
64
|
+
pg, dir, filename,
|
|
65
|
+
}) {
|
|
66
|
+
const start = Date.now();
|
|
67
|
+
const filepath = path.join(dir, filename);
|
|
68
|
+
const sql = fs.readFileSync(filepath, 'utf-8');
|
|
69
|
+
try {
|
|
70
|
+
console.log(filename, 'start', Date.now() - start);
|
|
71
|
+
await pg.query(sql);
|
|
72
|
+
console.log(filename, 'finish', Date.now() - start);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.log(filepath, 'error', err.toString(), Date.now() - start);
|
|
75
|
+
}
|
|
76
|
+
}
|
package/package.json
CHANGED
|
@@ -24,7 +24,7 @@ export default async function data(req) {
|
|
|
24
24
|
const cols = columns.map((el) => el.name || el).join(',');
|
|
25
25
|
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
26
26
|
const sqlTable = sql?.filter?.((el) => !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
|
|
27
|
-
const cardSqlFiltered = params.id ? cardSql?.filter?.((el) => !el?.disabled && el?.name && el?.sql?.replace) : [];
|
|
27
|
+
const cardSqlFiltered = params.id ? (cardSql?.filter?.((el) => !el?.disabled && el?.name && el?.sql?.replace) || []) : [];
|
|
28
28
|
const cardSqlTable = cardSqlFiltered.length ? cardSqlFiltered.map((el, i) => ` left join lateral (select json_agg(row_to_json(q)) as ${el.name} from (${el.sql})q) ct${i} on 1=1 `).join('') || '' : '';
|
|
29
29
|
|
|
30
30
|
const fData = query.filter ? await getFilterSQL({
|