Skip to content

Context Overview

Every handler in Chimpbase — actions, subscriptions, workers, cron jobs, and workflow steps — receives a ChimpbaseContext as its first argument. The context provides access to all runtime capabilities.

Context API

CategoryAPIPurpose
Databasectx.db.query(sql, params)Raw SQL queries
ctx.db.kysely<T>()Typed Kysely query builder
Pub/Subctx.pubsub.publish(event, payload)Publish ephemeral events
Queuesctx.queue.enqueue(name, payload, opts?)Enqueue durable background jobs
Key-Valuectx.kv.get/set/delete/listKey-value storage
Collectionsctx.collection.find/insert/update/deleteSchemaless JSON documents
Streamsctx.stream.append/readAppend-only event streams
Workflowsctx.workflow.start/get/signalWorkflow management
Actionsctx.action(name, args)Call other registered actions
Secretsctx.secret(name)Access preloaded secrets
Loggingctx.log.debug/info/warn/errorStructured logging
Metricsctx.metric(name, value, labels)Record metrics
Tracingctx.trace(name, callback, attrs?)Distributed tracing spans

Usage

Every primitive handler receives the context as the first argument:

ts
import { action } from "@chimpbase/runtime";

const createOrder = action("createOrder", async (ctx, input) => {
  // Database
  const [order] = await ctx.db.query<{ id: number }>(
    "insert into orders (total) values (?1) returning id",
    [input.total],
  );

  // Pub/Sub
  ctx.pubsub.publish("order.created", { orderId: order.id });

  // Queue
  await ctx.queue.enqueue("order.fulfill", { orderId: order.id });

  // Logging
  ctx.log.info("order created", { orderId: order.id });

  return order;
});

See the individual primitive pages for detailed API documentation: