@steedos/objectql 2.1.52 → 2.1.56

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 (39) hide show
  1. package/lib/dynamic-load/object_translations.js +4 -1
  2. package/lib/dynamic-load/object_translations.js.map +1 -1
  3. package/lib/dynamic-load/trigger.d.ts +1 -3
  4. package/lib/dynamic-load/trigger.js +24 -14
  5. package/lib/dynamic-load/trigger.js.map +1 -1
  6. package/lib/formula/core.js +22 -5
  7. package/lib/formula/core.js.map +1 -1
  8. package/lib/formula/index.d.ts +2 -0
  9. package/lib/formula/index.js +28 -1
  10. package/lib/formula/index.js.map +1 -1
  11. package/lib/formula/simple_params.d.ts +2 -0
  12. package/lib/formula/simple_params.js +91 -0
  13. package/lib/formula/simple_params.js.map +1 -0
  14. package/lib/formula/type.d.ts +3 -2
  15. package/lib/formula/type.js.map +1 -1
  16. package/lib/services/helpers/graphql.js +3 -0
  17. package/lib/services/helpers/graphql.js.map +1 -1
  18. package/lib/services/objectService.js +2 -1
  19. package/lib/services/objectService.js.map +1 -1
  20. package/lib/ts-types/triggerActionParams.d.ts +1 -0
  21. package/lib/types/object.js +31 -30
  22. package/lib/types/object.js.map +1 -1
  23. package/lib/types/trigger.d.ts +1 -0
  24. package/lib/types/trigger.js.map +1 -1
  25. package/lib/util/transform.js +1 -1
  26. package/lib/util/transform.js.map +1 -1
  27. package/package.json +8 -8
  28. package/src/dynamic-load/object_translations.ts +3 -1
  29. package/src/dynamic-load/trigger.ts +22 -15
  30. package/src/formula/core.ts +19 -2
  31. package/src/formula/index.ts +21 -1
  32. package/src/formula/simple_params.ts +92 -0
  33. package/src/formula/type.ts +4 -3
  34. package/src/services/helpers/graphql.ts +3 -0
  35. package/src/services/objectService.ts +2 -1
  36. package/src/ts-types/triggerActionParams.ts +1 -0
  37. package/src/types/object.ts +9 -6
  38. package/src/types/trigger.ts +1 -0
  39. package/src/util/transform.ts +1 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@steedos/objectql",
3
3
  "private": false,
4
- "version": "2.1.52",
4
+ "version": "2.1.56",
5
5
  "description": "",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
@@ -18,12 +18,12 @@
18
18
  "@salesforce/dev-config": "^1.4.4",
19
19
  "@salesforce/kit": "^1.0.4",
20
20
  "@salesforce/ts-types": "^1.1.1",
21
- "@steedos/filters": "2.1.52",
22
- "@steedos/formula": "2.1.52",
23
- "@steedos/metadata-core": "2.1.52",
21
+ "@steedos/filters": "2.1.56",
22
+ "@steedos/formula": "2.1.56",
23
+ "@steedos/metadata-core": "2.1.56",
24
24
  "@steedos/odata-v4-typeorm": "^1.20.1",
25
- "@steedos/schemas": "2.1.52",
26
- "@steedos/standard-objects": "2.1.52",
25
+ "@steedos/schemas": "2.1.56",
26
+ "@steedos/standard-objects": "2.1.56",
27
27
  "@types/express": "^4.16.1",
28
28
  "@types/mongodb": "^3.1.22",
29
29
  "@types/node": "^11.10.4",
@@ -57,7 +57,7 @@
57
57
  },
58
58
  "devDependencies": {
59
59
  "@salesforce/dev-scripts": "0.3.12",
60
- "@steedos/meteor-bundle-runner": "2.1.52",
60
+ "@steedos/meteor-bundle-runner": "2.1.56",
61
61
  "@types/chai": "^4.1.7",
62
62
  "@types/chai-as-promised": "7.1.0",
63
63
  "@types/mocha": "^5.2.6",
@@ -77,5 +77,5 @@
77
77
  "publishConfig": {
78
78
  "access": "public"
79
79
  },
80
- "gitHead": "cc48f943b363921f18933d1e9d5d77dd5cb2185c"
80
+ "gitHead": "a8eed2e9927d67c38df80be1474cb6b7f5b0159c"
81
81
  }
