common@3.2.2
- arrayToString(arr, separator)
- as(key, operation)
- asData(data, state)
- assert(expression, errorMessage)
- chunk(array, chunkSize)
- combine(operations)
- composeNextState(state, response)
- cursor(value, options)
- dataPath(path)
- dataValue(path)
- debug(args)
- each(dataSource, operation)
- field(key, value)
- fields(fields)
- fn(func)
- fnIf(condition, operation)
- group(arrayOfObjects, keyPath, callback)
- humanProper(str)
- index()
- join(targetPath, sourcePath, targetKey)
- jsonValue(obj, path)
- lastReferenceValue(path)
- lodash()
- log(args)
- map(path, callback)
- merge(dataSource, fields)
- parseCsv(csvData, [parsingOptions], [callback])
- referencePath(path)
- scrubEmojis(text, replacementChars)
- source(path)
- sourceValue(path)
- splitKeys(obj, keys)
- toArray(arg)
- validate(schema, data)
This adaptor exports the following namespaced functions:
- util.decode(base64Data, options)
- util.encode(data, options)
- util.uuid()
- beta.each(dataSource, operation)
- dateFns.format()
- dateFns.parse()
- http.get(url, options)
- http.options(opts)
- http.post(url, data, options)
- http.request(method, url, options)
Functions
arrayToString
arrayToString(arr, separator) ⇒ string
Turns an array into a string, separated by X.
| Param | Type | Description |
|---|---|---|
| arr | array | Array of toString'able primatives. |
| separator | string | Separator string. |
Example
field("destination_string__c", function(state) {
return arrayToString(dataValue("path_of_array")(state), ', ')
})
as
as(key, operation) ⇒ Operation
Run an operation and save the result to a custom key in state instead of overwriting state.data.
| Param | Type | Description |
|---|---|---|
| key | string | The state key to assign the result of the operation to. |
| operation | function | An operation that returns a new state object with a data property |
Example: Fetch cce-data from collections and store them under state.cceData
as('cceData', collections.get('cce-data-dhis2', { key: `*:*:${$.syncedAt}*` }));
asData
asData(data, state) ⇒ array
Simple switcher allowing other expressions to use either a JSONPath or object literals as a data source.
- JSONPath referencing a point in
state - Object Literal of the data itself.
- Function to be called with state.
| Param | Type | Description |
|---|---|---|
| data | String | object | function | |
| state | object | The current state. |
Example
asData('$.key'| key | callback)
assert
assert(expression, errorMessage) ⇒ operation
Asserts the given expression or function resolves to true, or else throws an exception. Optionally accepts and error message.
| Param | Type | Description |
|---|---|---|
| expression | any | The expression or function to be evaluated. |
| errorMessage | string | The error message thrown in case of a failed state. |
Example
assert('a' === 'b', '"a" is not equal to "b"')
chunk
chunk(array, chunkSize) ⇒ Object
Chunks an array into an array of arrays, each with no more than a certain size.
| Param | Type | Description |
|---|---|---|
| array | Object | Array to be chunked |
| chunkSize | Integer | The maxiumum size of each chunks |
Example
chunk([1,2,3,4,5], 2)
combine
combine(operations) ⇒ Operation
Combines two operations into one
| Param | Type | Description |
|---|---|---|
| operations | Operations | Operations to be performed. |
Example
combine(
create('foo'),
delete('bar')
)
composeNextState
composeNextState(state, response) ⇒ State
Prepares next state
| Param | Type | Description |
|---|---|---|
| state | State | state |
| response | Object | Response to be added |
Example
composeNextState(state, response)
cursor
cursor(value, options) ⇒ Operation
Sets a cursor property on state.
Supports natural language dates like now, today, yesterday, n hours ago, n days ago, and start,
which will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones
are not yet supported.
You can provide a formatter to customise the final cursor value, which is useful for normalising
different inputs. The custom formatter runs after natural language date conversion.
See the usage guide at https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors
| Param | Type | Description |
|---|---|---|
| value | any | the cursor value. Usually an ISO date, natural language date, or page number |
| options | object | options to control the cursor. |
| options.key | string | set the cursor key. Will persist through the whole run. |
| options.defaultValue | any | the value to use if value is falsy |
| options.format | function | custom formatter for the final cursor value |
Example: Use a cursor from state if present, or else use the default value
cursor($.cursor, { defaultValue: 'today' })
Example: Use a pagination cursor
cursor(22)
dataPath
dataPath(path) ⇒ string
Ensures a path points at the data.
| Param | Type | Description |
|---|---|---|
| path | string | JSONPath referencing a point in data. |
Example
dataPath('key')
dataValue
dataValue(path) ⇒ Operation
Picks out a single value from the source data object—usually state.data.
If a JSONPath returns more than one value for the reference, the first
item will be returned.
| Param | Type | Description |
|---|---|---|
| path | String | JSONPath referencing a point in data. |
Example
dataValue('key')
debug
debug(args) ⇒ Operation
Outputs a message to the console with the debug log level. This is usually filtered out by default. Use this at the top level of your job code, but not inside callbacks.
| Param | Type | Description |
|---|---|---|
| args | any | A value or message to display in the logs |
Example: Log values from state
debug('Patient List::', $.patients);
Example: Use console.debug inside a callback or fn block
fn((state) => {
console.debug(state.data);
return state;
})
each
each(dataSource, operation) ⇒ Operation
Iterates over an array of items and invokes an operation upon each one, where the state object is scoped so that state.data is the item under iteration. The rest of the state object is untouched and can be referenced as usual. You can pass an array directly, or use lazy state or a JSONPath string to reference a slice of state.
| Param | Type | Description |
|---|---|---|
| dataSource | DataSource | JSONPath referencing a point in state. |
| operation | Operation | The operation needed to be repeated. |
Example: Using lazy state ($) to iterate over items in state.data and pass each into an "insert" operation
each(
$.data,
// Inside the callback operation, `$.data` is scoped to the item under iteration
insert("patient", {
patient_name: $.data.properties.case_name,
patient_id: $.data.case_id,
})
);
Example: Iterate over items in state.data and pass each one into an "insert" operation
each(
$.data,
insert("patient", (state) => ({
patient_id: state.data.case_id,
...state.data
}))
);
Example: Using JSON path to iterate over items in state.data and pass each one into an "insert" operation
each(
"$.data[*]",
insert("patient", (state) => ({
patient_name: state.data.properties.case_name,
patient_id: state.data.case_id,
}))
);
field
field(key, value) ⇒ Field
Returns a key, value pair in an array.
| Param | Type | Description |
|---|---|---|
| key | string | Name of the field |
| value | Value | The value itself or a sourceable operation. |
Example
field('destination_field_name__c', 'value')
fields
fields(fields) ⇒ Object
Zips key value pairs into an object.
| Param | Type | Description |
|---|---|---|
| fields | Fields | a list of fields |
Example
fields(list_of_fields)
fn
fn(func) ⇒ Operation
Creates a custom step (or operation) for more flexible job writing.
| Param | Type | Description |
|---|---|---|
| func | function | is the function |
Example
fn(state => {
// do some things to state
return state;
});
fnIf
fnIf(condition, operation) ⇒ Operation
Execute a function only when the condition returns true
| Param | Type | Description |
|---|---|---|
| condition | Boolean | The condition that returns true |
| operation | Operation | The operation needed to be executed. |
Example
fnIf((state) => state?.data?.name, get("https://example.com"));
group
group(arrayOfObjects, keyPath, callback) ⇒ Operation
Groups an array of objects by a specified key path.
| Param | Type | Description |
|---|---|---|
| arrayOfObjects | Array.<Object> | The array of objects to be grouped. |
| keyPath | string | The key path to group by. |
| callback | function | (Optional) Callback function |
Example
const users = [
{ name: 'Alice', age: 25, city: 'New York' },
{ name: 'Bob', age: 30, city: 'San Francisco' },
{ name: 'Charlie', age: 25, city: 'New York' },
{ name: 'David', age: 30, city: 'San Francisco' }
];
group(users, 'city');
// state is { data: { 'New York': [/Alice, Charlie/], 'San Francisco': [ /Bob, David / ] }