@opengis/fastify-table 1.0.34 → 1.0.36

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 (77) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +105 -101
  3. package/README.md +26 -26
  4. package/config.js +12 -12
  5. package/crud/controllers/deleteCrud.js +14 -14
  6. package/crud/controllers/insert.js +29 -29
  7. package/crud/controllers/update.js +31 -31
  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 +24 -24
  13. package/crud/funcs/getOpt.js +10 -10
  14. package/crud/funcs/getToken.js +27 -27
  15. package/crud/funcs/isFileExists.js +13 -13
  16. package/crud/funcs/setOpt.js +16 -16
  17. package/crud/funcs/setToken.js +53 -53
  18. package/crud/index.js +29 -29
  19. package/helper.js +28 -28
  20. package/index.js +39 -39
  21. package/notification/controllers/userNotifications.js +19 -19
  22. package/notification/funcs/addNotification.js +8 -8
  23. package/notification/index.js +19 -19
  24. package/package.json +22 -22
  25. package/pg/funcs/getPG.js +29 -29
  26. package/pg/pgClients.js +20 -20
  27. package/policy/funcs/checkPolicy.js +74 -74
  28. package/policy/funcs/sqlInjection.js +33 -33
  29. package/policy/index.js +14 -14
  30. package/redis/client.js +8 -8
  31. package/redis/funcs/getRedis.js +23 -23
  32. package/redis/funcs/redisClients.js +2 -2
  33. package/redis/index.js +19 -19
  34. package/server/migrations/crm.sql +95 -95
  35. package/server/migrations/log.sql +41 -41
  36. package/server/templates/form/test.dataset.form.json +411 -411
  37. package/server/templates/select/test.storage.data.json +2 -2
  38. package/server/templates/table/test.dataset.table.json +24 -24
  39. package/server.js +14 -14
  40. package/table/controllers/data.js +57 -57
  41. package/table/controllers/filter.js +37 -32
  42. package/table/controllers/form.js +10 -10
  43. package/table/controllers/search.js +41 -41
  44. package/table/controllers/suggest.js +60 -60
  45. package/table/controllers/utils/getSelect.js +20 -20
  46. package/table/controllers/utils/getSelectMeta.js +66 -66
  47. package/table/controllers/utils/getTemplate.js +28 -28
  48. package/table/funcs/getFilterSQL/index.js +75 -75
  49. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  50. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  51. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  52. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  53. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  54. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  55. package/table/funcs/metaFormat/index.js +26 -26
  56. package/table/index.js +42 -42
  57. package/test/api/crud.test.js +56 -56
  58. package/test/api/crud.xss.test.js +71 -71
  59. package/test/api/notification.test.js +37 -37
  60. package/test/api/table.test.js +57 -57
  61. package/test/api/widget.test.js +114 -114
  62. package/test/config.example +18 -18
  63. package/test/funcs/crud.test.js +76 -76
  64. package/test/funcs/notification.test.js +31 -31
  65. package/test/funcs/pg.test.js +34 -34
  66. package/test/funcs/redis.test.js +19 -19
  67. package/test/templates/cls/test.json +9 -9
  68. package/test/templates/form/cp_building.form.json +32 -32
  69. package/test/templates/select/account_id.json +3 -3
  70. package/test/templates/select/storage.data.json +2 -2
  71. package/test/templates/table/gis.dataset.table.json +20 -20
  72. package/widget/controllers/utils/historyFormat.js +76 -76
  73. package/widget/controllers/utils/obj2db.js +13 -13
  74. package/widget/controllers/widget.del.js +41 -41
  75. package/widget/controllers/widget.get.js +77 -77
  76. package/widget/controllers/widget.set.js +64 -64
  77. package/widget/index.js +29 -29
