On this page
Scripting
Scripting in CoCoCo
Scripts add your own logic to CoCoCo: answer a webhook, prepare a report's data, transform a pipeline's rows, reach a device from a workflow. They are written in SwiftLua, a small and safe version of Lua. A script can reach only what CoCoCo gives it, and every run has time and memory limits, so a mistake in a script stays inside that script.
This section has three parts:
- The language. Getting started is a gentle
introduction for anyone new to Lua. The language and
Extra libraries are the full reference for the language and
its libraries, such as
json,timeanddecimal. - CoCoCo. Which profile a script runs in, where scripts run and how their data arrives, and how to call CoCoCo; then a complete script for each kind, in Workflows, webhooks and apps, Integrations and Pure scripts; and the limits every run has.
- The reference. Every
ctxfunction and object, with what it takes and returns.
Profiles
Every script runs in one of three profiles. All three have the same language and
libraries. They differ in how much of CoCoCo a script can reach, through the global
ctx.
| Profile | What it can reach |
|---|---|
| Normal | ctx.cache, ctx.config, ctx.dataContainer, ctx.device, ctx.edgeApp, ctx.graph, ctx.graphql, ctx.integration, ctx.jdf, ctx.jmf, ctx.knowledge, ctx.log, ctx.ml, ctx.notify, ctx.protobuf, ctx.script, ctx.sql, ctx.store, ctx.template, ctx.twin |
| Integration | ctx.api, ctx.cache, ctx.config, ctx.dataContainer, ctx.device, ctx.edgeApp, ctx.graph, ctx.graphql, ctx.integration, ctx.jdf, ctx.jmf, ctx.knowledge, ctx.log, ctx.ml, ctx.notify, ctx.protobuf, ctx.script, ctx.sql, ctx.store, ctx.template |
| Pure | No ctx. |
Pure scripts run in the places where speed matters most, such as once per pipeline
row, so they have no ctx and cannot load other scripts. Using ctx in one is an
error when the script is checked:
ctx in a pure script
-- A Pure script, such as a data pipeline stage, has no ctx.
local ok, result = ctx.graphql.query("{ __typename }", {})
return { ok = ok }Everything a Pure script needs comes from the language itself. The json library,
for example, works in every profile:
Encode a table as JSON
-- json is one of the language's libraries, available in every profile. It
-- doesn't wait on anything, so it returns one value, with no ok flag.
local encoded = json.encode({ method = "POST", retries = 3 })
-- decode is its inverse. Invalid JSON raises a catchable error rather than
-- returning nil, so pcall it if the input is untrusted.
local decoded = json.decode(encoded)
return { encoded = encoded, retries = decoded.retries }