Introduction
If you understand how a ‘state’ object works and have built a cloud function, find out how your function can respond differently depending on the data passed into it.
Recommendation:
Add additional information into the state
object; more than just the information required by the function for cursoring.
Context:
Fivetran doesn't utilize the content of the state
object; only the function uses the state
object value. The state
object is a JSON string passed back and forth between the function and Fivetran. The state
object can contain data that will be useful for the function and it is possible to leverage the contents of the JSON string for purposes other than just storing cursors.
For example, let’s assume that a function has two non-default code paths: a and b.
- On the initial run of a function, the
secrets
object will be passed to the function along with an emptystate
. This will invoke the default code path: - {“secrets”:{“apiKey”:1,”apiSecret”:2},”state”:{ }}
- If the source API your function interacts with uses an incremental integer as the cursor, you will pass that back to Fivetran in the
state
object: - {"state":”{“id”:10}}
- However, depending on the response from the source API you might also want the function to call
codepath a
orcodepath b
on the next run. You can append the codepath value to the response: - {"state":”{“id”:10, “codepath”:”a”}}
- On the next run, this will be passed into the function as part of that initial invocation from Fivetran:
- {"state":”{“id”:10, “codepath”:”a”}}
The function would then read that value and run codepath a
.
- If the response from the source API then indicates
codepath b
should be called on the next run, then the state will be updated and passed back to Fivetran: - {"state":”{“id”: n , “codepath”:”b”}}
- When passed back into the function, this would then invoke
codepath b
.
In summary, you can leverage the index as the cursor for the requests and also utilize the codepath
parameter to determine which path the function should execute.
Considerations:
The logic for what data is passed to Fivetran in the state
object and how the function subsequently utilizes that data should be built into the function.