@opengis/fastify-table 1.0.74 → 1.0.76

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 (64) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +237 -229
  3. package/README.md +26 -26
  4. package/config.js +10 -10
  5. package/cron/controllers/cronApi.js +22 -22
  6. package/cron/controllers/utils/cronList.js +1 -1
  7. package/cron/funcs/addCron.js +131 -131
  8. package/cron/index.js +10 -10
  9. package/crud/controllers/deleteCrud.js +1 -1
  10. package/crud/controllers/utils/checkXSS.js +45 -45
  11. package/crud/controllers/utils/xssInjection.js +72 -72
  12. package/crud/funcs/dataDelete.js +15 -15
  13. package/crud/funcs/dataUpdate.js +24 -24
  14. package/crud/funcs/getAccess.js +2 -1
  15. package/crud/funcs/getToken.js +27 -27
  16. package/crud/funcs/isFileExists.js +13 -13
  17. package/crud/funcs/setToken.js +53 -53
  18. package/crud/index.js +36 -36
  19. package/index.js +97 -97
  20. package/migration/exec.migrations.js +75 -75
  21. package/notification/controllers/testEmail.js +49 -49
  22. package/notification/funcs/sendNotification.js +111 -111
  23. package/notification/funcs/utils/sendEmail.js +39 -39
  24. package/notification/index.js +38 -38
  25. package/package.json +26 -26
  26. package/pg/funcs/getPG.js +30 -30
  27. package/redis/funcs/getRedis.js +23 -23
  28. package/server/migrations/0.sql +64 -13
  29. package/server/migrations/crm.sql +150 -150
  30. package/server/migrations/log.sql +43 -43
  31. package/server.js +14 -14
  32. package/table/controllers/card.js +44 -44
  33. package/table/controllers/filter.js +37 -37
  34. package/table/controllers/form.js +28 -28
  35. package/table/controllers/search.js +72 -72
  36. package/table/controllers/suggest.js +62 -62
  37. package/table/controllers/table.js +44 -44
  38. package/table/controllers/utils/getSelectMeta.js +66 -66
  39. package/table/controllers/utils/getTemplate.js +28 -28
  40. package/table/controllers/utils/getTemplates.js +18 -18
  41. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  42. package/table/funcs/metaFormat/index.js +27 -27
  43. package/table/index.js +78 -78
  44. package/test/api/crud.test.js +18 -11
  45. package/test/api/crud.xss.test.js +72 -72
  46. package/test/api/notification.test.js +37 -37
  47. package/test/api/table.test.js +57 -57
  48. package/test/api/widget.test.js +114 -114
  49. package/test/config.example +18 -18
  50. package/test/funcs/crud.test.js +76 -76
  51. package/test/funcs/notification.test.js +31 -31
  52. package/test/funcs/pg.test.js +34 -34
  53. package/test/funcs/redis.test.js +19 -19
  54. package/test/templates/cls/test.json +9 -9
  55. package/test/templates/form/cp_building.form.json +32 -32
  56. package/test/templates/select/account_id.json +3 -3
  57. package/test/templates/select/storage.data.json +2 -2
  58. package/test/templates/table/gis.dataset.table.json +20 -20
  59. package/util/controllers/next.id.js +4 -4
  60. package/util/controllers/properties.add.js +50 -50
  61. package/util/controllers/properties.get.js +19 -19
  62. package/util/controllers/status.monitor.js +3 -0
  63. package/util/index.js +4 -3
  64. package/widget/index.js +40 -40
