@opengis/fastify-table 1.0.29 → 1.0.30

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.
package/Changelog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.30 - 17.05.2024
4
+
5
+ - code optimization
6
+
3
7
  ## 1.0.29 - 17.05.2024
4
8
 
5
9
  - widget api post (file)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.29",
3
+ "version": "1.0.30",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -1,6 +1,5 @@
1
1
  import path from 'path';
2
2
 
3
- import obj2db from './utils/obj2db.js';
4
3
  import getMeta from '../../pg/funcs/getMeta.js';
5
4
 
6
5
  const tableList = {
@@ -25,44 +24,35 @@ export default async function widgetSet(req) {
25
24
 
26
25
  try {
27
26
  if (type === 'file') {
28
- req.params['*'] = objectid;
29
- const { message = {}, error, status } = await funcs.upload(req);
30
- if (error) {
31
- log.error('widget/file', { step: 'upload', message, error, status });
32
- return { error, status };
33
- }
34
- if (!message.name) return { error: 'upload error', status: 500 };
35
-
36
- const insertQuery = `insert into crm.file (file_id, uploaded_name, file_path, ext, size, file_status, uid, object_id)
37
- values ( '${message.result?.file_id}',
38
- '${message.name?.toLocaleLowerCase().replace(/'/g, '\'\'')}',
39
- '${message.result?.file_path?.replace(/\\/g, '/')}',
40
- '${path.extname(message.name).slice(1).toLowerCase()}',
41
- ${message.result?.size}, 1,
42
- '${session.passport?.user?.uid || 1}',
43
- nullif('${objectid}','') ) returning file_id as id`;
27
+ const file = await funcs.uploadMultiPart(req);
28
+ const extName = path.extname(file.filepath).slice(1).toLowerCase();
29
+
30
+ const data = {
31
+ uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
32
+ file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
33
+ ext: extName,
34
+ size: file?.size,
35
+ file_status: 1,
36
+ uid: req.session?.passport?.user?.uid || 1,
37
+ object_id: objectid,
38
+ };
44
39
 
45
- const { id } = pg.pk['crm.file'] ? await pg.one(insertQuery) : {};
40
+ const { rows = [] } = await funcs.dataInsert({ table: 'crm.file', data });
46
41
  return {
47
- rowCount: 1, data: 'ok', command: 'UPLOAD', id, objectid,
42
+ rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, objectid: rows[0]?.object_id,
48
43
  };
49
44
  }
50
- const { pk, columns: metaColumns } = await getMeta({ pg, table });
51
- if (!pk) return { error: 'table not found', status: 404 };
52
-
53
- const columnList = metaColumns.map((col) => col.name);
54
- const nonexistCol = Object.keys(body).filter((key) => !columnList.includes(key));
55
- const { columns, args } = obj2db({ ...body, uid: user?.uid, object_id: objectid }, nonexistCol);
45
+ const { pk } = await getMeta({ pg, table });
46
+ if (!pk) return { message: 'table not found', status: 404 };
56
47
 
57
- const query = id
58
- ? `UPDATE ${table} SET ${columns?.map((key, index) => `"${key}"=$${++index}`).join(',')} WHERE ${pkList[type]} = '${id}' `
59
- : `INSERT INTO ${table} (${columns?.join(',')}) VALUES (${columns?.map((el, index) => `$${++index}`).join(',')}) returning *;`;
48
+ const data = { ...body, uid: user?.uid, object_id: objectid };
60
49
 
61
- // console.log(query, args);
62
- const result = await pg.query(query, args);
50
+ const result = id
51
+ ? await funcs.dataUpdate({ table, data, id })
52
+ : await funcs.dataInsert({ table, data });
63
53
 
64
54
  return {
65
- rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]],
55
+ rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
66
56
  };
67
57
  }
68
58
  catch (err) {