@opengis/bi 1.2.30 → 1.2.32

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.
Files changed (75) hide show
  1. package/README.md +92 -92
  2. package/dist/bi.js +1 -1
  3. package/dist/bi.umd.cjs +88 -86
  4. package/dist/{import-file-B4o9p2_2.js → import-file-D-ISqB7l.js} +1772 -1593
  5. package/dist/style.css +1 -1
  6. package/dist/{vs-funnel-bar-BOWwPnjW.js → vs-funnel-bar-aoZzvriV.js} +3 -3
  7. package/dist/{vs-list-D8jGusRT.js → vs-list-CBkyJSBj.js} +53 -32
  8. package/dist/{vs-map-BGplOwpB.js → vs-map-C3C11qmT.js} +102 -91
  9. package/dist/{vs-map-cluster-CHQJV2As.js → vs-map-cluster-BsPmHIMx.js} +91 -81
  10. package/dist/{vs-number-jPqxFQ6d.js → vs-number-d58ftpH5.js} +3 -3
  11. package/dist/{vs-table-BDgIvJbY.js → vs-table-BHa5Velm.js} +6 -6
  12. package/dist/{vs-text-DcHOffy9.js → vs-text-Bq87gMTx.js} +4 -4
  13. package/package.json +77 -75
  14. package/plugin.js +21 -22
  15. package/server/helpers/mdToHTML.js +17 -17
  16. package/server/migrations/bi.dataset.sql +46 -46
  17. package/server/migrations/bi.sql +115 -112
  18. package/server/plugins/docs.js +48 -48
  19. package/server/plugins/hook.js +89 -89
  20. package/server/plugins/vite.js +81 -81
  21. package/server/routes/dashboard/controllers/dashboard.import.js +103 -103
  22. package/server/routes/dashboard/controllers/dashboard.js +158 -157
  23. package/server/routes/dashboard/controllers/dashboard.list.js +60 -60
  24. package/server/routes/dashboard/controllers/utils/yaml.js +11 -11
  25. package/server/routes/dashboard/index.mjs +26 -26
  26. package/server/routes/data/controllers/data.js +230 -229
  27. package/server/routes/data/controllers/util/chartSQL.js +49 -49
  28. package/server/routes/data/controllers/util/normalizeData.js +65 -65
  29. package/server/routes/data/index.mjs +38 -38
  30. package/server/routes/dataset/controllers/bi.dataset.list.js +29 -29
  31. package/server/routes/dataset/controllers/bi.db.list.js +19 -19
  32. package/server/routes/dataset/controllers/comment.js +55 -55
  33. package/server/routes/dataset/controllers/createDatasetPost.js +134 -134
  34. package/server/routes/dataset/controllers/data.js +149 -149
  35. package/server/routes/dataset/controllers/dbTablePreview.js +58 -58
  36. package/server/routes/dataset/controllers/dbTables.js +34 -34
  37. package/server/routes/dataset/controllers/delete.js +40 -40
  38. package/server/routes/dataset/controllers/deleteDataset.js +52 -52
  39. package/server/routes/dataset/controllers/editDataset.js +90 -90
  40. package/server/routes/dataset/controllers/export.js +214 -214
  41. package/server/routes/dataset/controllers/form.js +99 -99
  42. package/server/routes/dataset/controllers/format.js +46 -46
  43. package/server/routes/dataset/controllers/insert.js +47 -47
  44. package/server/routes/dataset/controllers/table.js +68 -68
  45. package/server/routes/dataset/controllers/update.js +43 -43
  46. package/server/routes/dataset/index.mjs +132 -132
  47. package/server/routes/dataset/utils/convertJSONToCSV.js +17 -17
  48. package/server/routes/dataset/utils/convertJSONToXls.js +47 -47
  49. package/server/routes/dataset/utils/createTableQuery.js +59 -59
  50. package/server/routes/dataset/utils/datasetForms.js +1 -1
  51. package/server/routes/dataset/utils/descriptionList.js +45 -45
  52. package/server/routes/dataset/utils/downloadRemoteFile.js +58 -58
  53. package/server/routes/dataset/utils/executeQuery.js +46 -46
  54. package/server/routes/dataset/utils/getLayersData.js +106 -106
  55. package/server/routes/dataset/utils/getTableData.js +46 -46
  56. package/server/routes/dataset/utils/insertDataQuery.js +12 -12
  57. package/server/routes/dataset/utils/metaFormat.js +24 -24
  58. package/server/routes/edit/controllers/dashboard.add.js +36 -36
  59. package/server/routes/edit/controllers/dashboard.delete.js +39 -39
  60. package/server/routes/edit/controllers/dashboard.edit.js +61 -61
  61. package/server/routes/edit/controllers/widget.add.js +78 -78
  62. package/server/routes/edit/controllers/widget.del.js +58 -58
  63. package/server/routes/edit/controllers/widget.edit.js +115 -115
  64. package/server/routes/edit/index.mjs +33 -33
  65. package/server/routes/map/controllers/cluster.js +125 -125
  66. package/server/routes/map/controllers/clusterVtile.js +166 -166
  67. package/server/routes/map/controllers/geojson.js +127 -127
  68. package/server/routes/map/controllers/heatmap.js +118 -118
  69. package/server/routes/map/controllers/map.js +69 -69
  70. package/server/routes/map/controllers/utils/downloadClusterData.js +44 -44
  71. package/server/routes/map/controllers/vtile.js +183 -183
  72. package/server/routes/map/index.mjs +32 -32
  73. package/server/templates/page/login.html +58 -58
  74. package/server/utils/getWidget.js +118 -117
  75. package/utils.js +12 -12
