Database

Raw SQL and typed Kysely access through the context.

Chimpbase provides two ways to interact with your database: raw SQL via ctx.query() and a typed query builder via ctx.db().

Raw SQL

const users = await ctx.query<{ id: number; email: string }>(
  "select id, email from users where active = ?1",
  [true],
);

Parameters use positional placeholders (?1, ?2, etc.) that work across PostgreSQL and SQLite.

Typed Kysely Access

For complex queries, use the Kysely query builder with your database types:

interface Database {
  users: { id: number; email: string; active: boolean };
  projects: { id: number; name: string; owner_id: number };
}

const users = await ctx.db<Database>()
  .selectFrom("users")
  .where("active", "=", true)
  .selectAll()
  .execute();

Transaction Behavior

Actions, subscriptions, and worker handlers run inside runtime-managed transactions. You do not need to (and cannot) use ctx.db().transaction() — this is intentionally unsupported to prevent nested transaction issues.

The runtime handles commit/rollback automatically. If your handler throws, the transaction is rolled back.