@opengis/fastify-table 1.0.17 → 1.0.19

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 (53) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +5 -0
  3. package/README.md +26 -26
  4. package/config.js +11 -11
  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 +20 -20
  13. package/crud/funcs/getToken.js +27 -27
  14. package/crud/funcs/isFileExists.js +13 -13
  15. package/crud/funcs/setToken.js +53 -53
  16. package/crud/index.js +29 -29
  17. package/helper.js +28 -28
  18. package/index.js +4 -0
  19. package/notification/controllers/userNotifications.js +19 -0
  20. package/notification/funcs/addNotification.js +8 -0
  21. package/notification/index.js +19 -0
  22. package/package.json +1 -1
  23. package/pg/index.js +35 -35
  24. package/server/migrations/crm.sql +55 -0
  25. package/server/migrations/log.sql +40 -0
  26. package/server/migrations/notifications.sql +14 -0
  27. package/server/templates/select/test.storage.data.json +3 -0
  28. package/server/templates/select/test.storage.data.sql +1 -0
  29. package/server/templates/table/test.dataset.table.json +25 -0
  30. package/server.js +14 -14
  31. package/table/controllers/search.js +41 -0
  32. package/table/index.js +13 -0
  33. package/test/api/crud.test.js +50 -50
  34. package/test/api/crud.xss.test.js +68 -68
  35. package/test/api/notification.test.js +37 -0
  36. package/test/api/table.test.js +12 -4
  37. package/test/api/widget.test.js +39 -0
  38. package/test/config.example +18 -18
  39. package/test/funcs/crud.test.js +76 -76
  40. package/test/funcs/notification.test.js +31 -0
  41. package/test/funcs/pg.test.js +34 -34
  42. package/test/funcs/redis.test.js +19 -19
  43. package/test/templates/cls/test.json +9 -9
  44. package/test/templates/form/cp_building.form.json +32 -32
  45. package/test/templates/select/account_id.json +3 -3
  46. package/test/templates/select/storage.data.json +2 -2
  47. package/test/templates/table/gis.dataset.table.json +20 -20
  48. package/test/widget.test.js +39 -0
  49. package/widget/controllers/utils/obj2db.js +13 -0
  50. package/widget/controllers/widget.del.js +42 -0
  51. package/widget/controllers/widget.get.js +145 -0
  52. package/widget/controllers/widget.set.js +49 -0
  53. package/widget/index.js +29 -0
package/server.js CHANGED
@@ -1,14 +1,14 @@
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
- const app = Fastify({ logger: false });
8
- app.register(appService, config);
9
- app.listen({ host: '0.0.0.0', port: process.env.PORT || 3000 }, (err) => {
10
- if (err) {
11
- app.log.error(err);
12
- process.exit(1);
13
- }
14
- });
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
+ const app = Fastify({ logger: false });
8
+ app.register(appService, config);
9
+ app.listen({ host: '0.0.0.0', port: process.env.PORT || 3000 }, (err) => {
10
+ if (err) {
11
+ app.log.error(err);
12
+ process.exit(1);
13
+ }
14
+ });
@@ -0,0 +1,41 @@
1
+ import getTemplate from './utils/getTemplate.js';
2
+ import getMeta from '../../pg/funcs/getMeta.js';
3
+ import metaFormat from '../funcs/metaFormat/index.js';
4
+
5
+ const maxLimit = 100;
6
+
7
+ export default async function data({
8
+ pg, query = {},
9
+ }) {
10
+ const time = Date.now();
11
+
12
+ const loadTable = await getTemplate('table', query.table);
13
+
14
+ if (!loadTable) { return { message: 'not found', status: 404 }; }
15
+
16
+ const { table, columns, meta } = loadTable;
17
+ const { pk } = await getMeta(table);
18
+
19
+ const cols = columns.map((el) => el.name || el).join(',');
20
+ const [orderColumn, orderDir] = (query.order || loadTable.order || '').split('-');
21
+ const order = cols.includes(orderColumn) && orderColumn?.length ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}` : '';
22
+
23
+ const limit = Math.min(maxLimit, +(query.limit || 10));
24
+ const offset = query.page && query.page > 0 ? ` offset ${(query.page - 1) * limit}` : '';
25
+
26
+ const search = meta?.search && query.key ? `(${meta?.search.concat(meta?.title ? `,${meta?.title}` : '').split(',').map(el => `${el} ilike '%${query.key}%'`).join(' or ')})` : null;
27
+
28
+ const where = [loadTable.query, search].filter((el) => el);
29
+ const q = `select ${pk ? `"${pk}" as id,` : ''} * from ${table} t where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
30
+
31
+ if (query.sql === '1') return q;
32
+
33
+ const { rows } = await pg.query(q);
34
+
35
+ const total = await pg.queryCache(`select count(*) from ${table} t where ${where.join(' and ') || 'true'}`).then((el) => el?.rows[0]?.count);
36
+
37
+ await metaFormat({ rows, table: query.table });
38
+ return {
39
+ time: Date.now() - time, total, count: rows.length, rows,
40
+ };
41
+ }
package/table/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import suggest from './controllers/suggest.js';
2
2
  import data from './controllers/data.js';
