|
2 | 2 |
|
3 | 3 | [](https://github.com/bpmn-io/variable-resolver/actions/workflows/CI.yml) |
4 | 4 |
|
5 | | -A bpmn-js extension to add and manage additional variable extractors in bpmn diagrams. |
| 5 | +An extension for [bpmn-js](https://github.com/bpmn-io/bpmn-js) that makes the data model of the diagram available to other components. |
| 6 | + |
| 7 | +> [!NOTE] |
| 8 | +> As of version `v3` this library exposes both written and consumed variables, you can filter them via options. |
6 | 9 |
|
7 | 10 | ## Usage |
8 | 11 |
|
@@ -30,19 +33,56 @@ const modeler = new BpmnModeler({ |
30 | 33 |
|
31 | 34 | ### Retrieving variables |
32 | 35 |
|
33 | | -To retrieve the variables from a diagram, use one of the following methods of the `variableResolver` service: |
| 36 | +Retrieve the variables from a diagram using the `variableResolver` service: |
34 | 37 |
|
35 | 38 | ```javascript |
36 | | -const elementRegistry = modeler.get('elementRegistry'); |
37 | 39 | const variableResolver = modeler.get('variableResolver'); |
| 40 | +const elementRegistry = modeler.get('elementRegistry'); |
38 | 41 |
|
| 42 | +// retrieve variables relevant to an element |
39 | 43 | const task = elementRegistry.get('Task_1'); |
40 | | -const process = elementRegistry.get('Process_1'); |
41 | 44 |
|
42 | | -await variableResolver.getVariablesForElement(task); // returns variables in the scope of the element |
43 | | -await variableResolver.getProcessVariables(process); // returns all variables for the process, not filtering by scope |
| 45 | +// default: variables relevant to <task> in its visible scopes |
| 46 | +await variableResolver.getVariablesForElement(task); |
| 47 | + |
| 48 | +// variables read by <task> only |
| 49 | +await variableResolver.getVariablesForElement(task, { |
| 50 | + read: true, |
| 51 | + written: false |
| 52 | +}); |
| 53 | + |
| 54 | +// all variables written by <task> |
| 55 | +await variableResolver.getVariablesForElement(task, { written: true, read: false }); |
| 56 | + |
| 57 | +// local variables only (scope === queried element) |
| 58 | +await variableResolver.getVariablesForElement(task, { |
| 59 | + local: true, |
| 60 | + external: false |
| 61 | +}); |
| 62 | + |
| 63 | +// non-local variables only (scope !== queried element) |
| 64 | +await variableResolver.getVariablesForElement(task, { |
| 65 | + local: false, |
| 66 | + external: true |
| 67 | +}); |
| 68 | + |
| 69 | +// retrieve all variables defined in a process |
| 70 | +const processElement = elementRegistry.get('Process_1'); |
| 71 | + |
| 72 | +// returns all variables for the process (unfiltered), for local processing |
| 73 | +await variableResolver.getProcessVariables(processElement); |
44 | 74 | ``` |
45 | 75 |
|
| 76 | +`getVariablesForElement(element, options)` supports five filter switches: |
| 77 | + |
| 78 | +| Option | Default | Description | |
| 79 | +| --- | --- | --- | |
| 80 | +| `read` | `true` | Include variables consumed by the queried element | |
| 81 | +| `written` | `true` | Include variables written/created by the queried element | |
| 82 | +| `local` | `true` | Include variables local to the queried element scope | |
| 83 | +| `external` | `true` | Include variables outside the queried element scope | |
| 84 | +| `outputMappings` | `true` | Count output-mapping reads as reads | |
| 85 | + |
46 | 86 | ### Adding a variable extractor |
47 | 87 |
|
48 | 88 | To add your own variables, extend the `variableProvider` class in your extension. It only needs to implement the `getVariables` method, which takes an element as an argument and returns an array of variables you want to add to the scope of the element. The function can be asynchronous. |
@@ -76,11 +116,9 @@ export const MyExtension = { |
76 | 116 |
|
77 | 117 | ### Advanced use-cases |
78 | 118 |
|
79 | | -By default, `getVariablesForElement` and `getProcessVariables` will attempt to merge variables with the same name and scope into |
80 | | -one. Entry structure and types are then mixed. |
| 119 | +By default, `getVariablesForElement` and `getProcessVariables` merge variables with the same name and scope into one - entry structure and types are mixed in the merged representation. |
81 | 120 |
|
82 | | -In some cases, you might want access to the raw data, e.g. to run lint rules to detect potential schema mismatches between providers. |
83 | | -For this, you can use |
| 121 | +In some cases, you might want access to the raw data, e.g. to run lint rules to detect potential schema mismatches between providers. For this, you can use `getRawVariables`: |
84 | 122 |
|
85 | 123 | ```javascript |
86 | 124 | const variableResolver = modeler.get('variableResolver'); |
|
0 commit comments