@opengis/fastify-table 1.0.25 → 1.0.26

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/Changelog.md +81 -77
  2. package/crud/controllers/insert.js +29 -29
  3. package/crud/controllers/update.js +31 -31
  4. package/crud/funcs/dataInsert.js +24 -24
  5. package/crud/funcs/dataUpdate.js +20 -20
  6. package/crud/funcs/getOpt.js +10 -10
  7. package/crud/funcs/setOpt.js +16 -16
  8. package/crud/index.js +29 -29
  9. package/dblist/controllers/deleteItem.js +12 -12
  10. package/dblist/controllers/updateItem.js +22 -22
  11. package/dblist/controllers/utils/checkItem.js +9 -9
  12. package/helper.js +28 -28
  13. package/index.js +41 -41
  14. package/notification/controllers/userNotifications.js +19 -19
  15. package/notification/funcs/addNotification.js +8 -8
  16. package/notification/index.js +19 -19
  17. package/package.json +22 -22
  18. package/pg/funcs/autoIndex.js +89 -89
  19. package/pg/funcs/getMeta.js +27 -27
  20. package/pg/funcs/getPG.js +29 -29
  21. package/pg/funcs/init.js +42 -42
  22. package/pg/funcs/pgClients.js +2 -2
  23. package/pg/pgClients.js +20 -20
  24. package/policy/funcs/checkPolicy.js +74 -74
  25. package/policy/funcs/sqlInjection.js +33 -33
  26. package/policy/index.js +14 -14
  27. package/redis/client.js +8 -8
  28. package/redis/funcs/getRedis.js +23 -23
  29. package/redis/funcs/redisClients.js +2 -2
  30. package/redis/index.js +19 -19
  31. package/server/migrations/crm.sql +57 -57
  32. package/server/migrations/log.sql +41 -41
  33. package/server/migrations/notifications.sql +14 -14
  34. package/server/templates/form/test.dataset.form.json +411 -411
  35. package/server/templates/select/test.storage.data.json +2 -2
  36. package/server/templates/table/test.dataset.table.json +24 -24
  37. package/table/controllers/data.js +57 -57
  38. package/table/controllers/filter.js +32 -32
  39. package/table/controllers/form.js +10 -10
  40. package/table/controllers/suggest.js +60 -60
  41. package/table/controllers/utils/getSelect.js +20 -20
  42. package/table/controllers/utils/getSelectMeta.js +66 -66
  43. package/table/controllers/utils/getTemplate.js +28 -28
  44. package/table/funcs/getFilterSQL/index.js +75 -75
  45. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  46. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  47. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  48. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  49. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  50. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  51. package/table/funcs/metaFormat/index.js +26 -26
  52. package/table/index.js +42 -42
  53. package/test/api/crud.test.js +56 -56
  54. package/test/api/crud.xss.test.js +71 -71
  55. package/test/api/dblist.test.js +47 -47
  56. package/test/api/notification.test.js +37 -37
  57. package/test/api/table.test.js +57 -57
  58. package/test/api/widget.test.js +39 -39
  59. package/test/funcs/notification.test.js +31 -31
  60. package/test/widget.test.js +39 -39
  61. package/widget/controllers/utils/historyFormat.js +76 -76
  62. package/widget/controllers/utils/obj2db.js +13 -13
  63. package/widget/controllers/widget.del.js +40 -40
  64. package/widget/controllers/widget.get.js +74 -74
  65. package/widget/controllers/widget.set.js +45 -45
  66. package/widget/index.js +29 -29
