custom.javascript
Run custom JavaScript code.
Description
A processor that makes it possible to process Conduit records using JavaScript.
The following helper functions and variables are available:
logger
: a logger that outputs to Conduit's logs. Check out Zerolog's API on how to use it.Record()
: constructs a new record which represents a successful processing result. It's analogous tosdk.SingleRecord
from Conduit's Go processor SDK.RawData()
: creates a raw data object. It's analogous toopencdc.RawData
. Optionally, it accepts a string argument, which will be cast into a byte array, for example:record.Key = RawData("new key")
.StructuredData()
: creates a structured data (map-like) object.
To find out what's possible with the JS processor, also refer to the documentation for goja, which is the JavaScript engine we use.
Configuration parameters
Name | Type | Default | Description |
---|---|---|---|
script | string | null | JavaScript code for this processor.
It needs to have a function |
script.path | string | null | The path to a .js file containing the processor code. |
Examples
Modify a record's metadata and payload using JavaScript
In this example we use the custom.javascript
processor to add a metadata key to the input record. It also prepends "hello, " to .Payload.After
.
Configuration parameters
Name | Value |
---|---|
script |
|
script.path | null |
Record difference
Before | After | ||||
1 | { | 1 | { | ||
2 | "position": null, | 2 | "position": null, | ||
3 | "operation": "Operation(0)", | 3 | "operation": "Operation(0)", | ||
4 | "metadata": { | 4 | "metadata": { | ||
5 | - | "existing-key": "existing-value" | 5 | + | "existing-key": "existing-value", |
6 | + | "processed": "true" | |||
6 | }, | 7 | }, | ||
7 | "key": null, | 8 | "key": null, | ||
8 | "payload": { | 9 | "payload": { | ||
9 | "before": null, | 10 | "before": null, | ||
10 | - | "after": "world" | 11 | + | "after": "hello, world" |
11 | } | 12 | } | ||
12 | } | 13 | } |