Skip to content

Commit a0e017c

Browse files
committed
globals docs
1 parent 93b7102 commit a0e017c

File tree

1 file changed

+120
-10
lines changed

1 file changed

+120
-10
lines changed

docs/jobs/job-writing-guide.md

Lines changed: 120 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ of key patterns in the OpenFn ecosystem which it is important to learn.
1313

1414
:::tip
1515

16-
If you're writing jobs on the platform app (Lightning), you can use the [AI Assistant](/documentation/build/ai-assistant) to help you. You'll find it in the Inspector.
16+
If you're writing jobs on the platform app (Lightning), you can use the
17+
[AI Assistant](/documentation/build/ai-assistant) to help you. You'll find it in
18+
the Inspector.
1719

1820
:::
1921

@@ -915,6 +917,111 @@ fn(state => {
915917
});
916918
```
917919

920+
## Globals
921+
922+
:::warning Availability
923+
924+
Globals are new to CLI version X.X.X and not yet available in Lightning
925+
926+
:::
927+
928+
Each step in your workflow runs in isolation: state is the only way to share
929+
information between steps.
930+
931+
Since CLI vX.X.X, you can declare global variables and functions which can be
932+
re-used throughout all steps in your workflow. This is particularly useful for
933+
declaring re-usable functions.
934+
935+
Globals are defined in a special kind of step. Everything you export from this
936+
"step" will be made globally available to all other steps.
937+
938+
For example, your globals may look like this:
939+
940+
```js
941+
export const PROJECT_ID = '1245';
942+
943+
export function toKebabCase(str) {
944+
return str.replace(/\s/g, '-');
945+
}
946+
```
947+
948+
This exports a constant, `PROJECT_ID`, and a (rather naive) helper function to
949+
convert any string into kebab-case.
950+
951+
In the CLI, you might save these to a file called `globals.js`, and reference
952+
them in `workflow.json` as a sibling of `steps`:
953+
954+
```json
955+
{
956+
"workflow": {
957+
"globals": "./globals.js",
958+
"steps": [{...}, {...} ]
959+
}
960+
}
961+
```
962+
963+
You can now reference these in any job code:
964+
965+
```js
966+
fn(state => {
967+
state.id = toKebabCase(state.data.name);
968+
return state;
969+
});
970+
```
971+
972+
There are some important rules to bear in mind when using globals:
973+
974+
- You cannot import other modules inside globals. Globals are not a replacement
975+
for adaptors.
976+
- Exported functions are not operations, and cannot be used at the top-level of
977+
a job expression (unless you specifically write it that way, see below)
978+
- The contents of globals are immutable: you cannot change their values and they
979+
are reset in-between steps. You can of course pass state into a global
980+
function and mutate it.
981+
982+
:::info Global functions are not Operations
983+
984+
A really important (but quite complicated) rule of globals is that any function
985+
you export is not necessarily an Operation.
986+
987+
An Operation is a function that returns a function that takes state and returns
988+
state. Operations are provided by adaptor functions and can only be used at the
989+
top level of your code.
990+
991+
But a function exported from globals is just a function. If you try and use it a
992+
the top level of job, you'll get an error like
993+
`TypeError: fn is not a function`.
994+
995+
For example:
996+
997+
```js
998+
fn(state => {
999+
// a valid fn block
1000+
return state;
1001+
});
1002+
toKebabCase(state.name); // TypeError: fn is not a function!
1003+
```
1004+
1005+
You can define your own operations by structuring your global functions
1006+
correctly. Remember that your function must return a function, and that this
1007+
inner function must take and return state. Your operation will probably want to
1008+
write to state too (by convention we write to state.data but you can do whatever
1009+
you like).
1010+
1011+
An operation variation of `toKebabCase` might look like this:
1012+
1013+
```js
1014+
// globals.js
1015+
export function toKebabCase(str) {
1016+
return state => {
1017+
state.result = str.replace(/\s/g, '-');
1018+
return state;
1019+
};
1020+
}
1021+
```
1022+
1023+
:::
1024+
9181025
## Using Cursors
9191026

9201027
Sometimes it is useful to maintain a rolling cursor position on the backend
@@ -1159,27 +1266,30 @@ fn(state => {
11591266

11601267
## Referencing credential secrets in your job code
11611268

1162-
If you want to reference any credential secrets in your job code, you can still map keys from your `state.configuration`. See example below that will dynamically map the username and password from your `configuration` (or "credential" if using the app) into your http request body.
1269+
If you want to reference any credential secrets in your job code, you can still
1270+
map keys from your `state.configuration`. See example below that will
1271+
dynamically map the username and password from your `configuration` (or
1272+
"credential" if using the app) into your http request body.
11631273

11641274
```js
11651275
post('/api/v1/auth/login', {
1166-
body: {
1276+
body: {
11671277
username: $.configuration.username, //map the UN from credential
1168-
password: $.configuration.password //map the PW from credential
1169-
},
1170-
headers: {'content-type': 'application/json'},
1171-
})
1278+
password: $.configuration.password, //map the PW from credential
1279+
},
1280+
headers: { 'content-type': 'application/json' },
1281+
});
11721282
```
11731283

11741284
:::info OpenFn scrubs Configuration & Functions from final state
11751285

11761286
OpenFn will automatically scrub the `configuration` key and any functions from
1177-
your final state, as well as from logs if running workflows on the app. This is to help ensure that your credential secrets are kept secure and won't be leaked into History.
1287+
your final state, as well as from logs if running workflows on the app. This is
1288+
to help ensure that your credential secrets are kept secure and won't be leaked
1289+
into History.
11781290

11791291
:::
11801292

1181-
1182-
11831293
<!--
11841294
I would like to include this BUT fields is not an operation and so works a bit differently
11851295

0 commit comments

Comments
 (0)