JavaScript, Lua and cloud functions

Deploy JavaScript functions that run next to your app's data, and run Lua scripts over its Redis-compatible keys.

Docs menu

The JS function runtime is documented in index.md; the typed description of every global is at /docs/runtime/api/ (served live at GET /runtime.d.ts, over MCP as the resource substrio://runtime/types).

What a function is

One JavaScript file per function. The only imports are the builtin modules (substrio:env and the Node subset, see the runtime reference); bundle any other dependency into the file. It runs on JavaScriptCore in a per-app isolate, with db, substrio, env, fetch and the web globals in scope. Three forms are recognised by the source:

export default { async fetch(request, env, ctx) { return Response.json({ ok: true }); } };   // ES module
module.exports = async function (event) { return { ok: true }; };                             // CommonJS
(function (event) { return { ok: true }; })                                                   // script

It is called by an HTTP route, function.invoke, a cron schedule or a document trigger (index.md, "Handler shapes").

Where each topic lives

Topic Page
Forms, handler shapes, limits and their errors, differences from Workers, globals index.md
Key-value stores: db.kv kv.md
db.sql, db.prepare, db.transaction, SQL over documents sql.md
Documents, filters and the chain collections-and-chains.md
Live records and query results records.md
substrio.* services services.md
Durable objects durable-objects.md
Failures, console, reading logs errors-and-logs.md
Async work and concurrency concurrency.md

Deploying, invoking and reading source revisions go through the function API: cloud-functions.md and agent function tooling.

Lua scripts

A Lua script runs against the app's default key-value store, Redis style, with keys and args as arrays of strings. Call it over the API with function.eval (load: true stores the script and answers its sha, which later calls send instead of the text), or from a function:

substrio.lua.eval("return redis.call('GET', KEYS[1])", ["greeting"]);

Lua runs under the same key, quota and deadline checks as every other call.

Boundaries

$whereJS is a query filter that runs JavaScript on each candidate document after the rest of the filter. A fetch call or another function call is not part of the database transaction: it does not commit or roll back with a record mutation.