@@ -1,73 +1,73 @@
1
- /* eslint-disable no-continue */
2
- /**
3
- * @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
4
- * @param {String} opt.query - запит до таблиці
5
- * @param {String} opt.hash - інформація з хешу по запиту
6
- */
7
-
8
- import formatValue from './formatValue.js';
9
-
10
- function getQuery({
11
- filter: filterStr, tableSQL, fields, filterList, config = {}, pg,
12
- }) {
13
- if (!filterStr) return null; // filter list API
14
-
15
- const mainOperators = ['=', '~', '>', '<'];
16
-
17
- // v3 filter
18
- const { allTemplates } = config;
19
-
20
- const filterQueryArray = config.v3?.filter
21
- ? decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')).replace(/'/g, '').split(/[;|]/)
22
- : decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')?.replace(/,null/g, '')).replace(/'/g, '').split(/[;|]/);
23
-
24
- const resultList = [];
25
-
26
- for (let i = 0; i < filterQueryArray.length; i += 1) {
27
- const item = filterQueryArray[i];
28
- const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
29
- const [name] = item.split(operator);
30
-
31
- // skip already added filter
32
- if (resultList.find((el) => el.name === name)) {
33
- continue;
34
- }
35
-
36
- // find all value
37
- const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
38
-
39
- const optimize = fields?.find((el) => el.name === name) ? null : tableSQL.find((el) => el.name === name);
40
-
41
- // find field and skip not exists
42
- const { dataTypeID } = fields?.find((el) => el.name === name) || fields?.find((el) => el.name === optimize?.pk) || {};
43
- if (!dataTypeID) continue;
44
-
45
- const type = pg.pgType?.[dataTypeID];
46
-
47
- // filter
48
- const filter = filterList?.find((el) => el.id === name) || { type: 'text' };
49
- const filterType = filter.type?.toLowerCase();
50
- // format query
51
-
52
- const { op, query } = formatValue({
53
- clsList: allTemplates?.cls || [],
54
- filter,
55
- optimize,
56
- filterType,
57
- name,
58
- value: decodeURIComponent(value),
59
- operator,
60
- fieldType: type || 'text',
61
- }) || {};
62
- // console.log({ query, value });
63
- if (!query) continue;
64
-
65
- resultList.push({
66
- name, value, query, operator: op, filterType, type,
67
- });
68
- }
69
-
70
- return resultList;
71
- }
72
-
73
- export default getQuery;
1
+ /* eslint-disable no-continue */
2
+ /**
3
+ * @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
4
+ * @param {String} opt.query - запит до таблиці
5
+ * @param {String} opt.hash - інформація з хешу по запиту
6
+ */
7
+
8
+ import formatValue from './formatValue.js';
9
+
10
+ function getQuery({
11
+ filter: filterStr, tableSQL, fields, filterList, config = {}, pg,
12
+ }) {
13
+ if (!filterStr) return null; // filter list API
14
+
15
+ const mainOperators = ['=', '~', '>', '<'];
16
+
17
+ // v3 filter
18
+ const { allTemplates } = config;
19
+
20
+ const filterQueryArray = config.v3?.filter
21
+ ? decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')).replace(/'/g, '').split(/[;|]/)
22
+ : decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')?.replace(/,null/g, '')).replace(/'/g, '').split(/[;|]/);
23
+
24
+ const resultList = [];
25
+
26
+ for (let i = 0; i < filterQueryArray.length; i += 1) {
27
+ const item = filterQueryArray[i];
28
+ const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
29
+ const [name] = item.split(operator);
30
+
31
+ // skip already added filter
32
+ if (resultList.find((el) => el.name === name)) {
33
+ continue;
34
+ }
35
+
36
+ // find all value
37
+ const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
38
+
39
+ const optimize = fields?.find((el) => el.name === name) ? null : tableSQL.find((el) => el.name === name);
40
+
41
+ // find field and skip not exists
42
+ const { dataTypeID } = fields?.find((el) => el.name === name) || fields?.find((el) => el.name === optimize?.pk) || {};
43
+ if (!dataTypeID) continue;
44
+
45
+ const type = pg.pgType?.[dataTypeID];
46
+
47
+ // filter
48
+ const filter = filterList?.find((el) => el.id === name) || { type: 'text' };
49
+ const filterType = filter.type?.toLowerCase();
50
+ // format query
51
+
52
+ const { op, query } = formatValue({
53
+ clsList: allTemplates?.cls || [],
54
+ filter,
55
+ optimize,
56
+ filterType,
57
+ name,
58
+ value: decodeURIComponent(value),
59
+ operator,
60
+ fieldType: type || 'text',
61
+ }) || {};
62
+ // console.log({ query, value });
63
+ if (!query) continue;
64
+
65
+ resultList.push({
66
+ name, value, query, operator: op, filterType, type,
67
+ });
68
+ }
69
+
70
+ return resultList;
71
+ }
72
+
73
+ export default getQuery;
@@ -1,12 +1,12 @@
1
- function getOptimizedQuery({ body, table, q }, count) {
2
- const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
3
-
4
- const tableName = body?.table || body?.model || table;
5
-
6
- const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
7
- .map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
8
-
9
- return `(select * from ${tableName} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
10
- }
11
-
12
- export default getOptimizedQuery;
1
+ function getOptimizedQuery({ body, table, q }, count) {
2
+ const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
3
+
4
+ const tableName = body?.table || body?.model || table;
5
+
6
+ const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
7
+ .map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
8
+
9
+ return `(select * from ${tableName} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
10
+ }
11
+
12
+ export default getOptimizedQuery;
@@ -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`);
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`);
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,20 +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.color ? el : el.text }), {}) };
16
- if (!cls.arr && data.length) {
17
- redis.hmset(key, clsAr);
18
- }
19
- return clsAr;
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.color ? el : el.text }), {}) };
16
+ if (!cls.arr && data.length) {
17
+ redis.hmset(key, clsAr);
18
+ }
19
+ return clsAr;
20
+ }
@@ -1,26 +1,26 @@
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 values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
12
- if (!values.length) return null;
13
-
14
- const cls = await getSelectVal({ name: attr.data, values });
15
- if (!cls) return null;
16
- rows.forEach(el => {
17
- const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
18
- if (!val) return;
19
- Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
20
- });
21
-
22
- return null;
23
- }));
24
-
25
- return rows;
26
- }
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 values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
12
+ if (!values.length) return null;
13
+
14
+ const cls = await getSelectVal({ name: attr.data, values });
15
+ if (!cls) return null;
16
+ rows.forEach(el => {
17
+ const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
18
+ if (!val) return;
19
+ Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
20
+ });
21
+
22
+ return null;
23
+ }));
24
+
25
+ return rows;
26
+ }
package/table/index.js CHANGED
@@ -1,42 +1,42 @@
1
- import suggest from './controllers/suggest.js';
2
- import data from './controllers/data.js';
3
- import search from './controllers/search.js';
4
- import filter from './controllers/filter.js';
5
- import form from './controllers/form.js';
6
- import metaFormat from './funcs/metaFormat/index.js';
7
- import getFilterSQL from './funcs/getFilterSQL/index.js';
8
- import getTemplate from './controllers/utils/getTemplate.js';
9
-
10
- const tableSchema = {
11
- querystring: {
12
- page: { type: 'number' },
13
- order: { type: 'string' },
14
- filter: { type: 'string' },
15
- },
16
- };
17
-
18
- const searchTableSchema = {
19
- querystring: {
20
- page: { type: 'number' },
21
- order: { type: 'string' },
22
- desc: { type: 'string' },
23
- filter: { type: 'string' },
24
- key: { type: 'string' },
25
- table: { type: 'string' },
26
- },
27
- };
28
-
29
- async function plugin(fastify, config = {}) {
30
- const prefix = config.prefix || '/api';
31
- fastify.decorate('metaFormat', metaFormat);
32
- fastify.decorate('getFilterSQL', getFilterSQL);
33
- fastify.decorate('getTemplate', getTemplate);
34
-
35
- fastify.get(`${prefix}/suggest/:data`, {}, suggest);
36
- fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
37
- fastify.get(`${prefix}/search`, { schema: searchTableSchema }, search);
38
- fastify.get(`${prefix}/filter/:table`, {}, filter);
39
- fastify.get(`${prefix}/form/:form`, {}, form);
40
- }
41
-
42
- export default plugin;
1
+ import suggest from './controllers/suggest.js';
2
+ import data from './controllers/data.js';
3
+ import search from './controllers/search.js';
4
+ import filter from './controllers/filter.js';
5
+ import form from './controllers/form.js';
6
+ import metaFormat from './funcs/metaFormat/index.js';
7
+ import getFilterSQL from './funcs/getFilterSQL/index.js';
8
+ import getTemplate from './controllers/utils/getTemplate.js';
9
+
10
+ const tableSchema = {
11
+ querystring: {
12
+ page: { type: 'number' },
13
+ order: { type: 'string' },
14
+ filter: { type: 'string' },
15
+ },
16
+ };
17
+
18
+ const searchTableSchema = {
19
+ querystring: {
20
+ page: { type: 'number' },
21
+ order: { type: 'string' },
22
+ desc: { type: 'string' },
23
+ filter: { type: 'string' },
24
+ key: { type: 'string' },
25
+ table: { type: 'string' },
26
+ },
27
+ };
28
+
29
+ async function plugin(fastify, config = {}) {
30
+ const prefix = config.prefix || '/api';
31
+ fastify.decorate('metaFormat', metaFormat);
32
+ fastify.decorate('getFilterSQL', getFilterSQL);
33
+ fastify.decorate('getTemplate', getTemplate);
34
+
35
+ fastify.get(`${prefix}/suggest/:data`, {}, suggest);
36
+ fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
37
+ fastify.get(`${prefix}/search`, { schema: searchTableSchema }, search);
38
+ fastify.get(`${prefix}/filter/:table`, {}, filter);
39
+ fastify.get(`${prefix}/form/:form`, {}, form);
40
+ }
41
+
42
+ export default plugin;
@@ -1,56 +1,56 @@
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}/table/gis.map`,
16
- body: {
17
- alias: 'testMap',
18
- map_id: '5400000',
19
- ord: 1,
20
- enabled: false,
21
- tags: ['unit', 'test'],
22
- },
23
- });
24
-
25
- const rep = JSON.parse(res?.body);
26
- assert.ok(rep.rows ? rep.rows[0]?.map_id : rep.map_id);
27
- });
28
-
29
- await t.test('PUT /update', async () => {
30
- const res = await app.inject({
31
- method: 'PUT',
32
- url: `${prefix}/table/gis.map/5400000`,
33
- body: {
34
- editor_id: '11',
35
- alias: 'testMapEdit',
36
- map_id: '5400000',
37
- ord: 2,
38
- enabled: true,
39
- tags: ['unittest'],
40
- },
41
- });
42
-
43
- const rep = JSON.parse(res?.body);
44
- assert.equal(rep.rows ? rep.rows[0]?.editor_id : rep.editor_id, '11');
45
- });
46
-
47
- await t.test('DELETE /delete', async () => {
48
- const res = await app.inject({
49
- method: 'DELETE',
50
- url: `${prefix}/table/gis.map/5400000`,
51
- });
52
-
53
- const rep = JSON.parse(res?.body);
54
- assert.ok(rep);
55
- });
56
- });
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}/table/gis.map`,
16
+ body: {
17
+ alias: 'testMap',
18
+ map_id: '5400000',
19
+ ord: 1,
20
+ enabled: false,
21
+ tags: ['unit', 'test'],
22
+ },
23
+ });
24
+
25
+ const rep = JSON.parse(res?.body);
26
+ assert.ok(rep.rows ? rep.rows[0]?.map_id : rep.map_id);
27
+ });
28
+
29
+ await t.test('PUT /update', async () => {
30
+ const res = await app.inject({
31
+ method: 'PUT',
32
+ url: `${prefix}/table/gis.map/5400000`,
33
+ body: {
34
+ editor_id: '11',
35
+ alias: 'testMapEdit',
36
+ map_id: '5400000',
37
+ ord: 2,
38
+ enabled: true,
39
+ tags: ['unittest'],
40
+ },
41
+ });
42
+
43
+ const rep = JSON.parse(res?.body);
44
+ assert.equal(rep.rows ? rep.rows[0]?.editor_id : rep.editor_id, '11');
45
+ });
46
+
47
+ await t.test('DELETE /delete', async () => {
48
+ const res = await app.inject({
49
+ method: 'DELETE',
50
+ url: `${prefix}/table/gis.map/5400000`,
51
+ });
52
+
53
+ const rep = JSON.parse(res?.body);
54
+ assert.ok(rep);
55
+ });
56
+ });