package/Changelog.md CHANGED
@@ -1,77 +1,81 @@
1
- # fastify-table
2
-
3
- ## 1.0.25 - 08.05.2024
4
-
5
- - decorator to hook
6
-
7
- ## 1.0.24 - 07.05.2024
8
-
9
- - getTemplate page
10
-
11
- ## 1.0.23 - 07.05.2024
12
-
13
- - getTemplate funcs
14
- - dblist api
15
-
16
- ## 1.0.22 - 03.05.2024
17
-
18
- - getFilterSQL funcs
19
-
20
- ## 1.0.21 - 03.05.2024
21
-
22
- - fix widget db structure
23
-
24
- ## 1.0.20 - 03.05.2024
25
-
26
- - fix filter separator
27
-
28
- ## 1.0.19 - 02.05.2024
29
-
30
- - widget plugin
31
- - notification plugin
32
-
33
- ## 1.0.9 - 29.04.2024
34
-
35
- - crud token support
36
- - security - xss restriction
37
-
38
- ## 1.0.8 - 29.04.2024
39
-
40
- - filter fix
41
-
42
- ## 1.0.7 - 26.04.2024
43
-
44
- - code optimization
45
-
46
- ## 1.0.6 - 25.04.2024
47
-
48
- - code optimization
49
-
50
- ## 1.0.5 - 24.04.2024
51
-
52
- - code optimization
53
-
54
- ## 1.0.4 - 20.04.2024
55
-
56
- - data api - order
57
- - suggest api - db support
58
- - del api fix
59
-
60
- ## 1.0.3 - 17.04.2024
61
-
62
- - fix unit test
63
-
64
- ## 1.0.2 - 14.04.2024
65
-
66
- - fix redis
67
-
68
- ## 1.0.1 - 14.04.2024
69
-
70
- - fix redis
71
-
72
- ## 1.0.0 - 14.04.2024
73
-
74
- - crud
75
- - pg
76
- - redis
77
- - table
1
+ # fastify-table
2
+
3
+ ## 1.0.26 - 09.05.2024
4
+
5
+ - fix getTableSql
6
+
7
+ ## 1.0.25 - 08.05.2024
8
+
9
+ - decorator to hook
10
+
11
+ ## 1.0.24 - 07.05.2024
12
+
13
+ - getTemplate page
14
+
15
+ ## 1.0.23 - 07.05.2024
16
+
17
+ - getTemplate funcs
18
+ - dblist api
19
+
20
+ ## 1.0.22 - 03.05.2024
21
+
22
+ - getFilterSQL funcs
23
+
24
+ ## 1.0.21 - 03.05.2024
25
+
26
+ - fix widget db structure
27
+
28
+ ## 1.0.20 - 03.05.2024
29
+
30
+ - fix filter separator
31
+
32
+ ## 1.0.19 - 02.05.2024
33
+
34
+ - widget plugin
35
+ - notification plugin
36
+
37
+ ## 1.0.9 - 29.04.2024
38
+
39
+ - crud token support
40
+ - security - xss restriction
41
+
42
+ ## 1.0.8 - 29.04.2024
43
+
44
+ - filter fix
45
+
46
+ ## 1.0.7 - 26.04.2024
47
+
48
+ - code optimization
49
+
50
+ ## 1.0.6 - 25.04.2024
51
+
52
+ - code optimization
53
+
54
+ ## 1.0.5 - 24.04.2024
55
+
56
+ - code optimization
57
+
58
+ ## 1.0.4 - 20.04.2024
59
+
60
+ - data api - order
61
+ - suggest api - db support
62
+ - del api fix
63
+
64
+ ## 1.0.3 - 17.04.2024
65
+
66
+ - fix unit test
67
+
68
+ ## 1.0.2 - 14.04.2024
69
+
70
+ - fix redis
71
+
72
+ ## 1.0.1 - 14.04.2024
73
+
74
+ - fix redis
75
+
76
+ ## 1.0.0 - 14.04.2024
77
+
78
+ - crud
79
+ - pg
80
+ - redis
81
+ - table
@@ -1,29 +1,29 @@
1
- import dataInsert from '../funcs/dataInsert.js';
2
- import getToken from '../funcs/getToken.js';
3
- import checkXSS from './utils/checkXSS.js';
4
- import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
-
6
- export default async function insert(req) {
7
- const loadTemplate = await getTemplate('table', req.params.table);
8
- const { table } = loadTemplate || req.params || {};
9
- if (!table) return { status: 404, message: 'table is required' };
10
-
11
- const { funcs, session, params } = req;
12
- const tokenDataString = await getToken({
13
- funcs, session, token: params.table, mode: 'a', json: 0,
14
- });
15
-
16
- const { form, add } = JSON.parse(tokenDataString || '{}');
17
-
18
- const formData = form ? await getTemplate('form', form) : {};
19
-
20
- const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
21
-
22
- if (xssCheck.error && formData?.xssCheck !== false) {
23
- req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
24
- return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
25
- }
26
-
27
- const res = await dataInsert({ table: add || table, data: req.body });
28
- return { rows: res.rows };
29
- }
1
+ import dataInsert from '../funcs/dataInsert.js';
2
+ import getToken from '../funcs/getToken.js';
3
+ import checkXSS from './utils/checkXSS.js';
4
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
+
6
+ export default async function insert(req) {
7
+ const loadTemplate = await getTemplate('table', req.params.table);
8
+ const { table } = loadTemplate || req.params || {};
9
+ if (!table) return { status: 404, message: 'table is required' };
10
+
11
+ const { funcs, session, params } = req;
12
+ const tokenDataString = await getToken({
13
+ funcs, session, token: params.table, mode: 'a', json: 0,
14
+ });
15
+
16
+ const { form, add } = JSON.parse(tokenDataString || '{}');
17
+
18
+ const formData = form ? await getTemplate('form', form) : {};
19
+
20
+ const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
21
+
22
+ if (xssCheck.error && formData?.xssCheck !== false) {
23
+ req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
24
+ return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
25
+ }
26
+
27
+ const res = await dataInsert({ table: add || table, data: req.body });
28
+ return { rows: res.rows };
29
+ }
@@ -1,31 +1,31 @@
1
- import dataUpdate from '../funcs/dataUpdate.js';
2
- import getToken from '../funcs/getToken.js';
3
- import checkXSS from './utils/checkXSS.js';
4
- import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
-
6
- export default async function update(req) {
7
- const loadTemplate = await getTemplate('table', req.params.table);
8
- const { table } = loadTemplate || req.params || {};
9
- const { id } = req.params || {};
10
- if (!req.params?.table) return { message: 'table is required', status: 404 };
11
- if (!id) return { message: 'id is required', status: 404 };
12
-
13
- const { funcs, session, params } = req;
14
- const tokenDataString = await getToken({
15
- funcs, session, token: params.table, mode: 'w', json: 0,
16
- });
17
-
18
- const tokenData = JSON.parse(tokenDataString || '{}');
19
-
20
- const formData = tokenData?.form ? await getTemplate('form', tokenData.form) : {};
21
-
22
- const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
23
-
24
- if (xssCheck.error && formData?.xssCheck !== false) {
25
- req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
26
- return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
27
- }
28
-
29
- const res = await dataUpdate({ table: tokenData?.table || table, id: tokenData?.id || id, data: req.body });
30
- return res;
31
- }
1
+ import dataUpdate from '../funcs/dataUpdate.js';
2
+ import getToken from '../funcs/getToken.js';
3
+ import checkXSS from './utils/checkXSS.js';
4
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
+
6
+ export default async function update(req) {
7
+ const loadTemplate = await getTemplate('table', req.params.table);
8
+ const { table } = loadTemplate || req.params || {};
9
+ const { id } = req.params || {};
10
+ if (!req.params?.table) return { message: 'table is required', status: 404 };
11
+ if (!id) return { message: 'id is required', status: 404 };
12
+
13
+ const { funcs, session, params } = req;
14
+ const tokenDataString = await getToken({
15
+ funcs, session, token: params.table, mode: 'w', json: 0,
16
+ });
17
+
18
+ const tokenData = JSON.parse(tokenDataString || '{}');
19
+
20
+ const formData = tokenData?.form ? await getTemplate('form', tokenData.form) : {};
21
+
22
+ const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
23
+
24
+ if (xssCheck.error && formData?.xssCheck !== false) {
25
+ req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
26
+ return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
27
+ }
28
+
29
+ const res = await dataUpdate({ table: tokenData?.table || table, id: tokenData?.id || id, data: req.body });
30
+ return res;
31
+ }
@@ -1,24 +1,24 @@
1
- import getPG from '../../pg/funcs/getPG.js';
2
- import getMeta from '../../pg/funcs/getMeta.js';
3
-
4
- export default async function dataInsert({ table, data }) {
5
- const pg = getPG({ name: 'client' });
6
- if (!data) return null;
7
- const { columns } = await getMeta(table);
8
- if (!columns) return null;
9
-
10
- const names = columns.map((el) => el.name);
11
- const filterData = Object.keys(data)
12
- .filter((el) => data[el] && names.includes(el)).map((el) => [el, data[el]]);
13
-
14
- const insertQuery = `insert into ${table}
15
-
16
- ( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
17
-
18
- values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
19
-
20
- returning *`;
21
-
22
- const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && !Array.isArray(el[1]) ? JSON.stringify(el[1]) : el[1]))]) || {};
23
- return res;
24
- }
1
+ import getPG from '../../pg/funcs/getPG.js';
2
+ import getMeta from '../../pg/funcs/getMeta.js';
3
+
4
+ export default async function dataInsert({ table, data }) {
5
+ const pg = getPG({ name: 'client' });
6
+ if (!data) return null;
7
+ const { columns } = await getMeta(table);
8
+ if (!columns) return null;
9
+
10
+ const names = columns.map((el) => el.name);
11
+ const filterData = Object.keys(data)
12
+ .filter((el) => data[el] && names.includes(el)).map((el) => [el, data[el]]);
13
+
14
+ const insertQuery = `insert into ${table}
15
+
16
+ ( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
17
+
18
+ values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
19
+
20
+ returning *`;
21
+
22
+ const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && !Array.isArray(el[1]) ? JSON.stringify(el[1]) : el[1]))]) || {};
23
+ return res;
24
+ }
@@ -1,20 +1,20 @@
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,
7
- }) {
8
- const pg = getPG({ name: 'client' });
9
- const { columns, pk } = await getMeta(table);
10
-
11
- const names = columns?.map((el) => el.name);
12
- const filterData = Object.keys(data)
13
- .filter((el) => data[el] && names?.includes(el)).map((el) => [el, data[el]]);
14
-
15
- const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => `"${key[0]}"=$${i + 2}`).join(',')}
16
- WHERE ${pk} = $1 returning *`;
17
- // console.log(updateDataset);
18
- const res = await pg.query(updateQuery, [id, ...filterData.map((el) => (typeof el[1] === 'object' && !Array.isArray(el[1]) ? JSON.stringify(el[1]) : el[1]))]).then(el => el?.rows?.[0]) || {};
19
- return res;
20
- }
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,
7
+ }) {
8
+ const pg = getPG({ name: 'client' });
9
+ const { columns, pk } = await getMeta(table);
10
+
11
+ const names = columns?.map((el) => el.name);
12
+ const filterData = Object.keys(data)
13
+ .filter((el) => data[el] && names?.includes(el)).map((el) => [el, data[el]]);
14
+
15
+ const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => `"${key[0]}"=$${i + 2}`).join(',')}
16
+ WHERE ${pk} = $1 returning *`;
17
+ // console.log(updateDataset);
18
+ const res = await pg.query(updateQuery, [id, ...filterData.map((el) => (typeof el[1] === 'object' && !Array.isArray(el[1]) ? JSON.stringify(el[1]) : el[1]))]).then(el => el?.rows?.[0]) || {};
19
+ return res;
20
+ }
@@ -1,10 +1,10 @@
1
- import getRedis from '../../redis/funcs/getRedis.js';
2
-
3
- export default async function getOpt(token, funcs) {
4
- const rclient = getRedis({ db: 0, funcs });
5
-
6
- const key = `opt:${token}`;
7
- const data = await rclient.get(key);
8
- if (!data) return null;
9
- return JSON.parse(data);
10
- }
1
+ import getRedis from '../../redis/funcs/getRedis.js';
2
+
3
+ export default async function getOpt(token, funcs) {
4
+ const rclient = getRedis({ db: 0, funcs });
5
+
6
+ const key = `opt:${token}`;
7
+ const data = await rclient.get(key);
8
+ if (!data) return null;
9
+ return JSON.parse(data);
10
+ }
@@ -1,16 +1,16 @@
1
- import { createHash } from 'crypto';
2
- import getRedis from '../../redis/funcs/getRedis.js';
3
-
4
- function md5(string) {
5
- return createHash('md5').update(string).digest('hex');
6
- }
7
-
8
- export default async function setOpt(params) {
9
- const token = Buffer.from(md5(typeof params === 'object' ? JSON.stringify(params) : params), 'hex').toString('base64').replace(/[+-=]+/g, '');
10
- // const token = md5(params);
11
- const key = `opt:${token}`;
12
-
13
- const rclient = getRedis({ db: 0, funcs: params.funcs });
14
- await rclient.set(key, JSON.stringify(params), 'EX', 60 * 60);
15
- return token;
16
- }
1
+ import { createHash } from 'crypto';
2
+ import getRedis from '../../redis/funcs/getRedis.js';
3
+
4
+ function md5(string) {
5
+ return createHash('md5').update(string).digest('hex');
6
+ }
7
+
8
+ export default async function setOpt(params) {
9
+ const token = Buffer.from(md5(typeof params === 'object' ? JSON.stringify(params) : params), 'hex').toString('base64').replace(/[+-=]+/g, '');
10
+ // const token = md5(params);
11
+ const key = `opt:${token}`;
12
+
13
+ const rclient = getRedis({ db: 0, funcs: params.funcs });
14
+ await rclient.set(key, JSON.stringify(params), 'EX', 60 * 60);
15
+ return token;
16
+ }
package/crud/index.js CHANGED
@@ -1,29 +1,29 @@
1
- import getOPt from './funcs/getOpt.js';
2
- import setOpt from './funcs/setOpt.js';
3
- import isFileExists from './funcs/isFileExists.js';
4
- import dataUpdate from './funcs/dataUpdate.js';
5
- import dataInsert from './funcs/dataInsert.js';
6
-
7
- import update from './controllers/update.js';
8
- import insert from './controllers/insert.js';
9
- import deleteCrud from './controllers/deleteCrud.js';
10
-
11
- // import config from '../config.js';
12
-
13
- async function plugin(fastify, config = {}) {
14
- const prefix = config.prefix || '/api';
15
- // funcs
16
- fastify.decorate('setOpt', setOpt);
17
- fastify.decorate('getOpt', getOPt);
18
- fastify.decorate('dataUpdate', dataUpdate);
19
- fastify.decorate('dataInsert', dataInsert);
20
-
21
- fastify.decorate('isFileExists', isFileExists);
22
-
23
- // api
24
- fastify.put(`${prefix}/table/:table/:id`, {}, update);
25
- fastify.delete(`${prefix}/table/:table/:id`, {}, deleteCrud);
26
- fastify.post(`${prefix}/table/:table`, {}, insert);
27
- }
28
-
29
- export default plugin;
1
+ import getOPt from './funcs/getOpt.js';
2
+ import setOpt from './funcs/setOpt.js';
3
+ import isFileExists from './funcs/isFileExists.js';
4
+ import dataUpdate from './funcs/dataUpdate.js';
5
+ import dataInsert from './funcs/dataInsert.js';
6
+
7
+ import update from './controllers/update.js';
8
+ import insert from './controllers/insert.js';
9
+ import deleteCrud from './controllers/deleteCrud.js';
10
+
11
+ // import config from '../config.js';
12
+
13
+ async function plugin(fastify, config = {}) {
14
+ const prefix = config.prefix || '/api';
15
+ // funcs
16
+ fastify.decorate('setOpt', setOpt);
17
+ fastify.decorate('getOpt', getOPt);
18
+ fastify.decorate('dataUpdate', dataUpdate);
19
+ fastify.decorate('dataInsert', dataInsert);
20
+
21
+ fastify.decorate('isFileExists', isFileExists);
22
+
23
+ // api
24
+ fastify.put(`${prefix}/table/:table/:id`, {}, update);
25
+ fastify.delete(`${prefix}/table/:table/:id`, {}, deleteCrud);
26
+ fastify.post(`${prefix}/table/:table`, {}, insert);
27
+ }
28
+
29
+ export default plugin;
@@ -1,12 +1,12 @@
1
- import { existsSync } from 'fs';
2
- import { readFile, writeFile } from 'fs/promises';
3
-
4
- export default async function deleteItem({ params = {} }) {
5
- const { id } = params;
6
- if (!id) return { error: 'not enough params', status: 400 };
7
- if (!existsSync('dblist.json')) return { data: [] };
8
- const data = JSON.parse(await readFile('dblist.json') || '[]');
9
-
10
- await writeFile('dblist.json', JSON.stringify(data.filter((el) => el.id !== id)));
11
- return { data };
12
- }
1
+ import { existsSync } from 'fs';
2
+ import { readFile, writeFile } from 'fs/promises';
3
+
4
+ export default async function deleteItem({ params = {} }) {
5
+ const { id } = params;
6
+ if (!id) return { error: 'not enough params', status: 400 };
7
+ if (!existsSync('dblist.json')) return { data: [] };
8
+ const data = JSON.parse(await readFile('dblist.json') || '[]');
9
+
10
+ await writeFile('dblist.json', JSON.stringify(data.filter((el) => el.id !== id)));
11
+ return { data };
12
+ }
@@ -1,22 +1,22 @@
1
- import { existsSync } from 'fs';
2
- import { readFile, writeFile } from 'fs/promises';
3
-
4
- import checkItem from './utils/checkItem.js';
5
-
6
- export default async function updateItem({ body = {}, params = {} }) {
7
- const { id } = params;
8
- if (!id) return { error: 'not enough params', status: 400 };
9
-
10
- if (!existsSync('dblist.json')) {
11
- return { error: 'nothing to update: 1', status: 400 };
12
- }
13
-
14
- const check = checkItem(body.data);
15
- if (check?.error) return check;
16
-
17
- const data = JSON.parse(await readFile('dblist.json') || '[]');
18
- data.filter((el) => el.id === id)?.forEach((el) => Object.assign(el, body.data));
19
-
20
- await writeFile('dblist.json', JSON.stringify(data));
21
- return { data };
22
- }
1
+ import { existsSync } from 'fs';
2
+ import { readFile, writeFile } from 'fs/promises';
3
+
4
+ import checkItem from './utils/checkItem.js';
5
+
6
+ export default async function updateItem({ body = {}, params = {} }) {
7
+ const { id } = params;
8
+ if (!id) return { error: 'not enough params', status: 400 };
9
+
10
+ if (!existsSync('dblist.json')) {
11
+ return { error: 'nothing to update: 1', status: 400 };
12
+ }
13
+
14
+ const check = checkItem(body.data);
15
+ if (check?.error) return check;
16
+
17
+ const data = JSON.parse(await readFile('dblist.json') || '[]');
18
+ data.filter((el) => el.id === id)?.forEach((el) => Object.assign(el, body.data));
19
+
20
+ await writeFile('dblist.json', JSON.stringify(data));
21
+ return { data };
22
+ }
@@ -1,9 +1,9 @@
1
- export default function validateItem(item) {
2
- if (!item || typeof item !== 'object' || Array.isArray(item)) {
3
- return { error: 'param data is invalid: 1', status: 400 };
4
- }
5
- if (!item.options?.database || Object.keys(item).filter((key) => !['key', 'name', 'options'].includes(key) || (typeof item[key] !== 'string' && key !== 'options')).length) {
6
- return { error: 'param data is invalid: 2', status: 400 };
7
- }
8
- return null;
9
- }
1
+ export default function validateItem(item) {
2
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
3
+ return { error: 'param data is invalid: 1', status: 400 };
4
+ }
5
+ if (!item.options?.database || Object.keys(item).filter((key) => !['key', 'name', 'options'].includes(key) || (typeof item[key] !== 'string' && key !== 'options')).length) {
6
+ return { error: 'param data is invalid: 2', status: 400 };
7
+ }
8
+ return null;
9
+ }
package/helper.js CHANGED
@@ -1,28 +1,28 @@
1
- // This file contains code that we reuse
2
- // between our tests.
3
- import Fastify from 'fastify';
4
- import config from './test/config.js';
5
- import appService from './index.js';
6
-
7
- import rclient from './redis/client.js';
8
- import pgClients from './pg/pgClients.js';
9
-
10
- // automatically build and tear down our instance
11
- async function build(t) {
12
- // you can set all the options supported by the fastify CLI command
13
- // const argv = [AppPath]
14
- process.env.NODE_ENV = 'production';
15
- const app = Fastify({ logger: false });
16
- app.register(appService, config);
17
- // close the app after we are done
18
- t.after(() => {
19
- // console.log('close app');
20
- pgClients.client.end();
21
- rclient.quit();
22
- app.close();
23
- });
24
-
25
- return app;
26
- }
27
-
28
- export default build;
1
+ // This file contains code that we reuse
2
+ // between our tests.
3
+ import Fastify from 'fastify';
4
+ import config from './test/config.js';
5
+ import appService from './index.js';
6
+
7
+ import rclient from './redis/client.js';
8
+ import pgClients from './pg/pgClients.js';
9
+
10
+ // automatically build and tear down our instance
11
+ async function build(t) {
12
+ // you can set all the options supported by the fastify CLI command
13
+ // const argv = [AppPath]
14
+ process.env.NODE_ENV = 'production';
15
+ const app = Fastify({ logger: false });
16
+ app.register(appService, config);
17
+ // close the app after we are done
18
+ t.after(() => {
19
+ // console.log('close app');
20
+ pgClients.client.end();
21
+ rclient.quit();
22
+ app.close();
23
+ });
24
+
25
+ return app;
26
+ }
27
+
28
+ export default build;