@@ -1,131 +1,131 @@
1
- import { createHash } from 'crypto';
2
-
3
- import cronList from '../controllers/utils/cronList.js';
4
- import getRedis from '../../redis/funcs/getRedis.js';
5
- import getPG from '../../pg/funcs/getPG.js';
6
-
7
- const md5 = (string) => createHash('md5').update(string).digest('hex');
8
-
9
- async function verifyUnique(name, config, rclient) {
10
- const cronId = config.port || 3000 + md5(name);
11
- // one per node check
12
- const key = `cron:unique:${cronId}`;
13
- const unique = await rclient.setnx(key, 1);
14
- const ttl = await rclient.ttl(key);
15
- if (!unique && ttl !== -1) {
16
- return false;
17
- }
18
- await rclient.expire(key, 20);
19
- return true;
20
- }
21
-
22
- const intervalStringMs = {
23
- everyMin: 1000 * 60,
24
- tenMin: 1000 * 60 * 10,
25
- everyHour: 1000 * 60 * 60,
26
- isHalfday: 1000 * 60 * 60 * 12,
27
- dailyHour: 1000 * 60 * 60 * 24,
28
- };
29
-
30
- const interval2ms = {
31
- string: (interval) => {
32
- const date = new Date();
33
- const intervarSplit = interval.match(/^(\*{2}|(\*)?(\d{1,2})):(\*(\d)|(\d{2}))/);
34
- if (!intervarSplit) {
35
- throw new Error(`interval ${interval} not suported`);
36
- }
37
- const [, , isHalfday, dailyHour, , tenMin, HourlyMin] = intervarSplit;
38
- const intervalMs = (isHalfday && intervalStringMs.isHalfday)
39
- || (dailyHour && intervalStringMs.dailyHour)
40
- || (tenMin && intervalStringMs.tenMin)
41
- || intervalStringMs.everyHour;
42
- const offsetDay = ((+dailyHour || 0) * 60 + (+tenMin || +HourlyMin)) * 60 * 1000;
43
- const offsetCur = (date - date.getTimezoneOffset() * 1000 * 60) % intervalMs;
44
- const waitMs = (offsetDay - offsetCur + intervalMs) % intervalMs;
45
- return [waitMs, intervalMs];
46
- },
47
- number: (interval) => {
48
- const date = new Date();
49
- const intervalMs = interval * 1000;
50
- const dateWithTZ = date - date.getTimezoneOffset() * 1000 * 60;
51
- const offsetCur = dateWithTZ % intervalMs;
52
- // start every cron within 1 hour
53
- const sixtyMinutesStartMs = 3600000;
54
- const waitMs = (intervalMs - offsetCur) % sixtyMinutesStartMs;
55
- return [waitMs, intervalMs];
56
- },
57
- };
58
-
59
- async function runCron({
60
- pg, funcs, func, name, rclient, log,
61
- }) {
62
- const unique = await verifyUnique(name, funcs.config, rclient);
63
-
64
- if (!unique) return;
65
- const db = pg.options.database;
66
- log.debug(`cron.${name}`, 1, db);
67
- try {
68
- const data = await func({ pg, funcs, log });
69
- log.debug('cron', { db, name, result: data });
70
- log.info('cron', { db, name, result: data });
71
- }
72
- catch (err) {
73
- log.debug('cron', { db, name, error: err.toString() });
74
- log.error('cron', { db, name, error: err.toString() });
75
- }
76
- }
77
-
78
- /**
79
- * interval:
80
- * - 02:54 - every day
81
- * - 2:03 - every day
82
- * - *1:43 - 2 times a day
83
- * - *12:03 - 2 times a day
84
- * - **:54 - every hour
85
- * - **:*3 - every 10 minutes
86
- * - 60 - every minute
87
- * - 10 * 60 - every 10 minutes
88
- */
89
-
90
- export default async function addCron(func, interval, fastify) {
91
- if (!fastify) {
92
- throw new Error('not enough params: fastify');
93
- }
94
-
95
- const { config = {}, log } = fastify;
96
- const { time = {}, disabled = [] } = config.cron || {};
97
- const pg = getPG();
98
- const rclient = getRedis();
99
-
100
- const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
101
-
102
- // if (!config.isServer) return;
103
-
104
- if (disabled.includes(name)) {
105
- log.debug('cron', { name, message: 'cron disabled' });
106
- return;
107
- }
108
-
109
- cronList[name] = func;
110
-
111
- const userInterval = time[name] || interval;
112
- const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
113
-
114
- if (intervalMs < 1000) {
115
- log.warn('cron', { name, error: `interval ${interval} to small` });
116
- return;
117
- }
118
-
119
- // setTimeout to w8 for the time to start
120
- setTimeout(() => {
121
- runCron({
122
- pg, funcs: fastify, func, name, rclient, log,
123
- });
124
- // interval
125
- setInterval(() => {
126
- runCron({
127
- pg, funcs: fastify, func, name, rclient, log,
128
- });
129
- }, intervalMs);
130
- }, waitMs);
131
- }
1
+ import { createHash } from 'crypto';
2
+
3
+ import cronList from '../controllers/utils/cronList.js';
4
+ import getRedis from '../../redis/funcs/getRedis.js';
5
+ import getPG from '../../pg/funcs/getPG.js';
6
+
7
+ const md5 = (string) => createHash('md5').update(string).digest('hex');
8
+
9
+ async function verifyUnique(name, config, rclient) {
10
+ const cronId = config.port || 3000 + md5(name);
11
+ // one per node check
12
+ const key = `cron:unique:${cronId}`;
13
+ const unique = await rclient.setnx(key, 1);
14
+ const ttl = await rclient.ttl(key);
15
+ if (!unique && ttl !== -1) {
16
+ return false;
17
+ }
18
+ await rclient.expire(key, 20);
19
+ return true;
20
+ }
21
+
22
+ const intervalStringMs = {
23
+ everyMin: 1000 * 60,
24
+ tenMin: 1000 * 60 * 10,
25
+ everyHour: 1000 * 60 * 60,
26
+ isHalfday: 1000 * 60 * 60 * 12,
27
+ dailyHour: 1000 * 60 * 60 * 24,
28
+ };
29
+
30
+ const interval2ms = {
31
+ string: (interval) => {
32
+ const date = new Date();
33
+ const intervarSplit = interval.match(/^(\*{2}|(\*)?(\d{1,2})):(\*(\d)|(\d{2}))/);
34
+ if (!intervarSplit) {
35
+ throw new Error(`interval ${interval} not suported`);
36
+ }
37
+ const [, , isHalfday, dailyHour, , tenMin, HourlyMin] = intervarSplit;
38
+ const intervalMs = (isHalfday && intervalStringMs.isHalfday)
39
+ || (dailyHour && intervalStringMs.dailyHour)
40
+ || (tenMin && intervalStringMs.tenMin)
41
+ || intervalStringMs.everyHour;
42
+ const offsetDay = ((+dailyHour || 0) * 60 + (+tenMin || +HourlyMin)) * 60 * 1000;
43
+ const offsetCur = (date - date.getTimezoneOffset() * 1000 * 60) % intervalMs;
44
+ const waitMs = (offsetDay - offsetCur + intervalMs) % intervalMs;
45
+ return [waitMs, intervalMs];
46
+ },
47
+ number: (interval) => {
48
+ const date = new Date();
49
+ const intervalMs = interval * 1000;
50
+ const dateWithTZ = date - date.getTimezoneOffset() * 1000 * 60;
51
+ const offsetCur = dateWithTZ % intervalMs;
52
+ // start every cron within 1 hour
53
+ const sixtyMinutesStartMs = 3600000;
54
+ const waitMs = (intervalMs - offsetCur) % sixtyMinutesStartMs;
55
+ return [waitMs, intervalMs];
56
+ },
57
+ };
58
+
59
+ async function runCron({
60
+ pg, funcs, func, name, rclient, log,
61
+ }) {
62
+ const unique = await verifyUnique(name, funcs.config, rclient);
63
+
64
+ if (!unique) return;
65
+ const db = pg.options.database;
66
+ log.debug(`cron.${name}`, 1, db);
67
+ try {
68
+ const data = await func({ pg, funcs, log });
69
+ log.debug('cron', { db, name, result: data });
70
+ log.info('cron', { db, name, result: data });
71
+ }
72
+ catch (err) {
73
+ log.debug('cron', { db, name, error: err.toString() });
74
+ log.error('cron', { db, name, error: err.toString() });
75
+ }
76
+ }
77
+
78
+ /**
79
+ * interval:
80
+ * - 02:54 - every day
81
+ * - 2:03 - every day
82
+ * - *1:43 - 2 times a day
83
+ * - *12:03 - 2 times a day
84
+ * - **:54 - every hour
85
+ * - **:*3 - every 10 minutes
86
+ * - 60 - every minute
87
+ * - 10 * 60 - every 10 minutes
88
+ */
89
+
90
+ export default async function addCron(func, interval, fastify) {
91
+ if (!fastify) {
92
+ throw new Error('not enough params: fastify');
93
+ }
94
+
95
+ const { config = {}, log } = fastify;
96
+ const { time = {}, disabled = [] } = config.cron || {};
97
+ const pg = getPG();
98
+ const rclient = getRedis();
99
+
100
+ const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
101
+
102
+ // if (!config.isServer) return;
103
+
104
+ if (disabled.includes(name)) {
105
+ log.debug('cron', { name, message: 'cron disabled' });
106
+ return;
107
+ }
108
+
109
+ cronList[name] = func;
110
+
111
+ const userInterval = time[name] || interval;
112
+ const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
113
+
114
+ if (intervalMs < 1000) {
115
+ log.warn('cron', { name, error: `interval ${interval} to small` });
116
+ return;
117
+ }
118
+
119
+ // setTimeout to w8 for the time to start
120
+ setTimeout(() => {
121
+ runCron({
122
+ pg, funcs: fastify, func, name, rclient, log,
123
+ });
124
+ // interval
125
+ setInterval(() => {
126
+ runCron({
127
+ pg, funcs: fastify, func, name, rclient, log,
128
+ });
129
+ }, intervalMs);
130
+ }, waitMs);
131
+ }
package/cron/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import cronApi from './controllers/cronApi.js';
2
- import addCron from './funcs/addCron.js';
3
-
4
- async function plugin(fastify, config = {}) {
5
- const prefix = config.prefix || '/api';
6
- fastify.decorate('addCron', addCron);
7
- fastify.get(`${prefix}/cron/:name`, {}, cronApi);
8
- }
9
-
10
- export default plugin;
1
+ import cronApi from './controllers/cronApi.js';
2
+ import addCron from './funcs/addCron.js';
3
+
4
+ async function plugin(fastify, config = {}) {
5
+ const prefix = config.prefix || '/api';
6
+ fastify.decorate('addCron', addCron);
7
+ fastify.get(`${prefix}/cron/:name`, {}, cronApi);
8
+ }
9
+
10
+ export default plugin;
@@ -3,7 +3,7 @@ import getTemplate from '../../table/controllers/utils/getTemplate.js';
3
3
  import getAccess from '../funcs/getAccess.js';
