Code
This component executes plain JavaScript code within a sandbox making it secure and safe. In regards the following limitations apply:
- Access to the console
- Eval function
Calling done
Because JavaScript allows you to write code asynchronously, you need to call the done
function when your code is done. When you are not using any asynchronous syntax, the done
call should be at the end of your script.
// your code
done();
In cases where you are calling return in the global scope, you need to call done
before the return
statement.
Configuration
Use Content Store / Enable lookup function
Checking this will allow you to use the mapping function lookup
and work with mapping values previously loaded into the Content Store using the Load Mapping component.
Connection string
A MongoDB connection string.
Example: mongodb://<username>:<password>@localhost:27017/<databaseName>
Here <databaseName>
is the database to store content.
Use TLS
Whether or not to use TLS in case your mongoDB requires TLS.
Allow Invalid Certificates
Checking this will disable certificate validation. Warning: specifying this option in a production environment makes your application insecure and potentially vulnerable to expired certificates and to foreign processes posing as valid client instances.
Certificate Authority File
One or more certificate authorities to trust when making a TLS connection. In order to access the local filesystem, the XILL4_WORKDIRS
environment variable must be set to the path of the directory to be accessed.
Example: .\ca.pem
Functions
Use send(value: any);
to send data to the output. This function can be executed multiple times.
Use error(message: string, data: any);
to execute the error handling behavior. data
is the data that caused the error and will be displayed in the log details.
Use setVariable(key: string, value: any);
to set a global variable that can be used in any code sandbox within the same flow.
Use getVariable(key: string);
to get a global variable.
Use deleteVariable(key: string);
to delete a global variable.
Use getFlowVariable(key: string, parseAs: string);
Options: 'boolean', 'array', 'number', 'object', 'date', 'string' to get a flow variable. By default parseAs
is a set to string
.
Note that the variable syntax %variableName%
is not supported in the code component and that flow variables are read-only.
Use parseAs(value: string, parseAs: string)
to parse a value as something else. Options: 'boolean', 'array', 'number', 'object', 'date', 'string'. By default parseAs
is a set to string
.
Use createFunction(functionName: string, function: function);
to create a custom function that can be used in any code component within the same flow.
Use runFunction(functionName: string)(parameters);
to run a custom function.
When using runFunction
, it executes the custom function within the code component where it was defined. Caution should be taken when connecting multiple code components, as this can potentially lead to an infinite loop. For instance, if you have two code components, where code component 1 has a custom function created with createFunction()
, and it sends a message using send()
, and code component 2 triggers that function with runFunction
upon receiving a message, it may cause code component 1 to send another message to code component 2, resulting in the loop repeating indefinitely. Please be mindful of this when designing your code to avoid unintended consequences.
Use deleteFunction(functionName: string);
to delete a custom function.
Use log(type: string, message: any, data: any);
to write a log record. Type options: 'info', 'error', 'warn', 'debug'
Use forEachAsync(arr: array, onItem: function, onFinish: function);
to asynchronously loop over each element in an array. onItem
receives the array element that was selected during that iteration of the Node.js event loop as its first parameter, and a next
function as its second parameter. onFinish
is called when forEachAsync
reached the end of the array, and receives a boolean indicating if it was cancelled or finished completely.
Use getProperty(obj: object, key: string);
to get a property from an object. You can use dot notation to get nested properties.
Use setProperty(obj: object, key: string, value: any);
to set a property on an object. You can use dot notation to set nested properties.
Basic example
const array = [1, 2, 3, 4, 5]; // array of objects we want to perform complex operations on
let array2 = [];
forEachAsync(
array,
(elem, next) => {
array2.push(elem * 2);
next();
},
() => {
log("info", array2); // [2, 4, 6, 8, 10]
done();
}
);
Example of cancelling the execution halfway through the array
const array = [1, 2, 3, 4, 5]; // array of objects we want to perform complex operations on
let array2 = [];
forEachAsync(
array,
(elem, next) => {
if (elem === 3) {
next("cancel");
} else {
array2.push(elem * 2);
next();
}
},
(cancelled) => {
log("info", cancelled); // true
log("info", array2); // [2, 4]
done();
}
);
Use clone(value: object);
to clone an object.
Use md5(value: string);
to get a md5 hash of the value.
Use sha1(value: string);
to get a sha1 hash of the value.
Use sha256(value: string);
to get a sha256 hash of the value.
Use sha512(value: string);
to get a sha512 hash of the value.
Use await lookup(mappingName: string, lookupValues: array);
to lookup mapping values previously loaded into the Content Store using the Load Mapping component.
mappingName
is the name of the mapping in which to lookup the values. The mapping name refers to the mapping name under which the mapping is stored in the Content Store. It is the combined mapping name, which consists of the workbookName
without the extension and the sheetName
divided by a -
. Note that all names in the mapping name are lowercased. For more information on the mapping name, see the Load Mapping component
lookupValues
is an array of values that are used to lookup the mapping. The order of the values in the array should correspond to the order of the keys in the mapping. For example, if the mapping has two keys, name
and lastName
, the lookupValues array should contain two values, the first value being the value of the name
key, and the second value being the value of the lastName
key.
Example mapping Excel sheet:
*name | *lastName | street | number | city |
---|---|---|---|---|
Mark | Mike | Main Street | 12 | Los Angeles |
Morgan | Freeman | First Avenue | 24 | Washington DC |
Example of using lookup:
// Using lookup in code component
async function main() {
const person = await lookup("mappingexample-sheet1", ["Morgan","Freeman"]);
send(person);
}
main()
.then(() => {
done();
})
.catch((e) => done(e));
Use await getMappingKeys(mappingName: string);
to get the keys of a mapping.
Use await lookupByKey(lookupKey: string);
to lookup mapping values by their key.
Example of using getMappingKeys and lookupByKey:
async function main() {
const keys = await getMappingKeys("mappingexample-sheet1");
for await(const key of keys) {
const person = await lookupByKey(key);
}
}
main()
.then(() => {
done();
})
.catch((e) => done(e));
Use escapeRegexSpecialChars(value: string)
to escape special characters in the given value
string using a built-in regex pattern, ensuring safe use in regular expressions.
The following modules are available within the code component:
- Node.js Path module, use
Path
- Node.js URL module, use
URL
- Luxon DateTime, use
DateTime
- Luxon Interval, use
Interval
- Luxon Duration, use
Duration
For documentation on the modules, please use the manufactures official documentation.
Variables
value
Contains the received data.
Inputs
-
Name: Input
- Description: Any data.
Outputs
-
Name: Output
- Description: Any data.