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:

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

LUA
-- 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

LUA
-- 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 }