jsSwitch
jsSwitch
component: script routing. Executes the configured JS script. The script should return an array of the names of the next or multiple chains to which the message should be routed.
JavaScript script supports ECMAScript 5.1(+) syntax specification and some ES6 specifications, such as: async/await/Promise/let. It allows calling Go custom functions in the script, please refer to udf .
# Configuration
Field | Type | Description | Default value |
---|---|---|---|
jsScript | string | js script | None |
jsScript
: You can filter msg, metadata, msgType. This field is the following function body contentfunction JsSwitch(msg, metadata, msgType) { ${jsScript} }
1
2
3- msg: Message content, if dataType=JSON, type is:
jsonObject
, you can usemsg.temperature
to operate. If dataType is other types, the field type is:string
- metadata: Message metadata, type:
jsonObject
- msgType: Message type
- Function return value type:
array
, an array of the names of the next or multiple chains to which the message should be routed
- msg: Message content, if dataType=JSON, type is:
Note
Script execution timeout configuration reference: config.ScriptMaxExecutionTime
# Relation Type
Use the script custom return value to connect to the next or multiple nodes, if there is no matching node, the Other
matching node will be used.
# Execution result
This component will not change the content of msg
, metadata
and msgType
.
# Configuration example
{
"id": "s1",
"type": "jsSwitch",
"name": "Script routing",
"configuration": {
"jsScript": "return ['one','two'];"
}
}
2
3
4
5
6
7
8
# Application example
Using this component, you can dynamically control the logic of connecting to the next or multiple nodes. For example: Automatic marketing system, after receiving an event, trigger a query of the visitor data for the last 3 days and 30 days (these two nodes are triggered in parallel), and then process the data separately.
{
"ruleChain": {
"id":"rule01",
"name": "Test rule chain",
"root": true
},
"metadata": {
"nodes": [
{
"id": "s1",
"type": "jsSwitch",
"name": "Script routing",
"configuration": {
"jsScript": "if msgType=='aa'{ return ['visitors_3days','visitors_30days'];}else {return ['visitors_3days','visitors_30days','visitors_60days'];}"
}
},
{
"id": "s2",
"type": "dbClient",
"name": "Query visitors for the last 3 days",
"configuration": {
"dbType":"mysql",
"dsn":"root:root@tcp(127.0.0.1:3306)/test",
"sql":"select * from users where DATE_SUB(CURDATE(), INTERVAL 3 DAY)<=visitor_time"
}
},
{
"id": "s3",
"type": "dbClient",
"name": "Query visitors for the last 30 days",
"configuration": {
"dbType":"mysql",
"dsn":"root:root@tcp(127.0.0.1:3306)/test",
"sql":"select * from users where DATE_SUB(CURDATE(), INTERVAL 30 DAY)<=visitor_time"
}
},
{
"id": "s4",
"type": "dbClient",
"name": "Query visitors for the last 60 days",
"configuration": {
"dbType":"mysql",
"dsn":"root:root@tcp(127.0.0.1:3306)/test",
"sql":"select * from users where DATE_SUB(CURDATE(), INTERVAL 60 DAY)<=visitor_time"
}
}
],
"connections": [
{
"fromId": "s1",
"toId": "s2",
"type": "visitors_3days"
},
{
"fromId": "s1",
"toId": "s3",
"type": "visitors_30days"
},
{
"fromId": "s1",
"toId": "s4",
"type": "visitors_60days"
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66