@opengis/fastify-table 1.0.91 → 1.0.92
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/crud/controllers/deleteCrud.js +22 -22
- package/crud/controllers/insert.js +58 -58
- package/crud/controllers/update.js +61 -61
- package/crud/funcs/dataDelete.js +19 -19
- package/crud/funcs/dataInsert.js +30 -30
- package/crud/funcs/dataUpdate.js +30 -29
- package/crud/funcs/getAccess.js +53 -53
- package/crud/funcs/getOpt.js +10 -10
- package/crud/funcs/setOpt.js +16 -16
- package/crud/funcs/utils/logChanges.js +71 -71
- package/crud/index.js +36 -36
- package/helper.js +28 -28
- package/index.js +97 -97
- package/notification/controllers/userNotifications.js +19 -19
- package/notification/funcs/addNotification.js +8 -8
- package/package.json +1 -1
- package/pg/pgClients.js +20 -20
- package/policy/funcs/checkPolicy.js +83 -83
- package/policy/funcs/sqlInjection.js +33 -33
- package/policy/index.js +14 -14
- package/redis/client.js +8 -8
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -19
- package/server/migrations/0.sql +78 -78
- package/server/templates/form/test.dataset.form.json +411 -411
- package/server/templates/table/test.dataset.table.json +28 -28
- package/server/templates/table/test.gis.map.table.json +44 -44
- package/table/controllers/data.js +103 -103
- package/table/controllers/suggest.js +79 -79
- package/table/controllers/table.js +52 -52
- package/table/controllers/utils/getSelectMeta.js +2 -2
- package/table/controllers/utils/getTemplate.js +1 -1
- package/table/controllers/utils/gisIRColumn.js +68 -68
- package/table/funcs/getFilterSQL/index.js +75 -75
- package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
- package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
- package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/table/funcs/metaFormat/getSelectVal.js +20 -20
- package/table/funcs/metaFormat/index.js +28 -28
- package/test/api/crud.test.js +88 -88
- package/test/api/table.test.js +89 -89
- package/test/api/widget.test.js +117 -117
- package/test/funcs/crud.test.js +122 -122
- package/util/controllers/properties.add.js +57 -57
- package/util/controllers/status.monitor.js +8 -8
- package/widget/controllers/utils/historyFormat.js +76 -76
- package/widget/controllers/utils/obj2db.js +13 -13
- package/widget/controllers/widget.del.js +44 -44
- package/widget/controllers/widget.get.js +98 -98
- package/widget/controllers/widget.set.js +76 -76
- package/widget/index.js +40 -40
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
function formatData(fieldType = 'text', value = null) {
|
|
2
|
-
if (!value) return null;
|
|
3
|
-
if (fieldType === 'geometry') {
|
|
4
|
-
return typeof value === 'object' ? `st_astext(st_geomfromgeojson('${JSON.stringify(value)}'::json))` : `st_astext('${value}'::geometry)`;
|
|
5
|
-
}
|
|
6
|
-
if (['integer', 'numeric', 'double precision'].includes(fieldType)) {
|
|
7
|
-
return value || null;
|
|
8
|
-
}
|
|
9
|
-
if (fieldType.includes('timestamp') || fieldType === 'date') {
|
|
10
|
-
if (typeof value === 'object') {
|
|
11
|
-
return value ? `'${value.toISOString()}'::${fieldType}` : null;
|
|
12
|
-
}
|
|
13
|
-
return value ? `'${value}'::${fieldType}` : null;
|
|
14
|
-
}
|
|
15
|
-
/* if (fieldType.includes('json') && Array.isArray(value)) {
|
|
16
|
-
return value;
|
|
17
|
-
} */
|
|
18
|
-
if (Array.isArray(value)) {
|
|
19
|
-
return `'{ ${value.join(',')} }'::${fieldType}`;
|
|
20
|
-
}
|
|
21
|
-
return typeof value === 'object' ? JSON.stringify(value) : `'${value || null}'`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export default async function logChanges({
|
|
25
|
-
pg, table, id, data, uid = 1, type,
|
|
26
|
-
}) {
|
|
27
|
-
if (!id) {
|
|
28
|
-
console.error('param id is required');
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
if (!table || !pg.pk?.[table]) {
|
|
32
|
-
console.error('table not found');
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
if (!pg.pk?.['log.table_changes'] || !pg.pk?.['log.table_changes_data']) {
|
|
36
|
-
console.error('log table not found');
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
if (!type) {
|
|
40
|
-
console.error('invalid type');
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
const { change_id: changeId } = await pg.query(`insert into log.table_changes(change_date,change_type,change_user_id,entity_type,entity_id)
|
|
46
|
-
values(CURRENT_DATE, $1, $2, $3, $4) returning change_id`, [type, uid, table, id]).then((res) => res.rows?.[0] || {});
|
|
47
|
-
|
|
48
|
-
const q = `select ${Object.keys(data || {}).join(',') || '*'} from ${table} where ${pg.pk?.[table]}=$1`;
|
|
49
|
-
// console.log(q, type, id);
|
|
50
|
-
const { fields = [] } = await pg.query(`select * from ${table} limit 0`);
|
|
51
|
-
const old = type !== 'INSERT' ? await pg.query(q, [id]).then((res) => res.rows?.[0] || {}) : {};
|
|
52
|
-
|
|
53
|
-
const fieldTypes = fields?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: pg.pgType[curr.dataTypeID] }), {}) || {};
|
|
54
|
-
const q1 = Object.keys(data || {}).map((el) => `insert into log.table_changes_data(change_id,entity_key,value_old,value_new)
|
|
55
|
-
values('${changeId}', '${el}', ${formatData(fieldTypes[el], old[el])}, ${formatData(fieldTypes[el], data[el])}) returning *`).join(';\n');
|
|
56
|
-
// console.log(q1);
|
|
57
|
-
const res = await pg.query(q1);
|
|
58
|
-
|
|
59
|
-
const newData = type === 'DELETE' ? {} : (Array.isArray(res) ? res : [res]).reduce((acc, curr) => Object.assign(acc, { [curr.rows?.[0].entity_key]: curr.rows?.[0].value_new }), {});
|
|
60
|
-
// console.log('logChanges OK', type);
|
|
61
|
-
return {
|
|
62
|
-
change_id: changeId, entity_type: table, entity_id: id, uid, change_type: type, old, new: newData,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
catch (err) {
|
|
66
|
-
console.error('logChanges error', type, table, id, data, err.toString());
|
|
67
|
-
return {
|
|
68
|
-
error: err.toString(), entity_type: table, entity_id: id, uid, change_type: type,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
1
|
+
function formatData(fieldType = 'text', value = null) {
|
|
2
|
+
if (!value) return null;
|
|
3
|
+
if (fieldType === 'geometry') {
|
|
4
|
+
return typeof value === 'object' ? `st_astext(st_geomfromgeojson('${JSON.stringify(value)}'::json))` : `st_astext('${value}'::geometry)`;
|
|
5
|
+
}
|
|
6
|
+
if (['integer', 'numeric', 'double precision'].includes(fieldType)) {
|
|
7
|
+
return value || null;
|
|
8
|
+
}
|
|
9
|
+
if (fieldType.includes('timestamp') || fieldType === 'date') {
|
|
10
|
+
if (typeof value === 'object') {
|
|
11
|
+
return value ? `'${value.toISOString()}'::${fieldType}` : null;
|
|
12
|
+
}
|
|
13
|
+
return value ? `'${value}'::${fieldType}` : null;
|
|
14
|
+
}
|
|
15
|
+
/* if (fieldType.includes('json') && Array.isArray(value)) {
|
|
16
|
+
return value;
|
|
17
|
+
} */
|
|
18
|
+
if (Array.isArray(value)) {
|
|
19
|
+
return `'{ ${value.join(',')} }'::${fieldType}`;
|
|
20
|
+
}
|
|
21
|
+
return typeof value === 'object' ? JSON.stringify(value) : `'${value || null}'`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default async function logChanges({
|
|
25
|
+
pg, table, id, data, uid = 1, type,
|
|
26
|
+
}) {
|
|
27
|
+
if (!id) {
|
|
28
|
+
console.error('param id is required');
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (!table || !pg.pk?.[table]) {
|
|
32
|
+
console.error('table not found');
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (!pg.pk?.['log.table_changes'] || !pg.pk?.['log.table_changes_data']) {
|
|
36
|
+
console.error('log table not found');
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (!type) {
|
|
40
|
+
console.error('invalid type');
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const { change_id: changeId } = await pg.query(`insert into log.table_changes(change_date,change_type,change_user_id,entity_type,entity_id)
|
|
46
|
+
values(CURRENT_DATE, $1, $2, $3, $4) returning change_id`, [type, uid, table, id]).then((res) => res.rows?.[0] || {});
|
|
47
|
+
|
|
48
|
+
const q = `select ${Object.keys(data || {}).join(',') || '*'} from ${table} where ${pg.pk?.[table]}=$1`;
|
|
49
|
+
// console.log(q, type, id);
|
|
50
|
+
const { fields = [] } = await pg.query(`select * from ${table} limit 0`);
|
|
51
|
+
const old = type !== 'INSERT' ? await pg.query(q, [id]).then((res) => res.rows?.[0] || {}) : {};
|
|
52
|
+
|
|
53
|
+
const fieldTypes = fields?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: pg.pgType[curr.dataTypeID] }), {}) || {};
|
|
54
|
+
const q1 = Object.keys(data || {}).map((el) => `insert into log.table_changes_data(change_id,entity_key,value_old,value_new)
|
|
55
|
+
values('${changeId}', '${el}', ${formatData(fieldTypes[el], old[el])}, ${formatData(fieldTypes[el], data[el])}) returning *`).join(';\n');
|
|
56
|
+
// console.log(q1);
|
|
57
|
+
const res = await pg.query(q1);
|
|
58
|
+
|
|
59
|
+
const newData = type === 'DELETE' ? {} : (Array.isArray(res) ? res : [res]).reduce((acc, curr) => Object.assign(acc, { [curr.rows?.[0].entity_key]: curr.rows?.[0].value_new }), {});
|
|
60
|
+
// console.log('logChanges OK', type);
|
|
61
|
+
return {
|
|
62
|
+
change_id: changeId, entity_type: table, entity_id: id, uid, change_type: type, old, new: newData,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
console.error('logChanges error', type, table, id, data, err.toString());
|
|
67
|
+
return {
|
|
68
|
+
error: err.toString(), entity_type: table, entity_id: id, uid, change_type: type,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
package/crud/index.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import getOpt from './funcs/getOpt.js';
|
|
2
|
-
import setOpt from './funcs/setOpt.js';
|
|
3
|
-
import isFileExists from './funcs/isFileExists.js';
|
|
4
|
-
import dataUpdate from './funcs/dataUpdate.js';
|
|
5
|
-
import dataInsert from './funcs/dataInsert.js';
|
|
6
|
-
|
|
7
|
-
import update from './controllers/update.js';
|
|
8
|
-
import insert from './controllers/insert.js';
|
|
9
|
-
import deleteCrud from './controllers/deleteCrud.js';
|
|
10
|
-
import getAccessFunc from './funcs/getAccess.js';
|
|
11
|
-
|
|
12
|
-
const tableSchema = {
|
|
13
|
-
params: {
|
|
14
|
-
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
15
|
-
table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
async function plugin(fastify, config = {}) {
|
|
20
|
-
const prefix = config.prefix || '/api';
|
|
21
|
-
// funcs
|
|
22
|
-
fastify.decorate('setOpt', setOpt);
|
|
23
|
-
fastify.decorate('getOpt', getOpt);
|
|
24
|
-
fastify.decorate('dataUpdate', dataUpdate);
|
|
25
|
-
fastify.decorate('dataInsert', dataInsert);
|
|
26
|
-
fastify.decorate('getAccess', getAccessFunc);
|
|
27
|
-
|
|
28
|
-
fastify.decorate('isFileExists', isFileExists);
|
|
29
|
-
|
|
30
|
-
// api
|
|
31
|
-
fastify.put(`${prefix}/table/:table/:id`, { schema: tableSchema }, update);
|
|
32
|
-
fastify.delete(`${prefix}/table/:table/:id`, { schema: tableSchema }, deleteCrud);
|
|
33
|
-
fastify.post(`${prefix}/table/:table`, { schema: tableSchema }, insert);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export default plugin;
|
|
1
|
+
import getOpt from './funcs/getOpt.js';
|
|
2
|
+
import setOpt from './funcs/setOpt.js';
|
|
3
|
+
import isFileExists from './funcs/isFileExists.js';
|
|
4
|
+
import dataUpdate from './funcs/dataUpdate.js';
|
|
5
|
+
import dataInsert from './funcs/dataInsert.js';
|
|
6
|
+
|
|
7
|
+
import update from './controllers/update.js';
|
|
8
|
+
import insert from './controllers/insert.js';
|
|
9
|
+
import deleteCrud from './controllers/deleteCrud.js';
|
|
10
|
+
import getAccessFunc from './funcs/getAccess.js';
|
|
11
|
+
|
|
12
|
+
const tableSchema = {
|
|
13
|
+
params: {
|
|
14
|
+
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
15
|
+
table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
async function plugin(fastify, config = {}) {
|
|
20
|
+
const prefix = config.prefix || '/api';
|
|
21
|
+
// funcs
|
|
22
|
+
fastify.decorate('setOpt', setOpt);
|
|
23
|
+
fastify.decorate('getOpt', getOpt);
|
|
24
|
+
fastify.decorate('dataUpdate', dataUpdate);
|
|
25
|
+
fastify.decorate('dataInsert', dataInsert);
|
|
26
|
+
fastify.decorate('getAccess', getAccessFunc);
|
|
27
|
+
|
|
28
|
+
fastify.decorate('isFileExists', isFileExists);
|
|
29
|
+
|
|
30
|
+
// api
|
|
31
|
+
fastify.put(`${prefix}/table/:table/:id`, { schema: tableSchema }, update);
|
|
32
|
+
fastify.delete(`${prefix}/table/:table/:id`, { schema: tableSchema }, deleteCrud);
|
|
33
|
+
fastify.post(`${prefix}/table/:table`, { schema: tableSchema }, insert);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default plugin;
|
package/helper.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
// This file contains code that we reuse
|
|
2
|
-
// between our tests.
|
|
3
|
-
import Fastify from 'fastify';
|
|
4
|
-
import config from './test/config.js';
|
|
5
|
-
import appService from './index.js';
|
|
6
|
-
|
|
7
|
-
import rclient from './redis/client.js';
|
|
8
|
-
import pgClients from './pg/pgClients.js';
|
|
9
|
-
|
|
10
|
-
// automatically build and tear down our instance
|
|
11
|
-
async function build(t) {
|
|
12
|
-
// you can set all the options supported by the fastify CLI command
|
|
13
|
-
// const argv = [AppPath]
|
|
14
|
-
process.env.NODE_ENV = 'production';
|
|
15
|
-
const app = Fastify({ logger: false });
|
|
16
|
-
app.register(appService, config);
|
|
17
|
-
// close the app after we are done
|
|
18
|
-
t.after(() => {
|
|
19
|
-
// console.log('close app');
|
|
20
|
-
pgClients.client.end();
|
|
21
|
-
rclient.quit();
|
|
22
|
-
app.close();
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
return app;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export default build;
|
|
1
|
+
// This file contains code that we reuse
|
|
2
|
+
// between our tests.
|
|
3
|
+
import Fastify from 'fastify';
|
|
4
|
+
import config from './test/config.js';
|
|
5
|
+
import appService from './index.js';
|
|
6
|
+
|
|
7
|
+
import rclient from './redis/client.js';
|
|
8
|
+
import pgClients from './pg/pgClients.js';
|
|
9
|
+
|
|
10
|
+
// automatically build and tear down our instance
|
|
11
|
+
async function build(t) {
|
|
12
|
+
// you can set all the options supported by the fastify CLI command
|
|
13
|
+
// const argv = [AppPath]
|
|
14
|
+
process.env.NODE_ENV = 'production';
|
|
15
|
+
const app = Fastify({ logger: false });
|
|
16
|
+
app.register(appService, config);
|
|
17
|
+
// close the app after we are done
|
|
18
|
+
t.after(() => {
|
|
19
|
+
// console.log('close app');
|
|
20
|
+
pgClients.client.end();
|
|
21
|
+
rclient.quit();
|
|
22
|
+
app.close();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return app;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default build;
|
package/index.js
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
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
|
-
import cronPlugin from './cron/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('@fastify/sensible'), {
|
|
35
|
-
errorHandler: false,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
fastify.register(import('@fastify/url-data'), {
|
|
39
|
-
errorHandler: false,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
fastify.register(import('@opengis/fastify-hb'));
|
|
43
|
-
fastify.decorate('getFolder', (req, type = 'server') => {
|
|
44
|
-
if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
|
|
45
|
-
const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
|
|
46
|
-
const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
|
|
47
|
-
return filepath;
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
fastify.addHook('onListen', async () => {
|
|
51
|
-
const { client } = pgClients;
|
|
52
|
-
if (client?.pk?.['crm.cls']) {
|
|
53
|
-
const clsDir = path.join(process.cwd(), 'server/templates/cls');
|
|
54
|
-
const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
|
|
55
|
-
if (files.length) {
|
|
56
|
-
const res = await Promise.all(files.map(async (filename) => {
|
|
57
|
-
const filepath = path.join(clsDir, filename);
|
|
58
|
-
const data = JSON.parse(readFileSync(filepath));
|
|
59
|
-
return { name: path.parse(filename).name, data };
|
|
60
|
-
}));
|
|
61
|
-
await client.query('truncate table crm.cls');
|
|
62
|
-
const { rows } = await client.query(`insert into crm.cls(name, type)
|
|
63
|
-
select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
|
|
64
|
-
rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
|
|
65
|
-
const sql = `insert into crm.cls(code, name, parent)
|
|
66
|
-
select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
|
|
67
|
-
await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
// call from another repo / project
|
|
71
|
-
fastify.execMigrations = execMigrations;
|
|
72
|
-
// execute core migrations
|
|
73
|
-
await fastify.execMigrations();
|
|
74
|
-
});
|
|
75
|
-
if (!fastify.funcs) {
|
|
76
|
-
fastify.addHook('onRequest', async (req) => {
|
|
77
|
-
req.funcs = fastify;
|
|
78
|
-
if (!req.user && req.session?.passport?.user) {
|
|
79
|
-
const { user } = req.session?.passport || {};
|
|
80
|
-
req.user = user;
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
// fastify.decorateRequest('funcs', fastify);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
policyPlugin(fastify);
|
|
87
|
-
redisPlugin(fastify);
|
|
88
|
-
await pgPlugin(fastify, opt);
|
|
89
|
-
tablePlugin(fastify, opt);
|
|
90
|
-
crudPlugin(fastify, opt);
|
|
91
|
-
notificationPlugin(fastify, opt);
|
|
92
|
-
widgetPlugin(fastify, opt);
|
|
93
|
-
utilPlugin(fastify, opt);
|
|
94
|
-
cronPlugin(fastify, opt);
|
|
95
|
-
}
|
|
96
|
-
export default fp(plugin);
|
|
97
|
-
// 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
|
+
import cronPlugin from './cron/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('@fastify/sensible'), {
|
|
35
|
+
errorHandler: false,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
fastify.register(import('@fastify/url-data'), {
|
|
39
|
+
errorHandler: false,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
fastify.register(import('@opengis/fastify-hb'));
|
|
43
|
+
fastify.decorate('getFolder', (req, type = 'server') => {
|
|
44
|
+
if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
|
|
45
|
+
const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
|
|
46
|
+
const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
|
|
47
|
+
return filepath;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
fastify.addHook('onListen', async () => {
|
|
51
|
+
const { client } = pgClients;
|
|
52
|
+
if (client?.pk?.['crm.cls']) {
|
|
53
|
+
const clsDir = path.join(process.cwd(), 'server/templates/cls');
|
|
54
|
+
const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
|
|
55
|
+
if (files.length) {
|
|
56
|
+
const res = await Promise.all(files.map(async (filename) => {
|
|
57
|
+
const filepath = path.join(clsDir, filename);
|
|
58
|
+
const data = JSON.parse(readFileSync(filepath));
|
|
59
|
+
return { name: path.parse(filename).name, data };
|
|
60
|
+
}));
|
|
61
|
+
await client.query('truncate table crm.cls');
|
|
62
|
+
const { rows } = await client.query(`insert into crm.cls(name, type)
|
|
63
|
+
select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
|
|
64
|
+
rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
|
|
65
|
+
const sql = `insert into crm.cls(code, name, parent)
|
|
66
|
+
select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
|
|
67
|
+
await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// call from another repo / project
|
|
71
|
+
fastify.execMigrations = execMigrations;
|
|
72
|
+
// execute core migrations
|
|
73
|
+
await fastify.execMigrations();
|
|
74
|
+
});
|
|
75
|
+
if (!fastify.funcs) {
|
|
76
|
+
fastify.addHook('onRequest', async (req) => {
|
|
77
|
+
req.funcs = fastify;
|
|
78
|
+
if (!req.user && req.session?.passport?.user) {
|
|
79
|
+
const { user } = req.session?.passport || {};
|
|
80
|
+
req.user = user;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
// fastify.decorateRequest('funcs', fastify);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
policyPlugin(fastify);
|
|
87
|
+
redisPlugin(fastify);
|
|
88
|
+
await pgPlugin(fastify, opt);
|
|
89
|
+
tablePlugin(fastify, opt);
|
|
90
|
+
crudPlugin(fastify, opt);
|
|
91
|
+
notificationPlugin(fastify, opt);
|
|
92
|
+
widgetPlugin(fastify, opt);
|
|
93
|
+
utilPlugin(fastify, opt);
|
|
94
|
+
cronPlugin(fastify, opt);
|
|
95
|
+
}
|
|
96
|
+
export default fp(plugin);
|
|
97
|
+
// export { rclient };
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export default async function userNotifications({
|
|
2
|
-
pg, session = {}, query = {},
|
|
3
|
-
}) {
|
|
4
|
-
const time = Date.now();
|
|
5
|
-
try {
|
|
6
|
-
const { uid } = session.passport?.user || {};
|
|
7
|
-
if (!uid) return { error: 'access restricted', status: 403 };
|
|
8
|
-
|
|
9
|
-
const queryFunc = query.nocache ? pg.query : pg.queryCache;
|
|
10
|
-
|
|
11
|
-
// queryCache not supports $1 params
|
|
12
|
-
const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
|
|
13
|
-
notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
|
|
14
|
-
return { time: Date.now() - time, total: rows?.length, rows };
|
|
15
|
-
}
|
|
16
|
-
catch (err) {
|
|
17
|
-
return { error: err.toString(), status: 500 };
|
|
18
|
-
}
|
|
19
|
-
}
|
|
1
|
+
export default async function userNotifications({
|
|
2
|
+
pg, session = {}, query = {},
|
|
3
|
+
}) {
|
|
4
|
+
const time = Date.now();
|
|
5
|
+
try {
|
|
6
|
+
const { uid } = session.passport?.user || {};
|
|
7
|
+
if (!uid) return { error: 'access restricted', status: 403 };
|
|
8
|
+
|
|
9
|
+
const queryFunc = query.nocache ? pg.query : pg.queryCache;
|
|
10
|
+
|
|
11
|
+
// queryCache not supports $1 params
|
|
12
|
+
const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
|
|
13
|
+
notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
|
|
14
|
+
return { time: Date.now() - time, total: rows?.length, rows };
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
return { error: err.toString(), status: 500 };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export default async function addNotification({
|
|
2
|
-
pg, session = {}, title, body, link, notificationType, uid: uid1,
|
|
3
|
-
}) {
|
|
4
|
-
const uid = uid1 || session.passport?.user?.uid || {};
|
|
5
|
-
const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
|
|
6
|
-
values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
|
|
7
|
-
return { id, status };
|
|
8
|
-
}
|
|
1
|
+
export default async function addNotification({
|
|
2
|
+
pg, session = {}, title, body, link, notificationType, uid: uid1,
|
|
3
|
+
}) {
|
|
4
|
+
const uid = uid1 || session.passport?.user?.uid || {};
|
|
5
|
+
const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
|
|
6
|
+
values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
|
|
7
|
+
return { id, status };
|
|
8
|
+
}
|
package/package.json
CHANGED
package/pg/pgClients.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import pg from 'pg';
|
|
2
|
-
import config from '../config.js';
|
|
3
|
-
import init from './funcs/init.js';
|
|
4
|
-
|
|
5
|
-
const pgClients = {};
|
|
6
|
-
if (config.pg) {
|
|
7
|
-
const client = new pg.Pool({
|
|
8
|
-
host: config.pg?.host || '127.0.0.1',
|
|
9
|
-
port: config.pg?.port || 5432,
|
|
10
|
-
database: config.pg?.database || 'postgres',
|
|
11
|
-
user: config.pg?.user || 'postgres',
|
|
12
|
-
password: config.pg?.password || 'postgres',
|
|
13
|
-
});
|
|
14
|
-
client.init = async () => {
|
|
15
|
-
await init(client);
|
|
16
|
-
};
|
|
17
|
-
client.init();
|
|
18
|
-
pgClients.client = client;
|
|
19
|
-
}
|
|
20
|
-
export default pgClients;
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
import config from '../config.js';
|
|
3
|
+
import init from './funcs/init.js';
|
|
4
|
+
|
|
5
|
+
const pgClients = {};
|
|
6
|
+
if (config.pg) {
|
|
7
|
+
const client = new pg.Pool({
|
|
8
|
+
host: config.pg?.host || '127.0.0.1',
|
|
9
|
+
port: config.pg?.port || 5432,
|
|
10
|
+
database: config.pg?.database || 'postgres',
|
|
11
|
+
user: config.pg?.user || 'postgres',
|
|
12
|
+
password: config.pg?.password || 'postgres',
|
|
13
|
+
});
|
|
14
|
+
client.init = async () => {
|
|
15
|
+
await init(client);
|
|
16
|
+
};
|
|
17
|
+
client.init();
|
|
18
|
+
pgClients.client = client;
|
|
19
|
+
}
|
|
20
|
+
export default pgClients;
|