@opengis/fastify-table 1.1.78 → 1.1.79
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/index.js +76 -76
- package/package.json +45 -45
- package/server/migrations/0.sql +80 -80
- package/server/migrations/cls.sql +39 -39
- package/server/migrations/context.sql +95 -0
- package/server/migrations/properties.sql +144 -144
- package/server/migrations/roles.sql +179 -175
- package/server/migrations/users.sql +170 -170
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/dataDelete.js +21 -21
- package/server/plugins/crud/funcs/dataInsert.js +38 -38
- package/server/plugins/crud/funcs/dataUpdate.js +50 -50
- package/server/plugins/crud/funcs/getAccess.js +48 -48
- package/server/plugins/crud/funcs/getOpt.js +13 -13
- package/server/plugins/crud/funcs/setOpt.js +21 -21
- package/server/plugins/crud/funcs/setToken.js +44 -44
- package/server/plugins/crud/funcs/utils/getFolder.js +10 -10
- package/server/plugins/crud/funcs/utils/logChanges.js +62 -62
- package/server/plugins/crud/index.js +23 -23
- package/server/plugins/hook/index.js +8 -8
- package/server/plugins/logger/errorStatus.js +19 -19
- package/server/plugins/logger/index.js +21 -21
- package/server/plugins/migration/funcs/exec.migrations.js +79 -79
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/funcs/getMeta.js +28 -28
- package/server/plugins/pg/funcs/getPG.js +33 -33
- package/server/plugins/pg/pgClients.js +21 -21
- package/server/plugins/policy/funcs/checkPolicy.js +92 -92
- package/server/plugins/policy/funcs/checkXSS.js +1 -1
- package/server/plugins/policy/index.js +12 -12
- package/server/plugins/policy/sqlInjection.js +33 -33
- package/server/plugins/redis/client.js +8 -8
- package/server/plugins/redis/funcs/redisClients.js +3 -3
- package/server/plugins/redis/index.js +17 -17
- package/server/plugins/table/funcs/addTemplateDir.js +8 -8
- package/server/plugins/table/funcs/getFilterSQL/index.js +96 -96
- package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +170 -170
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +66 -66
- package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/plugins/table/funcs/getSelect.js +19 -19
- package/server/plugins/table/funcs/getSelectMeta.js +60 -60
- package/server/plugins/table/funcs/getTemplates.js +19 -19
- package/server/plugins/table/funcs/gisIRColumn.js +72 -72
- package/server/plugins/table/funcs/loadTemplate.js +1 -1
- package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
- package/server/plugins/table/funcs/metaFormat/getSelectVal.js +38 -22
- package/server/plugins/table/funcs/metaFormat/index.js +31 -30
- package/server/plugins/table/funcs/userTemplateDir.js +1 -1
- package/server/plugins/table/index.js +13 -13
- package/server/plugins/util/funcs/eventStream.js +28 -28
- package/server/plugins/util/index.js +7 -7
- package/server/routes/cron/index.js +14 -14
- package/server/routes/crud/controllers/deleteCrud.js +36 -36
- package/server/routes/crud/controllers/insert.js +80 -80
- package/server/routes/crud/controllers/table.js +91 -91
- package/server/routes/crud/controllers/update.js +82 -82
- package/server/routes/crud/index.js +21 -21
- package/server/routes/logger/controllers/logger.file.js +92 -92
- package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
- package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
- package/server/routes/logger/index.js +17 -17
- package/server/routes/properties/controllers/properties.add.js +55 -55
- package/server/routes/properties/controllers/properties.get.js +17 -17
- package/server/routes/properties/index.js +16 -16
- package/server/routes/table/controllers/data.js +144 -144
- package/server/routes/table/controllers/filter.js +60 -60
- package/server/routes/table/controllers/form.js +42 -42
- package/server/routes/table/controllers/search.js +74 -74
- package/server/routes/table/controllers/suggest.js +84 -84
- package/server/routes/table/index.js +28 -28
- package/server/routes/table/schema.js +64 -64
- package/server/routes/util/controllers/status.monitor.js +8 -8
- package/server/routes/util/index.js +11 -11
- package/utils.js +122 -122
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import pgClients from '../../pg/pgClients.js';
|
|
2
|
-
|
|
3
|
-
import getSelect from './getSelect.js';
|
|
4
|
-
|
|
5
|
-
const limit = 50;
|
|
6
|
-
const selectMeta = {};
|
|
7
|
-
|
|
8
|
-
export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
|
|
9
|
-
if (selectMeta[name] && !nocache) return selectMeta[name];
|
|
10
|
-
|
|
11
|
-
const cls = await getSelect(name);
|
|
12
|
-
|
|
13
|
-
if (!cls) return null;
|
|
14
|
-
if (cls.arr) return cls;
|
|
15
|
-
if (!cls.sql) return null;
|
|
16
|
-
|
|
17
|
-
const { sql: original } = cls;
|
|
18
|
-
if (!original.toLowerCase) { console.log(`sql select null: ${name}`); return null; }
|
|
19
|
-
|
|
20
|
-
const sql = `with c(id,text) as (select * from (${original})q limit ${limit}) select * from c`;
|
|
21
|
-
|
|
22
|
-
/*= == meta table === */
|
|
23
|
-
|
|
24
|
-
const tableNew = original.toLowerCase().replace(/\n/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
25
|
-
.map((el) => el.split(/[ )]/)[0].replace(/[\r\n]+/g, ''));
|
|
26
|
-
|
|
27
|
-
const dataOrigin = await pg.query(sql?.replace(`limit ${limit}`, 'limit 0'));
|
|
28
|
-
|
|
29
|
-
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
30
|
-
|
|
31
|
-
// const table = getTable(original);
|
|
32
|
-
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
33
|
-
|
|
34
|
-
// column name
|
|
35
|
-
const cols = dataOrigin.fields.map((el) => el.name);
|
|
36
|
-
const type = dataOrigin.fields.map((el) => pg.pgType?.[el.dataTypeID] || 'text');
|
|
37
|
-
|
|
38
|
-
const searchColumn = cls?.searchColumn || (
|
|
39
|
-
dataOrigin.fields.find((el) => el.name === 'search') ? 'search' : dataOrigin.fields[1].name);
|
|
40
|
-
|
|
41
|
-
const searchQuery = `(${searchColumn.split(',').map((el) => `"${el}" ilike $1 `).join(' or ')})`;
|
|
42
|
-
|
|
43
|
-
const data = {
|
|
44
|
-
|
|
45
|
-
type: type.join(','),
|
|
46
|
-
cols: cols.join(','),
|
|
47
|
-
originalCols: dataOrigin1.fields.map((el) => el.name).join(','),
|
|
48
|
-
db: cls.db,
|
|
49
|
-
original: original?.includes('where') ? `select * from (${sql})q` : sql,
|
|
50
|
-
sql,
|
|
51
|
-
count,
|
|
52
|
-
searchQuery,
|
|
53
|
-
table: tableNew.join(','),
|
|
54
|
-
time: new Date().toISOString(),
|
|
55
|
-
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
selectMeta[name] = data;
|
|
59
|
-
return data;
|
|
60
|
-
}
|
|
1
|
+
import pgClients from '../../pg/pgClients.js';
|
|
2
|
+
|
|
3
|
+
import getSelect from './getSelect.js';
|
|
4
|
+
|
|
5
|
+
const limit = 50;
|
|
6
|
+
const selectMeta = {};
|
|
7
|
+
|
|
8
|
+
export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
|
|
9
|
+
if (selectMeta[name] && !nocache) return selectMeta[name];
|
|
10
|
+
|
|
11
|
+
const cls = await getSelect(name);
|
|
12
|
+
|
|
13
|
+
if (!cls) return null;
|
|
14
|
+
if (cls.arr) return cls;
|
|
15
|
+
if (!cls.sql) return null;
|
|
16
|
+
|
|
17
|
+
const { sql: original } = cls;
|
|
18
|
+
if (!original.toLowerCase) { console.log(`sql select null: ${name}`); return null; }
|
|
19
|
+
|
|
20
|
+
const sql = `with c(id,text) as (select * from (${original})q limit ${limit}) select * from c`;
|
|
21
|
+
|
|
22
|
+
/*= == meta table === */
|
|
23
|
+
|
|
24
|
+
const tableNew = original.toLowerCase().replace(/\n/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
25
|
+
.map((el) => el.split(/[ )]/)[0].replace(/[\r\n]+/g, ''));
|
|
26
|
+
|
|
27
|
+
const dataOrigin = await pg.query(sql?.replace(`limit ${limit}`, 'limit 0'));
|
|
28
|
+
|
|
29
|
+
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
30
|
+
|
|
31
|
+
// const table = getTable(original);
|
|
32
|
+
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
33
|
+
|
|
34
|
+
// column name
|
|
35
|
+
const cols = dataOrigin.fields.map((el) => el.name);
|
|
36
|
+
const type = dataOrigin.fields.map((el) => pg.pgType?.[el.dataTypeID] || 'text');
|
|
37
|
+
|
|
38
|
+
const searchColumn = cls?.searchColumn || (
|
|
39
|
+
dataOrigin.fields.find((el) => el.name === 'search') ? 'search' : dataOrigin.fields[1].name);
|
|
40
|
+
|
|
41
|
+
const searchQuery = `(${searchColumn.split(',').map((el) => `"${el}" ilike $1 `).join(' or ')})`;
|
|
42
|
+
|
|
43
|
+
const data = {
|
|
44
|
+
|
|
45
|
+
type: type.join(','),
|
|
46
|
+
cols: cols.join(','),
|
|
47
|
+
originalCols: dataOrigin1.fields.map((el) => el.name).join(','),
|
|
48
|
+
db: cls.db,
|
|
49
|
+
original: original?.includes('where') ? `select * from (${sql})q` : sql,
|
|
50
|
+
sql,
|
|
51
|
+
count,
|
|
52
|
+
searchQuery,
|
|
53
|
+
table: tableNew.join(','),
|
|
54
|
+
time: new Date().toISOString(),
|
|
55
|
+
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
selectMeta[name] = data;
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
import config from '../../../../config.js';
|
|
5
|
-
|
|
6
|
-
const loadTemplate = {};
|
|
7
|
-
|
|
8
|
-
export default async function getTemplateDir(type) {
|
|
9
|
-
if (!type) return null;
|
|
10
|
-
|
|
11
|
-
const cwd = process.cwd();
|
|
12
|
-
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
13
|
-
|
|
14
|
-
if (!loadTemplate[type]) {
|
|
15
|
-
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
16
|
-
loadTemplate[type] = typeList;
|
|
17
|
-
}
|
|
18
|
-
return loadTemplate[type];
|
|
19
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import config from '../../../../config.js';
|
|
5
|
+
|
|
6
|
+
const loadTemplate = {};
|
|
7
|
+
|
|
8
|
+
export default async function getTemplateDir(type) {
|
|
9
|
+
if (!type) return null;
|
|
10
|
+
|
|
11
|
+
const cwd = process.cwd();
|
|
12
|
+
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
13
|
+
|
|
14
|
+
if (!loadTemplate[type]) {
|
|
15
|
+
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
16
|
+
loadTemplate[type] = typeList;
|
|
17
|
+
}
|
|
18
|
+
return loadTemplate[type];
|
|
19
|
+
}
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import getSelect from './getSelect.js';
|
|
2
|
-
|
|
3
|
-
import getFilterSQL from './getFilterSQL/index.js';
|
|
4
|
-
import getTemplate from './getTemplate.js';
|
|
5
|
-
import pgClients from '../../pg/pgClients.js';
|
|
6
|
-
import config from '../../../../config.js';
|
|
7
|
-
|
|
8
|
-
export default async function gisIRColumn({
|
|
9
|
-
pg = pgClients.client, layer, column, sql, query = '1=1', filter, state, search, custom,
|
|
10
|
-
}) {
|
|
11
|
-
const time = Date.now();
|
|
12
|
-
|
|
13
|
-
const sel = await getSelect(query.cls || column);
|
|
14
|
-
|
|
15
|
-
const body = await getTemplate('table', layer);
|
|
16
|
-
const fData = await getFilterSQL({
|
|
17
|
-
table: layer,
|
|
18
|
-
filter,
|
|
19
|
-
state,
|
|
20
|
-
search,
|
|
21
|
-
custom,
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const { tlist } = await pg.one(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist from pg_class
|
|
25
|
-
where relkind in ('r','v','m')`);
|
|
26
|
-
|
|
27
|
-
const tableName = body?.table || layer;
|
|
28
|
-
if (!tlist.includes(tableName)) return { error: `table not found: ${tableName}`, status: 400 };
|
|
29
|
-
|
|
30
|
-
// eslint-disable-next-line max-len
|
|
31
|
-
const { fields } = await pg.query(`select * from (${fData?.optimizedSQL || `select * from ${tableName}`})q limit 0`);
|
|
32
|
-
|
|
33
|
-
const col = fields.find((el) => el.name === column);
|
|
34
|
-
|
|
35
|
-
if (!col) return { status: 404, message: 'not found' };
|
|
36
|
-
const colField = pg.pgType[col.dataTypeID]?.includes('[]') ? `unnest(${column})` : column;
|
|
37
|
-
|
|
38
|
-
const q = `select ${colField} as id, count(*)::int from (
|
|
39
|
-
${fData?.optimizedSQL || `select * from ${tableName} where ${body?.query || 'true'}`}
|
|
40
|
-
)t group by ${colField} order by count desc limit 15`;
|
|
41
|
-
|
|
42
|
-
if (sql) return q;
|
|
43
|
-
|
|
44
|
-
if (!body?.columns?.length) {
|
|
45
|
-
const { rows } = await pg.query(q);
|
|
46
|
-
if (sel?.arr?.length) {
|
|
47
|
-
rows.forEach((el) => {
|
|
48
|
-
const data = sel?.find((item) => item.id?.toString() === el.id?.toString());
|
|
49
|
-
Object.assign(el, data || {});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
count: rows?.reduce((acc, el) => acc + el.count, 0),
|
|
54
|
-
sql: config.local ? q : undefined,
|
|
55
|
-
rows,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const { rows } = await pg.query(q);
|
|
60
|
-
const cls = query.cls || body?.columns?.find((el) => el.name === column)?.data || col.data || col.option;
|
|
61
|
-
const select = await getSelect(cls, { val: rows.map((el) => el.id), ar: 1 });
|
|
62
|
-
rows.forEach((el) => {
|
|
63
|
-
const data = select?.arr ? select.arr?.find((item) => item.id?.toString() === el.id?.toString()) : undefined;
|
|
64
|
-
Object.assign(el, data || {});
|
|
65
|
-
});
|
|
66
|
-
return {
|
|
67
|
-
time: Date.now() - time,
|
|
68
|
-
count: rows.reduce((acc, el) => acc + el.count, 0),
|
|
69
|
-
sql: config.local ? q : undefined,
|
|
70
|
-
rows,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
1
|
+
import getSelect from './getSelect.js';
|
|
2
|
+
|
|
3
|
+
import getFilterSQL from './getFilterSQL/index.js';
|
|
4
|
+
import getTemplate from './getTemplate.js';
|
|
5
|
+
import pgClients from '../../pg/pgClients.js';
|
|
6
|
+
import config from '../../../../config.js';
|
|
7
|
+
|
|
8
|
+
export default async function gisIRColumn({
|
|
9
|
+
pg = pgClients.client, layer, column, sql, query = '1=1', filter, state, search, custom,
|
|
10
|
+
}) {
|
|
11
|
+
const time = Date.now();
|
|
12
|
+
|
|
13
|
+
const sel = await getSelect(query.cls || column);
|
|
14
|
+
|
|
15
|
+
const body = await getTemplate('table', layer);
|
|
16
|
+
const fData = await getFilterSQL({
|
|
17
|
+
table: layer,
|
|
18
|
+
filter,
|
|
19
|
+
state,
|
|
20
|
+
search,
|
|
21
|
+
custom,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const { tlist } = await pg.one(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist from pg_class
|
|
25
|
+
where relkind in ('r','v','m')`);
|
|
26
|
+
|
|
27
|
+
const tableName = body?.table || layer;
|
|
28
|
+
if (!tlist.includes(tableName)) return { error: `table not found: ${tableName}`, status: 400 };
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line max-len
|
|
31
|
+
const { fields } = await pg.query(`select * from (${fData?.optimizedSQL || `select * from ${tableName}`})q limit 0`);
|
|
32
|
+
|
|
33
|
+
const col = fields.find((el) => el.name === column);
|
|
34
|
+
|
|
35
|
+
if (!col) return { status: 404, message: 'not found' };
|
|
36
|
+
const colField = pg.pgType[col.dataTypeID]?.includes('[]') ? `unnest(${column})` : column;
|
|
37
|
+
|
|
38
|
+
const q = `select ${colField} as id, count(*)::int from (
|
|
39
|
+
${fData?.optimizedSQL || `select * from ${tableName} where ${body?.query || 'true'}`}
|
|
40
|
+
)t group by ${colField} order by count desc limit 15`;
|
|
41
|
+
|
|
42
|
+
if (sql) return q;
|
|
43
|
+
|
|
44
|
+
if (!body?.columns?.length) {
|
|
45
|
+
const { rows } = await pg.query(q);
|
|
46
|
+
if (sel?.arr?.length) {
|
|
47
|
+
rows.forEach((el) => {
|
|
48
|
+
const data = sel?.find((item) => item.id?.toString() === el.id?.toString());
|
|
49
|
+
Object.assign(el, data || {});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
count: rows?.reduce((acc, el) => acc + el.count, 0),
|
|
54
|
+
sql: config.local ? q : undefined,
|
|
55
|
+
rows,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const { rows } = await pg.query(q);
|
|
60
|
+
const cls = query.cls || body?.columns?.find((el) => el.name === column)?.data || col.data || col.option;
|
|
61
|
+
const select = await getSelect(cls, { val: rows.map((el) => el.id), ar: 1 });
|
|
62
|
+
rows.forEach((el) => {
|
|
63
|
+
const data = select?.arr ? select.arr?.find((item) => item.id?.toString() === el.id?.toString()) : undefined;
|
|
64
|
+
Object.assign(el, data || {});
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
time: Date.now() - time,
|
|
68
|
+
count: rows.reduce((acc, el) => acc + el.count, 0),
|
|
69
|
+
sql: config.local ? q : undefined,
|
|
70
|
+
rows,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
|
@@ -1,22 +1,38 @@
|
|
|
1
|
-
import getSelect from '../getSelect.js';
|
|
2
|
-
import pgClients from '../../../pg/pgClients.js';
|
|
3
|
-
import redis from '../../../redis/client.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (!cls
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import getSelect from '../getSelect.js';
|
|
2
|
+
import pgClients from '../../../pg/pgClients.js';
|
|
3
|
+
import redis from '../../../redis/client.js';
|
|
4
|
+
|
|
5
|
+
const selectIds = {};
|
|
6
|
+
export default async function getSelectVal({ pg = pgClients.client, name, values: valuesOrigin }) {
|
|
7
|
+
if (!valuesOrigin?.length) return null;
|
|
8
|
+
const values = valuesOrigin.map(el => el.toString());
|
|
9
|
+
const cls = await getSelect(name);
|
|
10
|
+
|
|
11
|
+
// === array ===
|
|
12
|
+
if (cls.arr) {
|
|
13
|
+
// console.log(name, cls?.arr, values);
|
|
14
|
+
return cls?.arr.filter(el => values.includes(el.id.toString())).reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// === sql ===
|
|
18
|
+
if (!cls?.sql) return null;
|
|
19
|
+
|
|
20
|
+
// select id column name
|
|
21
|
+
if (!selectIds[name]) selectIds[name] = await pg.queryCache(`select * from (${cls.sql})q limit 0`).then((res) => res.fields?.[0]?.name);
|
|
22
|
+
const id = selectIds[name];
|
|
23
|
+
|
|
24
|
+
// cache
|
|
25
|
+
const key = `select:${name}`;
|
|
26
|
+
const cache = (await redis.hmget(key, values)).reduce((p, el, i) => ({ ...p, [values[i]]: el }), {});
|
|
27
|
+
const filteredValues = values.filter(el => !cache[el]);
|
|
28
|
+
|
|
29
|
+
// query select
|
|
30
|
+
const q = `with c(id,text) as (select * from (${cls.sql})q where ${id} = any('{${filteredValues}}')) select * from c`;
|
|
31
|
+
const data = filteredValues.length ? await pg.query(q).then(el => el.rows) : [];
|
|
32
|
+
|
|
33
|
+
const clsAr = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
|
|
34
|
+
if (data?.length) {
|
|
35
|
+
redis.hmset(key, clsAr);
|
|
36
|
+
}
|
|
37
|
+
return clsAr;
|
|
38
|
+
}
|
|
@@ -1,30 +1,31 @@
|
|
|
1
|
-
import getTemplate from '../getTemplate.js';
|
|
2
|
-
import getSelectVal from './getSelectVal.js';
|
|
3
|
-
|
|
4
|
-
// import { getTemplate, getSelectVal } from '../../../../../utils.js';
|
|
5
|
-
|
|
6
|
-
export default async function metaFormat({ rows, table }) {
|
|
7
|
-
const loadTable = await getTemplate('table', table);
|
|
8
|
-
const selectCols = loadTable?.columns?.filter((e) => e.data);
|
|
9
|
-
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
10
|
-
if (!selectCols?.length && !metaCls?.length) return rows;
|
|
11
|
-
|
|
12
|
-
// cls & select format
|
|
13
|
-
|
|
14
|
-
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
15
|
-
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
16
|
-
if (!values.length) return null;
|
|
17
|
-
|
|
18
|
-
const cls = await getSelectVal({ name: attr.data, values });
|
|
19
|
-
if (!cls) return null;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
1
|
+
import getTemplate from '../getTemplate.js';
|
|
2
|
+
import getSelectVal from './getSelectVal.js';
|
|
3
|
+
|
|
4
|
+
// import { getTemplate, getSelectVal } from '../../../../../utils.js';
|
|
5
|
+
|
|
6
|
+
export default async function metaFormat({ rows, table }) {
|
|
7
|
+
const loadTable = await getTemplate('table', table);
|
|
8
|
+
const selectCols = loadTable?.columns?.filter((e) => e.data);
|
|
9
|
+
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
10
|
+
if (!selectCols?.length && !metaCls?.length) return rows;
|
|
11
|
+
|
|
12
|
+
// cls & select format
|
|
13
|
+
|
|
14
|
+
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
15
|
+
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
16
|
+
if (!values.length) return null;
|
|
17
|
+
|
|
18
|
+
const cls = await getSelectVal({ name: attr.data, values });
|
|
19
|
+
if (!cls) return null;
|
|
20
|
+
console.log(cls);
|
|
21
|
+
rows.forEach(el => {
|
|
22
|
+
const val = el[attr.name]?.map?.(c => cls[c.toString()] || cls[c] || c) || cls[el[attr.name].toString()] || cls[el[attr.name]] || el[attr.name];
|
|
23
|
+
if (!val) return;
|
|
24
|
+
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return null;
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
return rows;
|
|
31
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default [];
|
|
1
|
+
export default [];
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import metaFormat from './funcs/metaFormat/index.js';
|
|
2
|
-
import getFilterSQL from './funcs/getFilterSQL/index.js';
|
|
3
|
-
import getTemplate from './funcs/getTemplate.js';
|
|
4
|
-
import getSelect from './funcs/getSelect.js';
|
|
5
|
-
|
|
6
|
-
async function plugin(fastify) {
|
|
7
|
-
// fastify.decorate('metaFormat', metaFormat);
|
|
8
|
-
// fastify.decorate('getFilterSQL', getFilterSQL);
|
|
9
|
-
// fastify.decorate('getTemplate', getTemplate);
|
|
10
|
-
// fastify.decorate('getSelect', getSelect);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default plugin;
|
|
1
|
+
import metaFormat from './funcs/metaFormat/index.js';
|
|
2
|
+
import getFilterSQL from './funcs/getFilterSQL/index.js';
|
|
3
|
+
import getTemplate from './funcs/getTemplate.js';
|
|
4
|
+
import getSelect from './funcs/getSelect.js';
|
|
5
|
+
|
|
6
|
+
async function plugin(fastify) {
|
|
7
|
+
// fastify.decorate('metaFormat', metaFormat);
|
|
8
|
+
// fastify.decorate('getFilterSQL', getFilterSQL);
|
|
9
|
+
// fastify.decorate('getTemplate', getTemplate);
|
|
10
|
+
// fastify.decorate('getSelect', getSelect);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default plugin;
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
export default function eventStream(res) {
|
|
3
|
-
if (!res) return console.log;
|
|
4
|
-
const time = Date.now();
|
|
5
|
-
|
|
6
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
7
|
-
if (!res?._headerSent) {
|
|
8
|
-
res.raw.writeHead(200, {
|
|
9
|
-
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
10
|
-
'Cache-Control': 'no-cache',
|
|
11
|
-
Connection: 'keep-alive',
|
|
12
|
-
'X-Accel-Buffering': 'no',
|
|
13
|
-
});
|
|
14
|
-
res.hijack();
|
|
15
|
-
}
|
|
16
|
-
let prev = time;
|
|
17
|
-
function send(mes, finish) {
|
|
18
|
-
const t1 = Date.now();
|
|
19
|
-
res.raw.write(`data: ${finish ? 'finish' : ''} ${typeof mes === 'object' ? JSON.stringify(mes) : mes} ${t1 - prev}/${t1 - time}ms\n\n`);
|
|
20
|
-
prev = t1;
|
|
21
|
-
if (finish) {
|
|
22
|
-
res.raw.write('data: finish');
|
|
23
|
-
res.raw.end('');
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return send;
|
|
28
|
-
}
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
export default function eventStream(res) {
|
|
3
|
+
if (!res) return console.log;
|
|
4
|
+
const time = Date.now();
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
7
|
+
if (!res?._headerSent) {
|
|
8
|
+
res.raw.writeHead(200, {
|
|
9
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
10
|
+
'Cache-Control': 'no-cache',
|
|
11
|
+
Connection: 'keep-alive',
|
|
12
|
+
'X-Accel-Buffering': 'no',
|
|
13
|
+
});
|
|
14
|
+
res.hijack();
|
|
15
|
+
}
|
|
16
|
+
let prev = time;
|
|
17
|
+
function send(mes, finish) {
|
|
18
|
+
const t1 = Date.now();
|
|
19
|
+
res.raw.write(`data: ${finish ? 'finish' : ''} ${typeof mes === 'object' ? JSON.stringify(mes) : mes} ${t1 - prev}/${t1 - time}ms\n\n`);
|
|
20
|
+
prev = t1;
|
|
21
|
+
if (finish) {
|
|
22
|
+
res.raw.write('data: finish');
|
|
23
|
+
res.raw.end('');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return send;
|
|
28
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import eventStream from './funcs/eventStream.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
// fastify.decorate('eventStream', eventStream);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export default plugin;
|
|
1
|
+
import eventStream from './funcs/eventStream.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
// fastify.decorate('eventStream', eventStream);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default plugin;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import cronApi from './controllers/cronApi.js';
|
|
2
|
-
|
|
3
|
-
const cronSchema = {
|
|
4
|
-
params: {
|
|
5
|
-
name: { type: 'string', pattern: '^([\\d\\w._-]+)$' },
|
|
6
|
-
},
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
async function plugin(fastify, config = {}) {
|
|
10
|
-
const prefix = config.prefix || '/api';
|
|
11
|
-
fastify.get(`${prefix}/cron/:name`, { schema: cronSchema }, cronApi);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default plugin;
|
|
1
|
+
import cronApi from './controllers/cronApi.js';
|
|
2
|
+
|
|
3
|
+
const cronSchema = {
|
|
4
|
+
params: {
|
|
5
|
+
name: { type: 'string', pattern: '^([\\d\\w._-]+)$' },
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
async function plugin(fastify, config = {}) {
|
|
10
|
+
const prefix = config.prefix || '/api';
|
|
11
|
+
fastify.get(`${prefix}/cron/:name`, { schema: cronSchema }, cronApi);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default plugin;
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
dataDelete, getTemplate, getAccess, applyHook, getToken, config,
|
|
3
|
-
} from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
export default async function deleteCrud(req) {
|
|
6
|
-
const { user, params = {} } = req || {};
|
|
7
|
-
const hookData = await applyHook('preDelete', {
|
|
8
|
-
table: params?.table, id: params?.id, user,
|
|
9
|
-
});
|
|
10
|
-
if (hookData?.message && hookData?.status) {
|
|
11
|
-
return { message: hookData?.message, status: hookData?.status };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const tokenData = await getToken({
|
|
15
|
-
uid: user.uid, token: params.table, json: 1,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const { table: del, id } = hookData || tokenData || (config.auth?.disable ? req.params : {});
|
|
19
|
-
const { actions = [] } = await getAccess({ table: del, id, user }) || {};
|
|
20
|
-
|
|
21
|
-
if (!actions.includes('del') && !config?.local && !tokenData) {
|
|
22
|
-
return { message: 'access restricted', status: 403 };
|
|
23
|
-
}
|
|
24
|
-
const loadTemplate = await getTemplate('table', del);
|
|
25
|
-
|
|
26
|
-
const { table } = loadTemplate || hookData || tokenData || req.params || {};
|
|
27
|
-
|
|
28
|
-
if (!table) return { status: 404, message: 'table is required' };
|
|
29
|
-
if (!id) return { status: 404, message: 'id is required' };
|
|
30
|
-
|
|
31
|
-
const data = await dataDelete({
|
|
32
|
-
table, id, uid: user?.uid,
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
|
|
36
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
dataDelete, getTemplate, getAccess, applyHook, getToken, config,
|
|
3
|
+
} from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
export default async function deleteCrud(req) {
|
|
6
|
+
const { user, params = {} } = req || {};
|
|
7
|
+
const hookData = await applyHook('preDelete', {
|
|
8
|
+
table: params?.table, id: params?.id, user,
|
|
9
|
+
});
|
|
10
|
+
if (hookData?.message && hookData?.status) {
|
|
11
|
+
return { message: hookData?.message, status: hookData?.status };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const tokenData = await getToken({
|
|
15
|
+
uid: user.uid, token: params.table, json: 1,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const { table: del, id } = hookData || tokenData || (config.auth?.disable ? req.params : {});
|
|
19
|
+
const { actions = [] } = await getAccess({ table: del, id, user }) || {};
|
|
20
|
+
|
|
21
|
+
if (!actions.includes('del') && !config?.local && !tokenData) {
|
|
22
|
+
return { message: 'access restricted', status: 403 };
|
|
23
|
+
}
|
|
24
|
+
const loadTemplate = await getTemplate('table', del);
|
|
25
|
+
|
|
26
|
+
const { table } = loadTemplate || hookData || tokenData || req.params || {};
|
|
27
|
+
|
|
28
|
+
if (!table) return { status: 404, message: 'table is required' };
|
|
29
|
+
if (!id) return { status: 404, message: 'id is required' };
|
|
30
|
+
|
|
31
|
+
const data = await dataDelete({
|
|
32
|
+
table, id, uid: user?.uid,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
|
|
36
|
+
}
|