@steedos/service-objectql 2.5.0-beta.9 → 2.5.0

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 (2) hide show
  1. package/package.json +3 -3
  2. package/package.service.js +57 -43
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steedos/service-objectql",
3
- "version": "2.5.0-beta.9",
3
+ "version": "2.5.0",
4
4
  "main": "package.service.js",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -13,8 +13,8 @@
13
13
  "description": "steedos package",
14
14
  "repository": {},
15
15
  "license": "MIT",
16
- "gitHead": "a2623b433dd42632dc54d23c51b718bd4d1a2fdc",
16
+ "gitHead": "c4b366d1366ab52eb53da1a3ff4a80b16fd73cc0",
17
17
  "dependencies": {
18
- "@steedos/objectql": "2.5.0-beta.9"
18
+ "@steedos/objectql": "2.5.0"
19
19
  }
20
20
  }
@@ -1,14 +1,15 @@
1
1
  /*
2
2
  * @Author: sunhaolin@hotoa.com
3
3
  * @Date: 2023-03-23 15:12:14
4
- * @LastEditors: sunhaolin@hotoa.com
5
- * @LastEditTime: 2023-03-27 17:15:39
4
+ * @LastEditors: baozhoutao@steedos.com
5
+ * @LastEditTime: 2023-05-18 14:33:09
6
6
  * @Description:
7
7
  */
8
8
  "use strict";
9
9
  // @ts-check
10
+ const objectql = require('@steedos/objectql');
11
+ const { getObject } = objectql;
10
12
 
11
- const { getObject } = require('@steedos/objectql');
12
13
 
13
14
  /**
14
15
  * @typedef {import('moleculer').Context} Context Moleculer's Context
@@ -34,19 +35,6 @@ module.exports = {
34
35
  * Actions
35
36
  */
36
37
  actions: {
37
- aggregate: {
38
- params: {
39
- objectName: { type: "string" },
40
- query: { type: "object" },
41
- externalPipeline: { type: "array", items: "object" }
42
- },
43
- async handler(ctx) {
44
- const userSession = ctx.meta.user;
45
- const { objectName, query, externalPipeline } = ctx.params;
46
- const obj = getObject(objectName)
47
- return await obj.aggregate(query, externalPipeline, userSession)
48
- }
49
- },
50
38
  find: {
51
39
  params: {
52
40
  objectName: { type: "string" },
@@ -207,32 +195,6 @@ module.exports = {
207
195
  }
208
196
  }
209
197
  },
210
- directAggregate: {
211
- params: {
212
- objectName: { type: "string" },
213
- query: { type: "object" },
214
- externalPipeline: { type: "array", items: "object" }
215
- },
216
- async handler(ctx) {
217
- const userSession = ctx.meta.user;
218
- const { objectName, query, externalPipeline } = ctx.params;
219
- const obj = getObject(objectName)
220
- return await obj.directAggregate(query, externalPipeline, userSession)
221
- }
222
- },
223
- directAggregatePrefixalPipeline: {
224
- params: {
225
- objectName: { type: "string" },
226
- query: { type: "object" },
227
- prefixalPipeline: { type: "array", items: "object" }
228
- },
229
- async handler(ctx) {
230
- const userSession = ctx.meta.user;
231
- const { objectName, query, prefixalPipeline } = ctx.params;
232
- const obj = getObject(objectName)
233
- return await obj.directAggregatePrefixalPipeline(query, prefixalPipeline, userSession)
234
- }
235
- },
236
198
  directFind: {
237
199
  params: {
238
200
  objectName: { type: "string" },
@@ -521,6 +483,16 @@ module.exports = {
521
483
  const obj = getObject(objectName)
522
484
  return await obj.allowDelete(userSession);
523
485
  }
486
+ },
487
+ encryptFieldValue: {
488
+ params: {
489
+ objectName: { type: "string" },
490
+ doc: { type: "object", optional: true },
491
+ },
492
+ async handler(ctx) {
493
+ const { objectName, doc } = ctx.params;
494
+ return await this.encryptFieldValue(objectName, doc);
495
+ }
524
496
  }
525
497
 
526
498
  },
@@ -536,8 +508,50 @@ module.exports = {
536
508
  * Methods
537
509
  */
538
510
  methods: {
511
+ /**
512
+ * 获取需要加密的字段
513
+ * @param {*} objectName
514
+ * @returns
515
+ */
516
+ getRequireEncryptionFields: async function (objectName) {
517
+ const objFields = await objectql.getObject(objectName).getFields();
518
+ const requireEncryptionFields = {};
519
+ // 遍历所有字段,整理出要求加密的字段
520
+ for (const key in objFields) {
521
+ if (Object.hasOwnProperty.call(objFields, key)) {
522
+ const field = objFields[key];
523
+ if (field.enable_encryption) {
524
+ requireEncryptionFields[key] = field;
525
+ }
526
+ }
527
+ }
528
+ return requireEncryptionFields;
529
+ },
539
530
 
540
-
531
+ /**
532
+ * 加密字段值
533
+ * @param {*} objectName
534
+ * @param {*} doc
535
+ */
536
+ encryptFieldValue: async function (objectName, doc) {
537
+ try {
538
+ const datasource = objectql.getDataSource('default');
539
+ const requireEncryptionFields = await this.getRequireEncryptionFields(objectName);
540
+ for (const key in requireEncryptionFields) {
541
+ if (Object.hasOwnProperty.call(requireEncryptionFields, key)) {
542
+ const field = requireEncryptionFields[key];
543
+ // 判断是加密字段并且值不为空,且还未加密过
544
+ if (field.enable_encryption && _.has(doc, key) && doc[key] && !doc[key].buffer && !doc[key].sub_type) {
545
+ doc[key] = await datasource.adapter.encryptValue(doc[key]);
546
+ }
547
+ }
548
+ }
549
+ } catch (error) {
550
+ console.error('encryptFieldValue error', objectName, doc)
551
+ console.error(`[字段级加密] 对象${objectName}中字段加密失败:`, error);
552
+ }
553
+ return doc;
554
+ }
541
555
  },
542
556
 
543
557
  /**