How Can I Use the state Object?
Use Case
You have read the documentation on our cloud functions but don’t quite understand how best to utilize the state object. You are looking for an example of how to use this object in your function's response.
Recommendations
Fivetran recommends the following:
Ensure your function is always expecting an empty
stateobject on the first invocation of the function, and can handle scenarios where thestateis null.Ensure your function iterates the value returned from the
stateobject on every subsequent invocation of the function, where thestateobject is not null.
For more information, see the following example request and response format:
Request
if (req.body.state.value = null) {
hostname = endpoint.com
Path = /giveMeEverything
}
else {
hostname = endpoint.com
Path = /value
}
Response
state: {
value: value == null ? req.state.value : value
}
Context
The state object is what your function uses to maintain cursoring.
For example,
On the initial run of a function, the
stateis set as an empty string:{}.If your cursor is keyed on a date and the last date you received in the response from the source system is 01/01/21, then you must pass
{"state":01/01/21}to Fivetran.On the next run, the
stateis passed into the function as part of that initial invocation from Fivetran:{"state":01/01/21}.If you want to sync your data from 02/01/21, you must add the following logic in your function:
{"state":(01/01/21 + 1)}.
Considerations
state is a JSON object that contains cursors from the previous successful function execution. Fivetran requires a JSON string to be passed in the state response.