@opengis/fastify-table 1.0.9 → 1.0.11

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 (66) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +47 -47
  3. package/README.md +26 -26
  4. package/config.js +11 -11
  5. package/crud/controllers/deleteCrud.js +10 -10
  6. package/crud/controllers/insert.js +28 -28
  7. package/crud/controllers/update.js +29 -29
  8. package/crud/controllers/utils/checkXSS.js +45 -45
  9. package/crud/controllers/utils/xssInjection.js +72 -72
  10. package/crud/funcs/dataDelete.js +15 -15
  11. package/crud/funcs/dataInsert.js +24 -24
  12. package/crud/funcs/dataUpdate.js +3 -3
  13. package/crud/funcs/{getIdByToken.js → getToken.js} +27 -29
  14. package/crud/funcs/isFileExists.js +13 -13
  15. package/crud/funcs/{setTokenById.js → setToken.js} +53 -55
  16. package/helper.js +28 -28
  17. package/index.js +32 -32
  18. package/package.json +22 -22
  19. package/pg/funcs/autoIndex.js +89 -89
  20. package/pg/funcs/getMeta.js +27 -27
  21. package/pg/funcs/getPG.js +3 -0
  22. package/pg/funcs/init.js +42 -42
  23. package/pg/funcs/pgClients.js +2 -2
  24. package/pg/index.js +35 -35
  25. package/pg/pgClients.js +20 -17
  26. package/policy/funcs/checkPolicy.js +74 -74
  27. package/policy/funcs/sqlInjection.js +33 -33
  28. package/policy/index.js +14 -14
  29. package/redis/client.js +8 -8
  30. package/redis/funcs/getRedis.js +1 -2
  31. package/redis/funcs/redisClients.js +2 -2
  32. package/redis/index.js +19 -19
  33. package/server/templates/form/test.dataset.form.json +411 -411
  34. package/server.js +14 -14
  35. package/table/controllers/data.js +57 -55
  36. package/table/controllers/filter.js +32 -24
  37. package/table/controllers/form.js +10 -10
  38. package/table/controllers/suggest.js +60 -60
  39. package/table/controllers/utils/getSelect.js +20 -20
  40. package/table/controllers/utils/getSelectMeta.js +66 -66
  41. package/table/funcs/getFilterSQL/index.js +75 -75
  42. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  43. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  44. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  45. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  46. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  47. package/table/funcs/metaFormat/getSelectVal.js +20 -0
  48. package/table/funcs/metaFormat/index.js +24 -0
  49. package/table/index.js +25 -14
  50. package/test/api/crud.test.js +50 -50
  51. package/test/api/crud.xss.test.js +68 -70
  52. package/test/api/table.test.js +49 -49
  53. package/test/config.example +18 -18
  54. package/test/funcs/crud.test.js +76 -77
  55. package/test/funcs/pg.test.js +34 -32
  56. package/test/funcs/redis.test.js +19 -19
  57. package/test/templates/cls/itree.recommend.json +26 -0
  58. package/test/templates/cls/itree.type_plant.json +65 -0
  59. package/test/templates/cls/test.json +9 -9
  60. package/test/templates/form/cp_building.form.json +32 -32
  61. package/test/templates/select/account_id.json +3 -3
  62. package/test/templates/select/contact_id.sql +1 -0
  63. package/test/templates/select/storage.data.json +2 -2
  64. package/test/templates/table/gis.dataset.table.json +20 -20
  65. package/test/templates/table/green_space.table.json +3 -3
  66. package/test/funcs/table.test.js +0 -48