@@ -1,58 +1,58 @@
1
- import { pgClients } from "@opengis/fastify-table/utils.js";
2
-
3
- const q = `select nspname||'.'||relname as table, json_agg(json_build_object('name',attname, 'type', a.atttypid::regtype, 'description', coalesce(col_description(attrelid, attnum),attname))) as columns
4
- from pg_attribute a
5
- left join pg_catalog.pg_attrdef d ON (a.attrelid, a.attnum) = (d.adrelid, d.adnum)
6
- JOIN pg_class AS i
7
- ON i.oid = a.attrelid
8
- JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
9
- where a.attnum > 0 and nspname||'.'||relname = $1
10
- and not a.attisdropped
11
- group by nspname||'.'||relname limit 1`;
12
-
13
- export default async function dbTablePreview({ pg = pgClients.client, params = {}, query = {} }) {
14
- if (!params?.name) {
15
- return { message: 'not enough params: name', status: 400 };
16
- }
17
-
18
- if (query.sql) return q;
19
- try {
20
- const { table, columns } = await pg
21
- .query(q, [params.name.replace(/"/g, '')])
22
- .then(el => el.rows?.[0] || {});
23
- if (!table) {
24
- return { message: 'table not found', status: 404 };
25
- }
26
-
27
- const { count = 0 } = await pg.query('select reltuples as count from pg_class where oid = to_regclass($1)', [params.name])
28
- .then(el => el.rows?.[0] || {});
29
- const geom = columns.find((el) => el.type === 'geometry')?.name;
30
- const { bounds, extentStr } = geom
31
- ? await pg.query(`select
32
- count(*),
33
- st_asgeojson(st_extent(${geom}))::json as bounds,
34
- replace(regexp_replace(st_extent(${geom})::box2d::text,'BOX\\(|\\)','','g'),' ',',') as "extentStr"
35
- from ${params.name}`).then(el => el.rows?.[0] || {})
36
- : {};
37
- const extent = extentStr ? extentStr.split(',') : undefined;
38
-
39
- const systemColumns = [
40
- 'uid',
41
- 'files',
42
- 'editor_date',
43
- 'cdate',
44
- 'editor_id',
45
- geom,
46
- ];
47
-
48
- const columnList = columns.map((el) => el?.name).filter((el) => !systemColumns.includes(el));
49
- const { rows = [] } = await pg.query(`select ${columnList.map(el => `"${el.replace(/'/g, "''")}"`).join(',')} ${geom ? ', st_asgeojson(geom)::json as geom' : ''} from ${params.name.replace(/'/g, '')} limit 10`);
50
-
51
- return {
52
- count, geom: !!geom, bounds, extent, columns, rows,
53
- };
54
- }
55
- catch (err) {
56
- return { error: err.toString(), status: 500 };
57
- }
58
- }
1
+ import { pgClients } from "@opengis/fastify-table/utils.js";
2
+
3
+ const q = `select nspname||'.'||relname as table, json_agg(json_build_object('name',attname, 'type', a.atttypid::regtype, 'description', coalesce(col_description(attrelid, attnum),attname))) as columns
4
+ from pg_attribute a
5
+ left join pg_catalog.pg_attrdef d ON (a.attrelid, a.attnum) = (d.adrelid, d.adnum)
6
+ JOIN pg_class AS i
7
+ ON i.oid = a.attrelid
8
+ JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
9
+ where a.attnum > 0 and nspname||'.'||relname = $1
10
+ and not a.attisdropped
11
+ group by nspname||'.'||relname limit 1`;
12
+
13
+ export default async function dbTablePreview({ pg = pgClients.client, params = {}, query = {} }) {
14
+ if (!params?.name) {
15
+ return { message: 'not enough params: name', status: 400 };
16
+ }
17
+
18
+ if (query.sql) return q;
19
+ try {
20
+ const { table, columns } = await pg
21
+ .query(q, [params.name.replace(/"/g, '')])
22
+ .then(el => el.rows?.[0] || {});
23
+ if (!table) {
24
+ return { message: 'table not found', status: 404 };
25
+ }
26
+
27
+ const { count = 0 } = await pg.query('select reltuples as count from pg_class where oid = to_regclass($1)', [params.name])
28
+ .then(el => el.rows?.[0] || {});
29
+ const geom = columns.find((el) => el.type === 'geometry')?.name;
30
+ const { bounds, extentStr } = geom
31
+ ? await pg.query(`select
32
+ count(*),
33
+ st_asgeojson(st_extent(${geom}))::json as bounds,
34
+ replace(regexp_replace(st_extent(${geom})::box2d::text,'BOX\\(|\\)','','g'),' ',',') as "extentStr"
35
+ from ${params.name}`).then(el => el.rows?.[0] || {})
36
+ : {};
37
+ const extent = extentStr ? extentStr.split(',') : undefined;
38
+
39
+ const systemColumns = [
40
+ 'uid',
41
+ 'files',
42
+ 'editor_date',
43
+ 'cdate',
44
+ 'editor_id',
45
+ geom,
46
+ ];
47
+
48
+ const columnList = columns.map((el) => el?.name).filter((el) => !systemColumns.includes(el));
49
+ const { rows = [] } = await pg.query(`select ${columnList.map(el => `"${el.replace(/'/g, "''")}"`).join(',')} ${geom ? ', st_asgeojson(geom)::json as geom' : ''} from ${params.name.replace(/'/g, '')} limit 10`);
50
+
51
+ return {
52
+ count, geom: !!geom, bounds, extent, columns, rows,
53
+ };
54
+ }
55
+ catch (err) {
56
+ return { error: err.toString(), status: 500 };
57
+ }
58
+ }
@@ -1,34 +1,34 @@
1
- import { pgClients } from "@opengis/fastify-table/utils.js";
2
-
3
- export default async function dbTables({ pg = pgClients.client, query = {} }) {
4
- const q = `select
5
- t.table_schema ||'."'|| t.table_name ||'"' as table,
6
- obj_description(to_regclass(t.table_schema ||'."'|| t.table_name||'"')) as description,
7
- t.table_schema as schema,
8
- (select reltuples from pg_class where oid = to_regclass(t.table_schema ||'."'|| t.table_name||'"') ) as total,
9
- coalesce(isgeom,false) as isgeom
10
-
11
- from information_schema.tables t
12
- left join lateral(
13
- select true as isgeom from information_schema.columns c
14
- where c.table_name = t.table_name
15
- and c.table_schema = t.table_schema and 'geometry'=c.udt_name limit 1
16
- )c on 1=1
17
-
18
- where t.table_type = 'BASE TABLE'
19
- and t.table_schema not in ('public','log','admin','feature_ir','gis', 'setting')
20
- and t.table_name not like '%.%'
21
- and regexp_replace(t.table_name, '^[[:digit:]]', '', 'g') = t.table_name
22
- and 1=(SELECT count(*) FROM pg_catalog.pg_constraint con
23
- INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
24
- INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
25
- WHERE nsp.nspname = t.table_schema AND rel.relname = t.table_name and contype='p'
26
- )
27
- and isgeom
28
- order by total desc`;
29
-
30
- if (query.sql) return q;
31
-
32
- const { rows = [] } = await pg.queryCache(q, { time: 0 });
33
- return { rows };
34
- }
1
+ import { pgClients } from "@opengis/fastify-table/utils.js";
2
+
3
+ export default async function dbTables({ pg = pgClients.client, query = {} }) {
4
+ const q = `select
5
+ t.table_schema ||'."'|| t.table_name ||'"' as table,
6
+ obj_description(to_regclass(t.table_schema ||'."'|| t.table_name||'"')) as description,
7
+ t.table_schema as schema,
8
+ (select reltuples from pg_class where oid = to_regclass(t.table_schema ||'."'|| t.table_name||'"') ) as total,
9
+ coalesce(isgeom,false) as isgeom
10
+
11
+ from information_schema.tables t
12
+ left join lateral(
13
+ select true as isgeom from information_schema.columns c
14
+ where c.table_name = t.table_name
15
+ and c.table_schema = t.table_schema and 'geometry'=c.udt_name limit 1
16
+ )c on 1=1
17
+
18
+ where t.table_type = 'BASE TABLE'
19
+ and t.table_schema not in ('public','log','admin','feature_ir','gis', 'setting')
20
+ and t.table_name not like '%.%'
21
+ and regexp_replace(t.table_name, '^[[:digit:]]', '', 'g') = t.table_name
22
+ and 1=(SELECT count(*) FROM pg_catalog.pg_constraint con
23
+ INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
24
+ INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
25
+ WHERE nsp.nspname = t.table_schema AND rel.relname = t.table_name and contype='p'
26
+ )
27
+ and isgeom
28
+ order by total desc`;
29
+
30
+ if (query.sql) return q;
31
+
32
+ const { rows = [] } = await pg.queryCache(q, { time: 0 });
33
+ return { rows };
34
+ }
@@ -1,40 +1,40 @@
1
- import {
2
- config, getToken, dataDelete, pgClients,
3
- } from '@opengis/fastify-table/utils.js';
4
-
5
- export default async function datasetDataDelete({
6
- pg = pgClients.client, params = {}, user = {},
7
- }) {
8
- if (!user?.uid) {
9
- return { message: 'access restricted', status: 403 };
10
- }
11
-
12
- const tokenData = await getToken({
13
- uid: user?.uid,
14
- token: params.id,
15
- mode: 'a',
16
- json: 1,
17
- });
18
-
19
- if (!tokenData && !config.local && !config.debug) {
20
- return { message: 'token not allow', status: 403 };
21
- }
22
-
23
- const dataset = await pg.query('select dataset_id as id, table_name as table from bi.dataset where dataset_id=$1', [tokenData?.dataset || params.id])
24
- .then(el => el.rows[0] || {});
25
-
26
- const table = tokenData?.table || dataset.table;
27
-
28
- if (!dataset.id) {
29
- return { message: 'dataset not found', status: 404 };
30
- }
31
-
32
- const res = await dataDelete({
33
- pg,
34
- id: tokenData?.id || params?.object_id,
35
- table,
36
- uid: user?.uid,
37
- });
38
-
39
- return { rowCount: res.rowCount, msg: !res.rowCount ? res : null };
40
- }
1
+ import {
2
+ config, getToken, dataDelete, pgClients,
3
+ } from '@opengis/fastify-table/utils.js';
4
+
5
+ export default async function datasetDataDelete({
6
+ pg = pgClients.client, params = {}, user = {},
7
+ }) {
8
+ if (!user?.uid) {
9
+ return { message: 'access restricted', status: 403 };
10
+ }
11
+
12
+ const tokenData = await getToken({
13
+ uid: user?.uid,
14
+ token: params.id,
15
+ mode: 'a',
16
+ json: 1,
17
+ });
18
+
19
+ if (!tokenData && !config.local && !config.debug) {
20
+ return { message: 'token not allow', status: 403 };
21
+ }
22
+
23
+ const dataset = await pg.query('select dataset_id as id, table_name as table from bi.dataset where dataset_id=$1', [tokenData?.dataset || params.id])
24
+ .then(el => el.rows[0] || {});
25
+
26
+ const table = tokenData?.table || dataset.table;
27
+
28
+ if (!dataset.id) {
29
+ return { message: 'dataset not found', status: 404 };
30
+ }
31
+
32
+ const res = await dataDelete({
33
+ pg,
34
+ id: tokenData?.id || params?.object_id,
35
+ table,
36
+ uid: user?.uid,
37
+ });
38
+
39
+ return { rowCount: res.rowCount, msg: !res.rowCount ? res : null };
40
+ }
@@ -1,52 +1,52 @@
1
- import { dataDelete, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- /**
4
- * Видалення набору даних
5
- *
6
- * @method DELETE
7
- * @summary Видалення набору даних
8
- * @priority 4
9
- * @alias deleteDataset
10
- * @type api
11
- * @tag bi
12
- * @param {Object} query.id Dataset ID
13
- * @errors 400,500
14
- * @returns {Number} status Номер помилки
15
- * @returns {String} error Опис помилки
16
- * @returns {Object} rows Масив з колонками таблиці
17
- */
18
-
19
- export default async function deleteDataset({
20
- pg = pgClients.client, query = {}, user = {},
21
- }) {
22
- if (!user?.uid) {
23
- return { message: 'access restricted', status: 403 };
24
- }
25
-
26
- if (!query?.id) {
27
- return { message: 'not enough params: id', status: 404 };
28
- }
29
-
30
- const dataset = await pg.query('select dataset_id as id, table_name as table from bi.dataset where dataset_id=$1', [query.id])
31
- .then(el => el.rows[0] || {});
32
-
33
- if (!dataset.id) {
34
- return { message: 'dataset not found', status: 404 };
35
- }
36
-
37
- const table = dataset.table && pg.pk[dataset.table] ? dataset.table : dataset.table?.replace(/"/g, '');
38
-
39
- const dropTable = table && pg.pk?.[table] && table.startsWith('data_user.');
40
- if (dropTable) {
41
- await pg.query(`drop table if exists ${table}`);
42
- }
43
-
44
- await dataDelete({
45
- pg,
46
- table: 'bi.dataset',
47
- id: dataset.id,
48
- uid: user.uid,
49
- });
50
-
51
- return { message: { id: dataset?.id, table: dropTable ? dataset.table : undefined }, status: 200 };
52
- }
1
+ import { dataDelete, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ /**
4
+ * Видалення набору даних
5
+ *
6
+ * @method DELETE
7
+ * @summary Видалення набору даних
8
+ * @priority 4
9
+ * @alias deleteDataset
10
+ * @type api
11
+ * @tag bi
12
+ * @param {Object} query.id Dataset ID
13
+ * @errors 400,500
14
+ * @returns {Number} status Номер помилки
15
+ * @returns {String} error Опис помилки
16
+ * @returns {Object} rows Масив з колонками таблиці
17
+ */
18
+
19
+ export default async function deleteDataset({
20
+ pg = pgClients.client, query = {}, user = {},
21
+ }) {
22
+ if (!user?.uid) {
23
+ return { message: 'access restricted', status: 403 };
24
+ }
25
+
26
+ if (!query?.id) {
27
+ return { message: 'not enough params: id', status: 404 };
28
+ }
29
+
30
+ const dataset = await pg.query('select dataset_id as id, table_name as table from bi.dataset where dataset_id=$1', [query.id])
31
+ .then(el => el.rows[0] || {});
32
+
33
+ if (!dataset.id) {
34
+ return { message: 'dataset not found', status: 404 };
35
+ }
36
+
37
+ const table = dataset.table && pg.pk[dataset.table] ? dataset.table : dataset.table?.replace(/"/g, '');
38
+
39
+ const dropTable = table && pg.pk?.[table] && table.startsWith('data_user.');
40
+ if (dropTable) {
41
+ await pg.query(`drop table if exists ${table}`);
42
+ }
43
+
44
+ await dataDelete({
45
+ pg,
46
+ table: 'bi.dataset',
47
+ id: dataset.id,
48
+ uid: user.uid,
49
+ });
50
+
51
+ return { message: { id: dataset?.id, table: dropTable ? dataset.table : undefined }, status: 200 };
52
+ }
@@ -1,90 +1,90 @@
1
- import { dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- const columnTypeMatch = {
4
- text: 'text',
5
- select: 'text',
6
- date: 'date',
7
- 'yes/no': 'boolean',
8
- badge: 'text',
9
- number: 'numeric',
10
- tags: 'text[]',
11
- geom: 'geom',
12
- };
13
-
14
- /**
15
- * Редагування структури набору даних
16
- *
17
- * @method PUT
18
- * @summary Редагування структури набору даних
19
- * @priority 4
20
- * @alias editDataset
21
- * @type api
22
- * @tag bi
23
- * @param {Object} query.id Видалення ID
24
- * @errors 400,500
25
- * @returns {Number} status Номер помилки
26
- * @returns {String} error Опис помилки
27
- * @returns {Object} rows Масив з колонками таблиці
28
- */
29
-
30
- export default async function editDataset(req) {
31
- const {
32
- pg = pgClients.client, query = {}, body = {}, user = {},
33
- } = req;
34
- if (!user?.uid) {
35
- return { message: 'access restricted', status: 403 };
36
- }
37
-
38
- if (!query?.id) {
39
- return { message: 'not enough params: id', status: 404 };
40
- }
41
-
42
- if (!body.column_list?.length) {
43
- return { message: 'not enough params: columns', status: 400 };
44
- }
45
-
46
- const dataset = await pg.query('select dataset_id as id, table_name as table, column_list as columns from bi.dataset where dataset_id=$1', [query.id])
47
- .then(el => el.rows[0] || {});
48
-
49
- if (!dataset?.id) {
50
- return { message: 'dataset not found', status: 404 };
51
- }
52
-
53
- if (!dataset.table || !pg.pk?.[dataset.table]) {
54
- return { message: `table not found: ${dataset.table}`, status: 404 };
55
- }
56
-
57
- if (!dataset.table.startsWith('data_user.')) {
58
- return { message: 'access restricted: source', status: 403 };
59
- }
60
-
61
- const { fields = [] } = await pg.query(`select * from ${dataset.table} limit 0`);
62
- const columnList = fields.map((col) => col.name);
63
-
64
- const columns = (dataset.columns || [])
65
- .concat(body.column_list.filter((col) => !col.name || !columnList.includes(col.name)))
66
- .map((col, idx) => ({
67
- ...col,
68
- name: col.name && columnList.includes(col.name) ? col.name : `col_${idx}`,
69
- disabled: col.name && !body.column_list.find((item) => item.name === col.name),
70
- }));
71
-
72
- await dataUpdate({
73
- pg,
74
- table: 'bi.dataset',
75
- data: { column_list: columns },
76
- id: dataset.id,
77
- uid: user.uid,
78
- });
79
-
80
- const sqlList = columns
81
- .filter((col) => col.name.startsWith('col_') || (col.title && dataset.columns?.find?.((item) => item.name === col.name)?.title !== col.title))
82
- .map((col) => `alter table ${dataset.table} add column if not exists ${col.name} ${columnTypeMatch[col.type] || 'text'};
83
- comment on column ${dataset.table}."${col.name}" is '${(col.title || col.name).replace(/'/g, "''")}'`);
84
-
85
- if (sqlList.length) {
86
- await pg.query(sqlList.join(';'));
87
- }
88
-
89
- return { message: { id: dataset?.id, table: dataset.table, columns }, status: 200 };
90
- }
1
+ import { dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ const columnTypeMatch = {
4
+ text: 'text',
5
+ select: 'text',
6
+ date: 'date',
7
+ 'yes/no': 'boolean',
8
+ badge: 'text',
9
+ number: 'numeric',
10
+ tags: 'text[]',
11
+ geom: 'geom',
12
+ };
13
+
14
+ /**
15
+ * Редагування структури набору даних
16
+ *
17
+ * @method PUT
18
+ * @summary Редагування структури набору даних
19
+ * @priority 4
20
+ * @alias editDataset
21
+ * @type api
22
+ * @tag bi
23
+ * @param {Object} query.id Видалення ID
24
+ * @errors 400,500
25
+ * @returns {Number} status Номер помилки
26
+ * @returns {String} error Опис помилки
27
+ * @returns {Object} rows Масив з колонками таблиці
28
+ */
29
+
30
+ export default async function editDataset(req) {
31
+ const {
32
+ pg = pgClients.client, query = {}, body = {}, user = {},
33
+ } = req;
34
+ if (!user?.uid) {
35
+ return { message: 'access restricted', status: 403 };
36
+ }
37
+
38
+ if (!query?.id) {
39
+ return { message: 'not enough params: id', status: 404 };
40
+ }
41
+
42
+ if (!body.column_list?.length) {
43
+ return { message: 'not enough params: columns', status: 400 };
44
+ }
45
+
46
+ const dataset = await pg.query('select dataset_id as id, table_name as table, column_list as columns from bi.dataset where dataset_id=$1', [query.id])
47
+ .then(el => el.rows[0] || {});
48
+
49
+ if (!dataset?.id) {
50
+ return { message: 'dataset not found', status: 404 };
51
+ }
52
+
53
+ if (!dataset.table || !pg.pk?.[dataset.table]) {
54
+ return { message: `table not found: ${dataset.table}`, status: 404 };
55
+ }
56
+
57
+ if (!dataset.table.startsWith('data_user.')) {
58
+ return { message: 'access restricted: source', status: 403 };
59
+ }
60
+
61
+ const { fields = [] } = await pg.query(`select * from ${dataset.table} limit 0`);
62
+ const columnList = fields.map((col) => col.name);
63
+
64
+ const columns = (dataset.columns || [])
65
+ .concat(body.column_list.filter((col) => !col.name || !columnList.includes(col.name)))
66
+ .map((col, idx) => ({
67
+ ...col,
68
+ name: col.name && columnList.includes(col.name) ? col.name : `col_${idx}`,
69
+ disabled: col.name && !body.column_list.find((item) => item.name === col.name),
70
+ }));
71
+
72
+ await dataUpdate({
73
+ pg,
74
+ table: 'bi.dataset',
75
+ data: { column_list: columns },
76
+ id: dataset.id,
77
+ uid: user.uid,
78
+ });
79
+
80
+ const sqlList = columns
81
+ .filter((col) => col.name.startsWith('col_') || (col.title && dataset.columns?.find?.((item) => item.name === col.name)?.title !== col.title))
82
+ .map((col) => `alter table ${dataset.table} add column if not exists ${col.name} ${columnTypeMatch[col.type] || 'text'};
83
+ comment on column ${dataset.table}."${col.name}" is '${(col.title || col.name).replace(/'/g, "''")}'`);
84
+
85
+ if (sqlList.length) {
86
+ await pg.query(sqlList.join(';'));
87
+ }
88
+
89
+ return { message: { id: dataset?.id, table: dataset.table, columns }, status: 200 };
90
+ }