4
4
 
5
5
  export default async function deleteCrud(req) {
6
- const { actions = [], scope = 'my', my } = await getAccess(req, req.params.table, req.params.id) || {};
6
+ const { actions = [], scope, my } = await getAccess(req, req.params.table, req.params.id) || {};
7
7
  if (!actions.includes('del') || (scope === 'my' && !my)) {
8
8
  return { message: 'access restricted', status: 403 };
9
9
  }
@@ -1,45 +1,45 @@
1
- /* import sqlInjection from '../../../policy/funcs/sqlInjection.js'; */
2
- import xssInjection from './xssInjection.js';
3
-
4
- /* const checkList = xssInjection.concat(sqlInjection); */
5
-
6
- // RTE - rich text editor
7
-
8
- function checkXSS({ body, schema = {} }) {
9
- const data = typeof body === 'string' ? body : JSON.stringify(body);
10
- const stopWords = xssInjection.filter((el) => data.toLowerCase().includes(el));
11
-
12
- // check sql injection
13
- const stopSpecialSymbols = data.match(/\p{S}OR\p{S}|\p{P}OR\p{P}| OR |\+OR\+/gi);
14
- if (stopSpecialSymbols?.length) stopSpecialSymbols?.forEach((el) => stopWords.push(el));
15
-
16
- // escape arrows on non-RTE
17
- Object.keys(body)
18
- .filter((key) => ['<', '>'].find((el) => body[key]?.includes?.(el))
19
- && !['Summernote', 'Tiny', 'Ace'].includes(schema[key]?.type))
20
- ?.forEach((key) => {
21
- Object.assign(body, { [key]: body[key].replace(/</g, '&lt;').replace(/>/g, '&gt;') });
22
- });
23
- // try { } catch (err) { return { error: err.toString() }; }
24
-
25
- if (!stopWords.length) return { body };
26
-
27
- const disabledCheckFields = Object.keys(schema)?.filter((el) => schema[el]?.xssCheck === false); // exclude specific columns
28
-
29
- // check RTE
30
- /* const richTextFields = Object.keys(schema).filter((el) => ['Summernote', 'Tiny', 'Ace'].includes(schema[el]?.type));
31
- richTextFields.filter((key) => !checkList.find((el) => body[key].includes(el)))?.forEach((key) => {
32
- disabledCheckFields.push(key);
33
- }); */
34
-
35
- const field = Object.keys(body)
36
- ?.find((key) => body[key]
37
- && !disabledCheckFields.includes(key)
38
- && body[key].toLowerCase().includes(stopWords[0]));
39
- if (field) {
40
- return { error: `rule: ${stopWords[0]} | attr: ${field} | val: ${body[field]}`, body };
41
- }
42
- return { body };
43
- }
44
-
45
- export default checkXSS;
1
+ /* import sqlInjection from '../../../policy/funcs/sqlInjection.js'; */
2
+ import xssInjection from './xssInjection.js';
3
+
4
+ /* const checkList = xssInjection.concat(sqlInjection); */
5
+
6
+ // RTE - rich text editor
7
+
8
+ function checkXSS({ body, schema = {} }) {
9
+ const data = typeof body === 'string' ? body : JSON.stringify(body);
10
+ const stopWords = xssInjection.filter((el) => data.toLowerCase().includes(el));
11
+
12
+ // check sql injection
13
+ const stopSpecialSymbols = data.match(/\p{S}OR\p{S}|\p{P}OR\p{P}| OR |\+OR\+/gi);
14
+ if (stopSpecialSymbols?.length) stopSpecialSymbols?.forEach((el) => stopWords.push(el));
15
+
16
+ // escape arrows on non-RTE
17
+ Object.keys(body)
18
+ .filter((key) => ['<', '>'].find((el) => body[key]?.includes?.(el))
19
+ && !['Summernote', 'Tiny', 'Ace'].includes(schema[key]?.type))
20
+ ?.forEach((key) => {
21
+ Object.assign(body, { [key]: body[key].replace(/</g, '&lt;').replace(/>/g, '&gt;') });
22
+ });
23
+ // try { } catch (err) { return { error: err.toString() }; }
24
+
25
+ if (!stopWords.length) return { body };
26
+
27
+ const disabledCheckFields = Object.keys(schema)?.filter((el) => schema[el]?.xssCheck === false); // exclude specific columns
28
+
29
+ // check RTE
30
+ /* const richTextFields = Object.keys(schema).filter((el) => ['Summernote', 'Tiny', 'Ace'].includes(schema[el]?.type));
31
+ richTextFields.filter((key) => !checkList.find((el) => body[key].includes(el)))?.forEach((key) => {
32
+ disabledCheckFields.push(key);
33
+ }); */
34
+
35
+ const field = Object.keys(body)
36
+ ?.find((key) => body[key]
37
+ && !disabledCheckFields.includes(key)
38
+ && body[key].toLowerCase().includes(stopWords[0]));
39
+ if (field) {
40
+ return { error: `rule: ${stopWords[0]} | attr: ${field} | val: ${body[field]}`, body };
41
+ }
42
+ return { body };
43
+ }
44
+
45
+ export default checkXSS;
@@ -1,72 +1,72 @@
1
- const xssInjection = [
2
- 'onkeypress=',
3
- 'onkeyup=',
4
- 'ondblclick=',
5
- 'onerror=',
6
- 'onmouseover=',
7
- '<meta',
8
- '<script',
9
- 'vascript:',
10
- 'onkeydown=',
11
- 'onmousedown=',
12
- 'onmouseenter=',
13
- 'onmouseleave=',
14
- 'onmousemove=',
15
- 'onmouseout=',
16
- 'onmouseup=',
17
- 'onmousewheel=',
18
- 'onpaste=',
19
- 'onscroll=',
20
- 'onwheel=',
21
- 'javascript:',
22
- '\\x',
23
- 'eval(',
24
- 'onmouseover=',
25
- 'action=',
26
- 'xlink:',
27
- 'allowscriptaccess',
28
- 'href=',
29
- 'behavior:',
30
- 'onreadystatechange=',
31
- 'onstart=',
32
- 'offline=',
33
- 'onabort=',
34
- 'onafterprint=',
35
- 'onbeforeonload=',
36
- 'onbeforeprint=',
37
- 'onblur=',
38
- 'oncanplay=',
39
- 'oncanplaythrough=',
40
- 'onchange=',
41
- 'onclick=',
42
- 'oncontextmenu=',
43
- 'ondblclick=',
44
- 'ondrag=',
45
- 'ondragend=',
46
- 'ondragenter=',
47
- 'ondragleave=',
48
- 'ondragover=',
49
- 'ondragstart=',
50
- 'ondrop=',
51
- 'ondurationchange=',
52
- 'onemptied=',
53
- 'onended=',
54
- 'onerror=',
55
- 'onfocus=',
56
- 'onformchange=',
57
- 'onforminput=',
58
- 'onhaschange=',
59
- 'oninput=',
60
- 'oninvalid=',
61
- 'onkeydown=',
62
- 'onkeypress=',
63
- 'onkeyup=',
64
- 'onload=',
65
- 'onloadeddata=',
66
- 'onloadedmetadata=',
67
- 'onloadstart=',
68
- 'alert(',
69
- 'script:',
70
- ];
71
-
72
- export default xssInjection;
1
+ const xssInjection = [
2
+ 'onkeypress=',
3
+ 'onkeyup=',
4
+ 'ondblclick=',
5
+ 'onerror=',
6
+ 'onmouseover=',
7
+ '<meta',
8
+ '<script',
9
+ 'vascript:',
10
+ 'onkeydown=',
11
+ 'onmousedown=',
12
+ 'onmouseenter=',
13
+ 'onmouseleave=',
14
+ 'onmousemove=',
15
+ 'onmouseout=',
16
+ 'onmouseup=',
17
+ 'onmousewheel=',
18
+ 'onpaste=',
19
+ 'onscroll=',
20
+ 'onwheel=',
21
+ 'javascript:',
22
+ '\\x',
23
+ 'eval(',
24
+ 'onmouseover=',
25
+ 'action=',
26
+ 'xlink:',
27
+ 'allowscriptaccess',
28
+ 'href=',
29
+ 'behavior:',
30
+ 'onreadystatechange=',
31
+ 'onstart=',
32
+ 'offline=',
33
+ 'onabort=',
34
+ 'onafterprint=',
35
+ 'onbeforeonload=',
36
+ 'onbeforeprint=',
37
+ 'onblur=',
38
+ 'oncanplay=',
39
+ 'oncanplaythrough=',
40
+ 'onchange=',
41
+ 'onclick=',
42
+ 'oncontextmenu=',
43
+ 'ondblclick=',
44
+ 'ondrag=',
45
+ 'ondragend=',
46
+ 'ondragenter=',
47
+ 'ondragleave=',
48
+ 'ondragover=',
49
+ 'ondragstart=',
50
+ 'ondrop=',
51
+ 'ondurationchange=',
52
+ 'onemptied=',
53
+ 'onended=',
54
+ 'onerror=',
55
+ 'onfocus=',
56
+ 'onformchange=',
57
+ 'onforminput=',
58
+ 'onhaschange=',
59
+ 'oninput=',
60
+ 'oninvalid=',
61
+ 'onkeydown=',
62
+ 'onkeypress=',
63
+ 'onkeyup=',
64
+ 'onload=',
65
+ 'onloadeddata=',
66
+ 'onloadedmetadata=',
67
+ 'onloadstart=',
68
+ 'alert(',
69
+ 'script:',
70
+ ];
71
+
72
+ export default xssInjection;
@@ -1,15 +1,15 @@
1
- import getPG from '../../pg/funcs/getPG.js';
2
-
3
- import getMeta from '../../pg/funcs/getMeta.js';
4
-
5
- export default async function dataDelete({
6
- table, id, pg: pg1,
7
- }) {
8
- const pg = pg1 || getPG({ name: 'client' });
9
- const { pk } = await getMeta(table);
10
- if (!pg.tlist?.includes(table)) return 'table not exist';
11
- const delQuery = `delete from ${table} WHERE ${pk} = $1 returning *`;
12
- // console.log(updateDataset);
13
- const res = await pg.one(delQuery, [id]) || {};
14
- return res;
15
- }
1
+ import getPG from '../../pg/funcs/getPG.js';
2
+
3
+ import getMeta from '../../pg/funcs/getMeta.js';
4
+
5
+ export default async function dataDelete({
6
+ table, id, pg: pg1,
7
+ }) {
8
+ const pg = pg1 || getPG({ name: 'client' });
9
+ const { pk } = await getMeta(table);
10
+ if (!pg.tlist?.includes(table)) return 'table not exist';
11
+ const delQuery = `delete from ${table} WHERE ${pk} = $1 returning *`;
12
+ // console.log(updateDataset);
13
+ const res = await pg.one(delQuery, [id]) || {};
14
+ return res;
15
+ }
@@ -1,24 +1,24 @@
1
- import getPG from '../../pg/funcs/getPG.js';
2
-
3
- import getMeta from '../../pg/funcs/getMeta.js';
4
-
5
- export default async function dataUpdate({
6
- table, id, data, pg: pg1,
7
- }) {
8
- if (!data || !table || !id) return null;
9
-
10
- const pg = pg1 || getPG({ name: 'client' });
11
- const { columns, pk } = await getMeta(table);
12
-
13
- const names = columns?.map((el) => el.name);
14
- const filterData = Object.keys(data)
15
- .filter((el) => (typeof data[el] === 'boolean' ? true : data[el] && names?.includes(el)));
16
-
17
- const filterValue = filterData.map((el) => [el, data[el]]).map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]));
18
-
19
- const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => (key === 'geom' ? `"${key}"=st_setsrid(st_geomfromgeojson($${i + 2}::json),4326)` : `"${key}"=$${i + 2}`)).join(',')}
20
- WHERE ${pk} = $1 returning *`;
21
- // console.log(updateDataset);
22
- const res = await pg.query(updateQuery, [id, ...filterValue]).then(el => el?.rows?.[0]) || {};
23
- return res;
24
- }
1
+ import getPG from '../../pg/funcs/getPG.js';
2
+
3
+ import getMeta from '../../pg/funcs/getMeta.js';
4
+
5
+ export default async function dataUpdate({
6
+ table, id, data, pg: pg1,
7
+ }) {
8
+ if (!data || !table || !id) return null;
9
+
10
+ const pg = pg1 || getPG({ name: 'client' });
11
+ const { columns, pk } = await getMeta(table);
12
+
13
+ const names = columns?.map((el) => el.name);
14
+ const filterData = Object.keys(data)
15
+ .filter((el) => (typeof data[el] === 'boolean' ? true : data[el] && names?.includes(el)));
16
+
17
+ const filterValue = filterData.map((el) => [el, data[el]]).map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]));
18
+
19
+ const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => (key === 'geom' ? `"${key}"=st_setsrid(st_geomfromgeojson($${i + 2}::json),4326)` : `"${key}"=$${i + 2}`)).join(',')}
20
+ WHERE ${pk} = $1 returning *`;
21
+ // console.log(updateDataset);
22
+ const res = await pg.query(updateQuery, [id, ...filterValue]).then(el => el?.rows?.[0]) || {};
23
+ return res;
24
+ }
@@ -20,7 +20,7 @@ where a.route_id=$1 and $2 in (b.user_uid, d.user_uid)`;
20
20
 
21
21
  export default async function getAccess(req, template, id = null) {
22
22
  if (config.disableAccessRestriction || true) {
23
- return { actions: ['get', 'edit', 'del'], query: '1=1' };
23
+ return { actions: ['get', 'edit', 'del'], my: true, query: '1=1' };
24
24
  }
25
25
  const { pg, session = {} } = req;
26
26
  const { uid, user_type: userType } = session.passport?.user || {};
@@ -32,6 +32,7 @@ export default async function getAccess(req, template, id = null) {
32
32
  if (!table) return null;
33
33
 
34
34
  const { scope = 'my', actions = [] } = await pg.one(q, [template, uid]);
35
+ // console.log(scope, actions);
35
36
 
36
37
  const { columns = [] } = await getMeta(table);
37
38
  const columnList = columns.map((el) => el.name || el).join(',');