3
+ import search from './controllers/search.js';
3
4
  import filter from './controllers/filter.js';
4
5
  import form from './controllers/form.js';
5
6
  import metaFormat from './funcs/metaFormat/index.js';
@@ -12,12 +13,24 @@ const tableSchema = {
12
13
  },
13
14
  };
14
15
 
16
+ const searchTableSchema = {
17
+ querystring: {
18
+ page: { type: 'number' },
19
+ order: { type: 'string' },
20
+ desc: { type: 'string' },
21
+ filter: { type: 'string' },
22
+ key: { type: 'string' },
23
+ table: { type: 'string' },
24
+ },
25
+ };
26
+
15
27
  async function plugin(fastify, config = {}) {
16
28
  const prefix = config.prefix || '/api';
17
29
  fastify.decorate('metaFormat', metaFormat);
18
30
 
19
31
  fastify.get(`${prefix}/suggest/:data`, {}, suggest);
20
32
  fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
33
+ fastify.get(`${prefix}/search`, { schema: searchTableSchema }, search);
21
34
  fastify.get(`${prefix}/filter/:table`, {}, filter);
22
35
  fastify.get(`${prefix}/form/:form`, {}, form);
23
36
  }
@@ -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,68 +1,68 @@
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
+ 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
+ });
@@ -0,0 +1,37 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+ import config from '../config.js';
6
+
7
+ const session = { passport: { user: { uid: config.testUser?.uid || '1' } } };
8
+
9
+ import userNotifications from '../../notification/controllers/userNotifications.js';
10
+
11
+ import pgClients from '../../pg/pgClients.js';
12
+
13
+ test('api && funcs notification', async (t) => {
14
+ const app = await build(t);
15
+ const pg = pgClients.client;
16
+ /*
17
+ // require dependency
18
+ await t.test('GET /auth', async () => {
19
+ const res = await app.inject({
20
+ method: 'GET',
21
+ url: `/api/login?username=${config.testUser?.username}&password=${config.testUser?.password}`,
22
+ });
23
+ assert.ok(res.statusCode);
24
+ });
25
+ await t.test('GET /notification', async () => {
26
+ const res = await app.inject({
27
+ method: 'GET',
28
+ url: '/api/notification',
29
+ });
30
+ const rep = JSON.parse(res?.body);
31
+ assert.ok(rep.time);
32
+ }); */
33
+ await t.test('GET /notification', async () => {
34
+ const rep = await userNotifications({ pg, session });
35
+ assert.ok(rep.time);
36
+ });
37
+ });
@@ -9,7 +9,7 @@ test('api table', async (t) => {
9
9
  await t.test('GET /suggest', async () => {
10
10
  const res = await app.inject({
11
11
  method: 'GET',
12
- url: '/api/suggest/storage.data',
12
+ url: '/api/suggest/test.storage.data',
13
13
  });
14
14
  // console.log(res?.body);
15
15
  const rep = JSON.parse(res?.body);
@@ -19,17 +19,25 @@ test('api table', async (t) => {
19
19
  await t.test('GET /data', async () => {
20
20
  const res = await app.inject({
21
21
  method: 'GET',
22
- url: '/api/data/gis.dataset.table',
22
+ url: '/api/data/test.dataset.table',
23
23
  });
24
24
  // console.log(res);
25
25
  const rep = JSON.parse(res?.body);
26
26
  // console.log(rep.total);
27
27
  assert.ok(rep.total);
28
28
  });
29
+ await t.test('GET /search', async () => {
30
+ const res = await app.inject({
31
+ method: 'GET',
32
+ url: '/api/search?table=test.dataset.table&key=0',
33
+ });
34
+ const rep = JSON.parse(res?.body);
35
+ assert.ok(rep.total);
36
+ });
29
37
  await t.test('GET /form', async () => {
30
38
  const res = await app.inject({
31
39
  method: 'GET',
32
- url: '/api/form/cp_building.form',
40
+ url: '/api/form/test.dataset.form',
33
41
  });
34
42
  // console.log(res);
35
43
  const rep = JSON.parse(res?.body);
@@ -39,7 +47,7 @@ test('api table', async (t) => {
39
47
  await t.test('GET /filter', async () => {
40
48
  const res = await app.inject({
41
49
  method: 'GET',
42
- url: '/api/filter/gis.dataset.table',
50
+ url: '/api/filter/test.dataset.table',
43
51
  });
44
52
  // console.log(res);
45
53
  const rep = JSON.parse(res?.body);
@@ -0,0 +1,39 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+ import config from '../config.js';
6
+
7
+ const session = { passport: { user: { uid: config.testUser?.uid || '1' } } };
8
+
9
+ import widgetGet from '../../widget/controllers/widget.get.js';
10
+ import widgetSet from '../../widget/controllers/widget.set.js';
11
+ import widgetDel from '../../widget/controllers/widget.del.js';
12
+
13
+ import pgClients from '../../pg/pgClients.js';
14
+
15
+ test('widget api', async (t) => {
16
+ await build(t);
17
+ const pg = pgClients.client;
18
+ await t.test('POST /widget/:type/:objectid', async () => {
19
+ const body = {};
20
+ const rep = await widgetSet({
21
+ pg, params: { type: 'comment', objectid: '1' }, session, body,
22
+ });
23
+ assert.ok(rep.data);
24
+ });
25
+
26
+ let resp;
27
+ await t.test('GET /widget/:type/:objectid', async () => {
28
+ resp = await widgetGet({
29
+ pg, session, params: { type: 'comment', objectid: '1' },
30
+ });
31
+ assert.ok(resp.rows.length > 0, 'widget data get fail');
32
+ });
33
+ await t.test('DELETE /widget/:type/:objectid', async () => {
34
+ resp = await widgetDel({
35
+ pg, session, params: { type: 'comment', objectid: '1', id: resp.rows?.find((row) => row)?.comment_id },
36
+ });
37
+ assert.ok(resp.data.rowCount === 1, 'widget data delete fail');
38
+ });
39
+ });
@@ -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;
@@ -1,76 +1,76 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
- import config from '../config.js';
4
- import pgClients from '../../pg/pgClients.js';
5
- import rclient from '../../redis/client.js';
6
-
7
- import dataInsert from '../../crud/funcs/dataInsert.js';
8
- import dataUpdate from '../../crud/funcs/dataUpdate.js';
9
- import dataDelete from '../../crud/funcs/dataDelete.js';
10
- import isFileExists from '../../crud/funcs/isFileExists.js';
11
-
12
- import getOpt from '../../crud/funcs/getOpt.js';
13
- import setOpt from '../../crud/funcs/setOpt.js';
14
-
15
- import getToken from '../../crud/funcs/getToken.js';
16
- import setToken from '../../crud/funcs/setToken.js';
17
-
18
- test('funcs crud', async (t) => {
19
- await pgClients.client.init();
20
- await t.test('getOpt/setOpt', async () => {
21
- const opt = await setOpt({ table: 'gis.dataset' });
22
- const data = await getOpt(opt);
23
- // console.log(data);
24
- assert.equal(data.table, 'gis.dataset');
25
- });
26
-
27
- const id = (Math.random() * 10000).toFixed();
28
- await t.test('dataInsert', async () => {
29
- const data = await dataInsert({ table: 'gis.dataset', data: { dataset_id: id, dataset_name: '222' } });
30
- assert.equal(data.dataset_id, id);
31
- });
32
-
33
- await t.test('dataUpdate', async () => {
34
- const data = await dataUpdate({ table: 'gis.dataset', id, data: { dataset_name: '22211' } });
35
- assert.equal(data.dataset_name, '22211');
36
- });
37
-
38
- await t.test('dataDelete', async () => {
39
- const data = await dataDelete({ table: 'gis.dataset', id });
40
- assert.ok(data);
41
- });
42
-
43
- await t.test('isFileExists', async () => {
44
- const data = await isFileExists({ filepath: '../../crud/funcs/isFileExists.js' });
45
- assert.equal(data, false);
46
- });
47
-
48
- let tokens;
49
- const session = { passport: { user: { uid: '1' } } };
50
- const tokenData = JSON.stringify({ add: 'gis.dataset', form: 'test.dataset.form' });
51
-
52
- await t.test('setToken', async () => {
53
- tokens = setToken({
54
- funcs: { config },
55
- ids: [tokenData],
56
- mode: 'a',
57
- uid: 1,
58
- array: 1,
59
- });
60
- assert.equal(tokens.length, 1);
61
- });
62
- await t.test('getToken', async () => {
63
- const data = await getToken({
64
- uid: 1,
65
- token: tokens[0],
66
- mode: 'a',
67
- });
68
- assert.equal(data, tokenData);
69
- });
70
-
71
- // pgClients.client.query('delete from gis.dataset where dataset_id=$1', [id]);
72
- t.after(() => {
73
- pgClients.client?.end();
74
- rclient.quit();
75
- });
76
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+ import config from '../config.js';
4
+ import pgClients from '../../pg/pgClients.js';
5
+ import rclient from '../../redis/client.js';
6
+
7
+ import dataInsert from '../../crud/funcs/dataInsert.js';
8
+ import dataUpdate from '../../crud/funcs/dataUpdate.js';
9
+ import dataDelete from '../../crud/funcs/dataDelete.js';
10
+ import isFileExists from '../../crud/funcs/isFileExists.js';
11
+
12
+ import getOpt from '../../crud/funcs/getOpt.js';
13
+ import setOpt from '../../crud/funcs/setOpt.js';
14
+
15
+ import getToken from '../../crud/funcs/getToken.js';
16
+ import setToken from '../../crud/funcs/setToken.js';
17
+
18
+ test('funcs crud', async (t) => {
19
+ await pgClients.client.init();
20
+ await t.test('getOpt/setOpt', async () => {
21
+ const opt = await setOpt({ table: 'gis.dataset' });
22
+ const data = await getOpt(opt);
23
+ // console.log(data);
24
+ assert.equal(data.table, 'gis.dataset');
25
+ });
26
+
27
+ const id = (Math.random() * 10000).toFixed();
28
+ await t.test('dataInsert', async () => {
29
+ const data = await dataInsert({ table: 'gis.dataset', data: { dataset_id: id, dataset_name: '222' } });
30
+ assert.equal(data.dataset_id, id);
31
+ });
32
+
33
+ await t.test('dataUpdate', async () => {
34
+ const data = await dataUpdate({ table: 'gis.dataset', id, data: { dataset_name: '22211' } });
35
+ assert.equal(data.dataset_name, '22211');
36
+ });
37
+
38
+ await t.test('dataDelete', async () => {
39
+ const data = await dataDelete({ table: 'gis.dataset', id });
40
+ assert.ok(data);
41
+ });
42
+
43
+ await t.test('isFileExists', async () => {
44
+ const data = await isFileExists({ filepath: '../../crud/funcs/isFileExists.js' });
45
+ assert.equal(data, false);
46
+ });
47
+
48
+ let tokens;
49
+ const session = { passport: { user: { uid: '1' } } };
50
+ const tokenData = JSON.stringify({ add: 'gis.dataset', form: 'test.dataset.form' });
51
+
52
+ await t.test('setToken', async () => {
53
+ tokens = setToken({
54
+ funcs: { config },
55
+ ids: [tokenData],
56
+ mode: 'a',
57
+ uid: 1,
58
+ array: 1,
59
+ });
60
+ assert.equal(tokens.length, 1);
61
+ });
62
+ await t.test('getToken', async () => {
63
+ const data = await getToken({
64
+ uid: 1,
65
+ token: tokens[0],
66
+ mode: 'a',
67
+ });
68
+ assert.equal(data, tokenData);
69
+ });
70
+
71
+ // pgClients.client.query('delete from gis.dataset where dataset_id=$1', [id]);
72
+ t.after(() => {
73
+ pgClients.client?.end();
74
+ rclient.quit();
75
+ });
76
+ });