@@ -1,34 +1,34 @@
1
- function getTable(table) {
2
- const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
3
- ?.map((el) => el.split(/[ )]/)[0]);
4
- return result;
5
- }
6
-
7
- /**
8
- * @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
9
- * @param {String} opt.query - запит до таблиці
10
- * @param {String} opt.hash - інформація з хешу по запиту
11
- */
12
- const tableSql = {};
13
- async function getTableSql({
14
- pg, body, table, fields,
15
- }) {
16
- if (tableSql[table]) return tableSql[table];
17
-
18
- const fieldList = fields.map((el) => el.name);
19
-
20
- const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
21
-
22
- if (!tableList) { tableSql[table] = []; return []; }
23
-
24
- const data = await Promise.all(tableList?.map(async (tableEl) => {
25
- const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`, { cache: 1 });
26
- return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
27
- }));
28
-
29
- tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
30
-
31
- return tableSql[table];
32
- }
33
-
34
- export default getTableSql;
1
+ function getTable(table) {
2
+ const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
3
+ ?.map((el) => el.split(/[ )]/)[0]);
4
+ return result;
5
+ }
6
+
7
+ /**
8
+ * @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
9
+ * @param {String} opt.query - запит до таблиці
10
+ * @param {String} opt.hash - інформація з хешу по запиту
11
+ */
12
+ const tableSql = {};
13
+ async function getTableSql({
14
+ pg, body, table, fields,
15
+ }) {
16
+ if (tableSql[table]) return tableSql[table];
17
+
18
+ const fieldList = fields.map((el) => el.name);
19
+
20
+ const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
21
+
22
+ if (!tableList) { tableSql[table] = []; return []; }
23
+
24
+ const data = await Promise.all(tableList?.map(async (tableEl) => {
25
+ const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`, { cache: 1 });
26
+ return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
27
+ }));
28
+
29
+ tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
30
+
31
+ return tableSql[table];
32
+ }
33
+
34
+ export default getTableSql;
@@ -0,0 +1,20 @@
1
+ import getSelect from '../../controllers/utils/getSelect.js';
2
+ import pg from '../../../pg/pgClients.js';
3
+ import redis from '../../../redis/client.js';
4
+
5
+ export default async function metaFormat({ name, values }) {
6
+ const cls = await getSelect(name);
7
+ if (!cls?.arr && !cls?.sql) return null;
8
+ const key = `select:${name}`;
9
+ const cache = !cls.arr ? (await redis.hmget(key, values)).reduce((p, el, i) => ({ ...p, [values[i]]: el }), {}) : {};
10
+
11
+ const data = cls.arr || (values.filter(el => !cache[el]).length
12
+ ? await pg.client.query(`with c(id,text) as (${cls.sql}) select * from c where id = any('{${values.filter(el => !cache[el])}}')`).then(el => el.rows)
13
+ : []);
14
+
15
+ const clsAr = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.text }), {}) };
16
+ if (!cls.arr && data.length) {
17
+ redis.hmset(key, clsAr);
18
+ }
19
+ return clsAr;
20
+ }
@@ -0,0 +1,24 @@
1
+ import getTemplate from '../../controllers/utils/getTemplate.js';
2
+ import getSelectVal from './getSelectVal.js';
3
+
4
+ export default async function metaFormat({ rows, table }) {
5
+ const loadTable = await getTemplate('table', table);
6
+ const selectCols = loadTable.columns?.filter((e) => e.data);
7
+
8
+ // cls & select format
9
+
10
+ await Promise.all(selectCols?.map(async (attr) => {
11
+ const val = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
12
+ if (!val.length) return null;
13
+
14
+ const clsAr = await getSelectVal({ name: attr.data, values: val });
15
+ if (!clsAr) return null;
16
+ rows.forEach(el => {
17
+ Object.assign(el, { [`${attr.name}_text`]: clsAr[el[attr.name]] || el[attr.name] });
18
+ });
19
+
20
+ return null;
21
+ }));
22
+
23
+ return rows;
24
+ }
package/table/index.js CHANGED
@@ -1,14 +1,25 @@
1
- import suggest from './controllers/suggest.js';
2
- import data from './controllers/data.js';
3
- import filter from './controllers/filter.js';
4
- import form from './controllers/form.js';
5
-
6
- async function plugin(fastify, config = {}) {
7
- const prefix = config.prefix || '/api';
8
- fastify.get(`${prefix}/suggest/:data`, {}, suggest);
9
- fastify.get(`${prefix}/data/:table/:id?`, {}, data); // vs.crm.data.api с node
10
- fastify.get(`${prefix}/filter/:table`, {}, filter);
11
- fastify.get(`${prefix}/form/:form`, {}, form);
12
- }
13
-
14
- export default plugin;
1
+ import suggest from './controllers/suggest.js';
2
+ import data from './controllers/data.js';
3
+ import filter from './controllers/filter.js';
4
+ import form from './controllers/form.js';
5
+ import metaFormat from './funcs/metaFormat/index.js';
6
+
7
+ const tableSchema = {
8
+ querystring: {
9
+ page: { type: 'number' },
10
+ order: { type: 'string' },
11
+ filter: { type: 'string' },
12
+ },
13
+ };
14
+
15
+ async function plugin(fastify, config = {}) {
16
+ const prefix = config.prefix || '/api';
17
+ fastify.decorate('metaFormat', metaFormat);
18
+
19
+ fastify.get(`${prefix}/suggest/:data`, {}, suggest);
20
+ fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
21
+ fastify.get(`${prefix}/filter/:table`, {}, filter);
22
+ fastify.get(`${prefix}/form/:form`, {}, form);
23
+ }
24
+
25
+ export default plugin;
@@ -1,50 +1,50 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../../helper.js';
5
-
6
- import config from '../config.js';
7
-
8
- test('api crud', async (t) => {
9
- const app = await build(t);
10
- const prefix = config.prefix || '/api';
11
-
12
- await t.test('POST /insert', async () => {
13
- const res = await app.inject({
14
- method: 'POST',
15
- url: `${prefix}/crud/gis.dataset`,
16
- body: { dataset_name: '111', dataset_id: '5400000' },
17
- });
18
-
19
- const rep = JSON.parse(res?.body);
20
- // rep.dataset_id
21
- // console.log(rep);
22
- // pgClients.client.query('delete from gis.dataset where dataset_id=$1', [rep.dataset_id]);
23
- assert.ok(rep.dataset_id);
24
- });
25
-
26
- await t.test('PUT /update', async () => {
27
- const res = await app.inject({
28
- method: 'PUT',
29
- url: `${prefix}/crud/gis.dataset/5400000`,
30
- body: { editor_id: '11' },
31
- });
32
-
33
- const rep = JSON.parse(res?.body);
34
- // rep.dataset_id
35
- // console.log(rep);
36
- assert.equal(rep.editor_id, '11');
37
- });
38
-
39
- await t.test('DELETE /delete', async () => {
40
- const res = await app.inject({
41
- method: 'DELETE',
42
- url: `${prefix}/crud/gis.dataset/5400000`,
43
- });
44
-
45
- const rep = JSON.parse(res?.body);
46
- // rep.dataset_id
47
- // console.log(rep);
48
- assert.ok(rep);
49
- });
50
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+
6
+ import config from '../config.js';
7
+
8
+ test('api crud', async (t) => {
9
+ const app = await build(t);
10
+ const prefix = config.prefix || '/api';
11
+
12
+ await t.test('POST /insert', async () => {
13
+ const res = await app.inject({
14
+ method: 'POST',
15
+ url: `${prefix}/crud/gis.dataset`,
16
+ body: { dataset_name: '111', dataset_id: '5400000' },
17
+ });
18
+
19
+ const rep = JSON.parse(res?.body);
20
+ // rep.dataset_id
21
+ // console.log(rep);
22
+ // pgClients.client.query('delete from gis.dataset where dataset_id=$1', [rep.dataset_id]);
23
+ assert.ok(rep.dataset_id);
24
+ });
25
+
26
+ await t.test('PUT /update', async () => {
27
+ const res = await app.inject({
28
+ method: 'PUT',
29
+ url: `${prefix}/crud/gis.dataset/5400000`,
30
+ body: { editor_id: '11' },
31
+ });
32
+
33
+ const rep = JSON.parse(res?.body);
34
+ // rep.dataset_id
35
+ // console.log(rep);
36
+ assert.equal(rep.editor_id, '11');
37
+ });
38
+
39
+ await t.test('DELETE /delete', async () => {
40
+ const res = await app.inject({
41
+ method: 'DELETE',
42
+ url: `${prefix}/crud/gis.dataset/5400000`,
43
+ });
44
+
45
+ const rep = JSON.parse(res?.body);
46
+ // rep.dataset_id
47
+ // console.log(rep);
48
+ assert.ok(rep);
49
+ });
50
+ });
@@ -1,70 +1,68 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../../helper.js';
5
-
6
- import setTokenById from '../../crud/funcs/setTokenById.js';
7
- import config from '../config.js';
8
-
9
- test('api crud xss', async (t) => {
10
- const app = await build(t);
11
- const session = { passport: { user: { uid: '1' } } };
12
- app.decorateRequest('session', session);
13
-
14
- const prefix = config.prefix || '/api';
15
-
16
- let addTokens;
17
- let editTokens;
18
-
19
- // before
20
- t.test('setTokenById', async () => {
21
- addTokens = setTokenById({
22
- funcs: { config },
23
- ids: [JSON.stringify({ add: 'gis.dataset', form: 'test.dataset.form' })],
24
- mode: 'a',
25
- session,
26
- array: 1,
27
- });
28
- editTokens = setTokenById({
29
- funcs: { config },
30
- ids: [JSON.stringify({ id: '5400000', table: 'gis.dataset', form: 'test.dataset.form' })],
31
- mode: 'w',
32
- session,
33
- array: 1,
34
- });
35
- assert.ok(addTokens.length === 1 && editTokens.length === 1, 'invalid token');
36
- });
37
-
38
- await t.test('POST /insert', async () => {
39
- const res = await app.inject({
40
- method: 'POST',
41
- url: `${prefix}/crud/${addTokens[0]}`,
42
- body: { dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>', dataset_id: '5400000' },
43
- });
44
-
45
- const rep = JSON.parse(res?.body);
46
-
47
- assert.ok(rep.status, 409);
48
- });
49
-
50
- await t.test('PUT /update', async () => {
51
- const res = await app.inject({
52
- method: 'PUT',
53
- url: `${prefix}/crud/${editTokens[0]}/${editTokens[0]}`,
54
- body: { editor_id: '11', dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>' },
55
- });
56
-
57
- const rep = JSON.parse(res?.body);
58
-
59
- assert.equal(rep.status, 409);
60
- });
61
- await t.test('DELETE /delete', async () => {
62
- const res = await app.inject({
63
- method: 'DELETE',
64
- url: `${prefix}/crud/gis.dataset/5400000`,
65
- });
66
-
67
- const rep = JSON.parse(res?.body);
68
- assert.ok(rep);
69
- });
70
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+
6
+ import setToken from '../../crud/funcs/setToken.js';
7
+ import config from '../config.js';
8
+
9
+ test('api crud xss', async (t) => {
10
+ const app = await build(t);
11
+ const session = { passport: { user: { uid: '1' } } };
12
+ app.decorateRequest('session', session);
13
+
14
+ const prefix = config.prefix || '/api';
15
+
16
+ let addTokens;
17
+ let editTokens;
18
+
19
+ // before
20
+ t.test('setToken', async () => {
21
+ addTokens = setToken({
22
+ ids: [JSON.stringify({ add: 'gis.dataset', form: 'test.dataset.form' })],
23
+ mode: 'a',
24
+ uid: 1,
25
+ array: 1,
26
+ });
27
+ editTokens = setToken({
28
+ ids: [JSON.stringify({ id: '5400000', table: 'gis.dataset', form: 'test.dataset.form' })],
29
+ mode: 'w',
30
+ uid: 1,
31
+ array: 1,
32
+ });
33
+ assert.ok(addTokens.length === 1 && editTokens.length === 1, 'invalid token');
34
+ });
35
+
36
+ await t.test('POST /insert', async () => {
37
+ const res = await app.inject({
38
+ method: 'POST',
39
+ url: `${prefix}/crud/${addTokens[0]}`,
40
+ body: { dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>', dataset_id: '5400000' },
41
+ });
42
+
43
+ const rep = JSON.parse(res?.body);
44
+
45
+ assert.ok(rep.status, 409);
46
+ });
47
+
48
+ await t.test('PUT /update', async () => {
49
+ const res = await app.inject({
50
+ method: 'PUT',
51
+ url: `${prefix}/crud/${editTokens[0]}/${editTokens[0]}`,
52
+ body: { editor_id: '11', dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>' },
53
+ });
54
+
55
+ const rep = JSON.parse(res?.body);
56
+
57
+ assert.equal(rep.status, 409);
58
+ });
59
+ await t.test('DELETE /delete', async () => {
60
+ const res = await app.inject({
61
+ method: 'DELETE',
62
+ url: `${prefix}/crud/gis.dataset/5400000`,
63
+ });
64
+
65
+ const rep = JSON.parse(res?.body);
66
+ assert.ok(rep);
67
+ });
68
+ });
@@ -1,49 +1,49 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../../helper.js';
5
-
6
- test('api table', async (t) => {
7
- const app = await build(t);
8
- // assert.ok(1);
9
- await t.test('GET /suggest', async () => {
10
- const res = await app.inject({
11
- method: 'GET',
12
- url: '/api/suggest/storage.data',
13
- });
14
- // console.log(res?.body);
15
- const rep = JSON.parse(res?.body);
16
- // console.log(rep.total);
17
- assert.ok(rep.total);
18
- });
19
- await t.test('GET /data', async () => {
20
- const res = await app.inject({
21
- method: 'GET',
22
- url: '/api/data/gis.dataset.table',
23
- });
24
- // console.log(res);
25
- const rep = JSON.parse(res?.body);
26
- // console.log(rep.total);
27
- assert.ok(rep.total);
28
- });
29
- await t.test('GET /form', async () => {
30
- const res = await app.inject({
31
- method: 'GET',
32
- url: '/api/form/cp_building.form',
33
- });
34
- // console.log(res);
35
- const rep = JSON.parse(res?.body);
36
- // console.log(rep.total);
37
- assert.ok(rep);
38
- });
39
- await t.test('GET /filter', async () => {
40
- const res = await app.inject({
41
- method: 'GET',
42
- url: '/api/filter/gis.dataset.table',
43
- });
44
- // console.log(res);
45
- const rep = JSON.parse(res?.body);
46
- // console.log(rep.total);
47
- assert.ok(rep);
48
- });
49
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+
6
+ test('api table', async (t) => {
7
+ const app = await build(t);
8
+ // assert.ok(1);
9
+ await t.test('GET /suggest', async () => {
10
+ const res = await app.inject({
11
+ method: 'GET',
12
+ url: '/api/suggest/storage.data',
13
+ });
14
+ // console.log(res?.body);
15
+ const rep = JSON.parse(res?.body);
16
+ // console.log(rep.total);
17
+ assert.ok(rep.total);
18
+ });
19
+ await t.test('GET /data', async () => {
20
+ const res = await app.inject({
21
+ method: 'GET',
22
+ url: '/api/data/gis.dataset.table',
23
+ });
24
+ // console.log(res);
25
+ const rep = JSON.parse(res?.body);
26
+ // console.log(rep.total);
27
+ assert.ok(rep.total);
28
+ });
29
+ await t.test('GET /form', async () => {
30
+ const res = await app.inject({
31
+ method: 'GET',
32
+ url: '/api/form/cp_building.form',
33
+ });
34
+ // console.log(res);
35
+ const rep = JSON.parse(res?.body);
36
+ // console.log(rep.total);
37
+ assert.ok(rep);
38
+ });
39
+ await t.test('GET /filter', async () => {
40
+ const res = await app.inject({
41
+ method: 'GET',
42
+ url: '/api/filter/gis.dataset.table',
43
+ });
44
+ // console.log(res);
45
+ const rep = JSON.parse(res?.body);
46
+ // console.log(rep.total);
47
+ assert.ok(rep);
48
+ });
49
+ });
@@ -1,18 +1,18 @@
1
- import config from '../config.js';
2
-
3
- Object.assign(config, {
4
- templateDir: 'test/templates',
5
- pg: {
6
- host: '192.168.3.160',
7
- port: 5434,
8
- database: 'mbk_rivne_dma',
9
- user: 'postgres',
10
- password: 'postgres',
11
- },
12
- redis: {
13
- host: '192.168.3.160',
14
- port: 6379,
15
- family: 4,
16
- },
17
- });
18
- export default config;
1
+ import config from '../config.js';
2
+
3
+ Object.assign(config, {
4
+ templateDir: 'test/templates',
5
+ pg: {
6
+ host: '192.168.3.160',
7
+ port: 5434,
8
+ database: 'mbk_rivne_dma',
9
+ user: 'postgres',
10
+ password: 'postgres',
11
+ },
12
+ redis: {
13
+ host: '192.168.3.160',
14
+ port: 6379,
15
+ family: 4,
16
+ },
17
+ });
18
+ export default config;