@@ -34,5 +34,7 @@ export const addObjectTranslationsFiles = async (filePath: string)=>{
34
34
 
35
35
  export const getObjectTranslations = async ()=>{
36
36
  const metadataObjectTranslations = await getSteedosSchema().metadataBroker.call('translations.getObjectTranslations')
37
- return _.pluck(metadataObjectTranslations, 'metadata')
37
+ if(metadataObjectTranslations){
38
+ return _.pluck(metadataObjectTranslations, 'metadata')
39
+ }
38
40
  }
@@ -35,8 +35,8 @@ export const addObjectListenerConfig = (json: SteedosListenerConfig) => {
35
35
  throw new Error('missing attribute listenTo')
36
36
  }
37
37
 
38
- if (!_.isString(json.listenTo) && !_.isFunction(json.listenTo)) {
39
- throw new Error('listenTo must be a function or string')
38
+ if (!_.isString(json.listenTo) && !_.isFunction(json.listenTo) && !_.isArray(json.listenTo) && !_.isRegExp(json.listenTo)) {
39
+ throw new Error('listenTo must be a function or string or array or regExp')
40
40
  }
41
41
 
42
42
  let object_name = '';
@@ -46,22 +46,29 @@ export const addObjectListenerConfig = (json: SteedosListenerConfig) => {
46
46
  } else if (_.isFunction(json.listenTo)) {
47
47
  object_name = json.listenTo()
48
48
  }
49
-
50
- let object = getObjectConfig(object_name);
51
- if (object) {
52
- if(!object.listeners){
53
- object.listeners = {}
49
+ if(object_name){
50
+ let object = getObjectConfig(object_name);
51
+ if (object) {
52
+ if(!object.listeners){
53
+ object.listeners = {}
54
+ }
55
+ delete json.listenTo
56
+ const license = clone(json);
57
+ license.name = json._id || getMD5(JSONStringify(json));
58
+ object.listeners[license.name] = license
59
+ if(object.datasource === 'meteor'){
60
+ util.extend(object, {triggers: transformListenersToTriggers(object, license)})
61
+ }
54
62
  }
55
- delete json.listenTo
56
- const license = clone(json);
57
- license.name = json._id || getMD5(JSONStringify(json));
58
- object.listeners[license.name] = license
59
- if(object.datasource === 'meteor'){
60
- util.extend(object, {triggers: transformListenersToTriggers(object, license)})
63
+ addLazyLoadListeners(object_name, Object.assign({}, json, {listenTo: object_name}));
64
+ return Object.assign({}, json, {listenTo: object_name});
65
+ }else{
66
+ if(_.isRegExp(json.listenTo)){
67
+ return Object.assign({}, json, {listenTo: json.listenTo.toString()});
68
+ }else{
69
+ return json;
61
70
  }
62
71
  }
63
- addLazyLoadListeners(object_name, Object.assign({}, json, {listenTo: object_name}));
64
- return Object.assign({}, json, {listenTo: object_name});
65
72
  }
66
73
 
67
74
  export const loadObjectTriggers = function (filePath: string){
@@ -11,6 +11,7 @@ import _ = require('lodash')
11
11
  // import _eval = require('eval')
12
12
  import { extract, parse } from '@steedos/formula';
13
13
  import { getFieldSubstitution, FormulonDataType } from './params'
14
+ import { getSimpleParamSubstitution } from './simple_params'
14
15
 
15
16
  /**
16
17
  * 根据公式内容,取出其中{}中的变量
@@ -63,7 +64,7 @@ export const computeFormulaParams = async (doc: JsonMap, vars: Array<SteedosForm
63
64
  let params: Array<SteedosFormulaParamTypeConfig> = [];
64
65
  const spaceId = doc.space;
65
66
  if (vars && vars.length) {
66
- for (let { key, paths, is_user_var: isUserVar } of vars) {
67
+ for (let { key, paths, is_user_var: isUserVar, is_simple_var: isSimpleVar } of vars) {
67
68
  key = key.trim();
68
69
  // 如果变量key以$user开头,则解析为userSession,此时paths为空
69
70
  let tempValue: any;
@@ -83,6 +84,16 @@ export const computeFormulaParams = async (doc: JsonMap, vars: Array<SteedosForm
83
84
  // tempFormulaParams[FormulaUserKey] = currentUserId;
84
85
  // tempValue = evalFieldFormula(tepmFormula, tempFormulaParams);
85
86
  }
87
+ if(isSimpleVar){
88
+ // 普通变量,取参数值时直接取值,而不用走变量上的paths属性。
89
+ // 注意未传入objectName时,公式中的user var的isSimpleVar为false,还是走下面的paths取值逻辑。
90
+ tempValue = <any>doc[key];
91
+ params.push({
92
+ key: key,
93
+ value: tempValue
94
+ });
95
+ continue;
96
+ }
86
97
  tempValue = _.reduce(paths, (reslut, next, index) => {
87
98
  if (index === 0) {
88
99
  if (isUserVar) {
@@ -176,7 +187,13 @@ export const runFormula = function (formula: string, params: Array<SteedosFormul
176
187
  // formulaParams[key] = value;
177
188
  // 把{}括起来的变量替换为计算得到的变量值
178
189
  // formula = formula.replace(`{${key}}`, `__params["${key}"]`);
179
- formulaParams[key] = getFieldSubstitution(path.reference_from, path.field_name, value, blankValue);
190
+ if(path){
191
+ formulaParams[key] = getFieldSubstitution(path.reference_from, path.field_name, value, blankValue);
192
+ }
193
+ else{
194
+ // 变量中没有path属性说明是普通变量
195
+ formulaParams[key] = getSimpleParamSubstitution(value, blankValue);
196
+ }
180
197
  });
181
198
 
182
199
  // console.log("===runFormula===formula====", formula);
@@ -58,4 +58,24 @@ async function _computeFormula(formula: string, objectName:string, data: any, cu
58
58
  return runFormula(formula, params, options, {objectName});
59
59
  }
60
60
 
61
- export const computeFormula = _computeFormula
61
+ export const computeFormula = _computeFormula
62
+
63
+ async function _computeSimpleFormula(formula: string, data: any, currentUserId?: string, spaceId?: string, options?: SteedosFormulaOptions) {
64
+ // objectConfig参数值设置为null传入computeFormulaVarsAndQuotes表示计算不带objectConfig参数的普通公式变量
65
+ const varsAndQuotes = await computeFormulaVarsAndQuotes(formula, null);
66
+ const vars = varsAndQuotes.vars;
67
+ if (!currentUserId) {
68
+ const required = isCurrentUserIdRequiredForFormulaVars(vars);
69
+ if(required){
70
+ throw new Error(`The param 'currentUserId' is required for formula ${formula.replace("$", "\\$")}`);
71
+ }
72
+ }
73
+ if(spaceId){
74
+ data.space = spaceId;
75
+ }
76
+
77
+ let params = await computeFormulaParams(data, vars, currentUserId);
78
+ return runFormula(formula, params, options);
79
+ }
80
+
81
+ export const computeSimpleFormula = _computeSimpleFormula
@@ -0,0 +1,92 @@
1
+ import { SteedosFormulaBlankValue } from './type';
2
+ import { FormulonDataType } from './params';
3
+
4
+ const VALUE_TYPES_NOT_SUPPORTED = ["function"];
5
+
6
+ function getSubstitutionDataType(value: any){
7
+ const valueType = typeof value;
8
+ let dateType: FormulonDataType;
9
+ if(VALUE_TYPES_NOT_SUPPORTED.indexOf(dateType) > -1){
10
+ throw new Error(`runFormula:Catch an error the param type "${valueType}" is not supported while eval formula with the params value: "${value}"`);
11
+ }
12
+ switch (valueType) {
13
+ case 'string':
14
+ dateType = FormulonDataType.Text;
15
+ break;
16
+ case 'number':
17
+ dateType = FormulonDataType.Number;
18
+ break;
19
+ case 'boolean':
20
+ dateType = FormulonDataType.Checkbox;
21
+ break;
22
+ case 'object':
23
+ if(value === null){
24
+ dateType = FormulonDataType.Null;
25
+ }
26
+ else if (value instanceof Date){
27
+ dateType = FormulonDataType.Datetime;
28
+ }
29
+ else{
30
+ throw new Error(`runFormula:Catch an error the param type "${valueType}" is not supported while eval formula with the params value: "${value}"`);
31
+ }
32
+ break;
33
+ case 'undefined':
34
+ dateType = FormulonDataType.Null;
35
+ break;
36
+ default:
37
+ break;
38
+ }
39
+ return dateType;
40
+ }
41
+
42
+ function getSubstitutionOptions(dataType: string, value: any){
43
+ switch (dataType) {
44
+ case FormulonDataType.Number:
45
+ let scale = 0;
46
+ let str = value.toString();
47
+ let dotStr = str.toString().split(".")[1];
48
+ let dotCount = dotStr && dotStr.length;
49
+ if(dotCount > 0){
50
+ scale = dotCount;
51
+ }
52
+ return { scale };
53
+ case FormulonDataType.Text:
54
+ return {length: Number.MAX_VALUE};
55
+ default:
56
+ return ;
57
+ }
58
+ }
59
+
60
+ function getSubstitutionValue(dataType: string, value: any, blankValue: SteedosFormulaBlankValue){
61
+ switch (dataType) {
62
+ case FormulonDataType.Text:
63
+ if(value === null || value === undefined){
64
+ return '';
65
+ }
66
+ break;
67
+ case FormulonDataType.Number:
68
+ if(value === null || value === undefined){
69
+ if(blankValue === SteedosFormulaBlankValue.blanks){
70
+ return null;
71
+ }
72
+ else{
73
+ return 0;
74
+ }
75
+ }
76
+ break;
77
+ default:
78
+ break;
79
+ }
80
+ return value;
81
+ }
82
+
83
+ export function getSimpleParamSubstitution(value: any, blankValue: SteedosFormulaBlankValue){
84
+ let fieldSubstitution: any = {
85
+ type: 'literal',
86
+ value: value
87
+ }
88
+ fieldSubstitution.dataType = getSubstitutionDataType(value);
89
+ fieldSubstitution.options = getSubstitutionOptions(fieldSubstitution.dataType, value);
90
+ fieldSubstitution.value = getSubstitutionValue(fieldSubstitution.dataType, value, blankValue);
91
+ return fieldSubstitution;
92
+ }
@@ -44,7 +44,7 @@ export type SteedosFormulaVarPathTypeConfig = {
44
44
  */
45
45
  export type SteedosFormulaParamTypeConfig = {
46
46
  key: string,
47
- path: any,
47
+ path?: any,
48
48
  value: any
49
49
  }
50
50
 
@@ -62,8 +62,9 @@ export type SteedosFormulaParamTypeConfig = {
62
62
  */
63
63
  export type SteedosFormulaVarTypeConfig = {
64
64
  key: string,
65
- paths: Array<SteedosFormulaVarPathTypeConfig>,
66
- is_user_var?: boolean
65
+ paths?: Array<SteedosFormulaVarPathTypeConfig>,
66
+ is_user_var?: boolean,
67
+ is_simple_var?: boolean //当不传入objectApiName时变量上设置该属性表示一个普通的变量,此时paths为空
67
68
  }
68
69
 
69
70
  export type SteedosFieldFormulaTypeConfig = {
@@ -28,6 +28,9 @@ const GRAPHQL_ACTION_PREFIX = 'graphql_';
28
28
  export function generateActionGraphqlProp(actionName: string, objectConfig: SteedosObjectTypeConfig) {
29
29
  let gplObj: any = {};
30
30
  let objectName = objectConfig.name;
31
+ if (objectName == 'users') {
32
+ return gplObj;
33
+ }
31
34
  switch (actionName) {
32
35
  case 'count':
33
36
  gplObj.query = gql`
@@ -566,6 +566,7 @@ module.exports = {
566
566
  ctx.broker.emit(`${getObjectServiceName(field.reference_to)}.refresh`, {});
567
567
  }
568
568
  })
569
+ // console.log(`ctx.broker.destroyService`, this.object.name);
569
570
  ctx.broker.destroyService(this);
570
571
  if(onDestroyObjectService && _.isFunction(onDestroyObjectService)){
571
572
  onDestroyObjectService(objectApiName);
@@ -650,7 +651,7 @@ module.exports = {
650
651
  try {
651
652
  Creator.loadObjects(objectConfig, objectConfig.name);
652
653
  } catch (error) {
653
- this.logger.error(error)
654
+ this.logger.error('metadata.objects.inserted error', objectConfig.name, error)
654
655
  }
655
656
  }).promise();
656
657
  }
@@ -2,6 +2,7 @@ import { JsonMap } from "@salesforce/ts-types";
2
2
  import { SteedosIDType } from "..";
3
3
 
4
4
  export type TriggerActionParams = {
5
+ objectName?: string
5
6
  isExecuting?: boolean,
6
7
  isInsert?: boolean,
7
8
  isUpdate?: boolean,
@@ -366,11 +366,14 @@ export class SteedosObjectType extends SteedosObjectProperties {
366
366
 
367
367
  for (const trigger of triggers) {
368
368
  let params = generateActionParams(when, context); //参考sf
369
- try {
370
- await this._schema.metadataBroker.call(`${trigger.service.name}.${trigger.metadata.action}`, params)
371
- } catch (error) {
372
- console.error(error)
373
- }
369
+ await this._schema.metadataBroker.call(`${trigger.service.name}.${trigger.metadata.action}`, params).catch((error)=>{
370
+ //如果action trigger 下线,则只打印error
371
+ if(error && _.isObject(error) && error.type === 'SERVICE_NOT_AVAILABLE'){
372
+ console.error(`runTriggerActions error`, error)
373
+ }else{
374
+ throw error
375
+ }
376
+ })
374
377
  }
375
378
 
376
379
  }
@@ -1257,7 +1260,7 @@ export class SteedosObjectType extends SteedosObjectProperties {
1257
1260
 
1258
1261
  let userSession = args[args.length - 1]
1259
1262
 
1260
- let context: SteedosTriggerContextConfig = { userId: userSession ? userSession.userId : undefined, spaceId: userSession ? userSession.spaceId : undefined }
1263
+ let context: SteedosTriggerContextConfig = { objectName: this.name, userId: userSession ? userSession.userId : undefined, spaceId: userSession ? userSession.spaceId : undefined }
1261
1264
 
1262
1265
  if (method === 'find' || method === 'findOne' || method === 'count') {
1263
1266
  context.query = args[args.length - 2]
@@ -12,6 +12,7 @@ export type SteedosTriggerContextConfig = {
12
12
  previousDoc?: JsonMap, //仅afterUpdate, afterDelete时存在此属性
13
13
  query?: SteedosQueryOptions,
14
14
  data?: JsonMap //仅afterFind,afterCount时存在此属性
15
+ objectName?: string
15
16
  }
16
17
 
17
18
  export type SteedosTriggerTypeConfig = {
@@ -155,7 +155,7 @@ export function transformListenersToTriggers(object: SteedosObjectTypeConfig, js
155
155
  }
156
156
 
157
157
  export function generateActionParams(when: string, context: SteedosTriggerContextConfig): TriggerActionParams {
158
- let params: TriggerActionParams = { isExecuting: true };
158
+ let params: TriggerActionParams = { objectName: context.objectName, isExecuting: true };
159
159
  switch (when) {
160
160
  case 'before.find':
161
161
  params.isBefore = true;