Skip to content
Documentation
On this page

App storage and SQLite

Give an Executor app a SQLite database with SQL files in migrations/ and read or write rows synchronously with ctx.sql.

An Executor app owns a private SQLite database as soon as it includes one or more .sql files in migrations/. Deploy applies pending migrations in number order before activating the new version, and handlers read and write rows synchronously through ctx.sql.exec and ctx.sql.transaction. App SQLite storage works on Local, Self-host, and Cloud.

The step that matters most: add migrations/0001_init.sql with your CREATE TABLE statements and deploy—there is nothing to declare in defineApp requirements.

Prerequisites and permissions: an app you can edit and deploy (Local owner, or on Cloud and Self-host, the app’s creator or an organization admin or owner; see Build an app).

You’ll know it worked when… executor apps deploy succeeds without migration errors, calling a mutation inserts a row with ctx.sql.exec, and a subsequent query returns that row.

How to use it

Migration files and ctx.sql behave identically on Local, Self-host, and Cloud.

1. Add numbered migrations in migrations/

Place .sql files directly inside migrations/ named <number>_<name>.sql, such as migrations/0001_create_notes.sql and migrations/0002_add_pinned.sql.

  • Executor applies pending migrations in numeric order inside one transaction during executor apps deploy, before activating the new version.
  • If a migration fails, nothing is applied and the previous version stays active.
  • Already-applied migrations are recorded by order, filename, and content hash in _executor_migrations. Never edit or delete an applied migration file; add a new numbered file for schema changes.

2. Read in queries and write in mutations

ctx.sql.exec<Row>(sql, ...bindings) runs synchronously (do not await it) and returns a cursor with .toArray(), .one() (throws unless exactly one row returned), .columnNames, .rowsRead, and .rowsWritten.

  • Queries receive a read-only SqlReader. Every statement runs inside a transaction that always rolls back; a statement that modifies rows fails.
  • Mutations receive a writable Sql. Outside ctx.sql.transaction(...), each statement commits on its own.
  • Transactions: ctx.sql.transaction((tx) => ...) runs a synchronous callback in one SQLite transaction and rolls back if it throws. Returning a Promise or using await inside transaction is refused so no transaction stays open while waiting on network I/O.

3. Combine SQLite with external API calls

Never hold a transaction open across fetch. Instead, use reserve-call-publish:

  1. Write a "pending" row in one ctx.sql.transaction(...).
  2. Call the external API with ctx.fetch outside the transaction.
  3. Update the row conditionally (UPDATE ... WHERE id = ? AND status = 'pending') and check rowsWritten to handle concurrent calls or failures.

For workflow step mutations invoked with step.runMutation, perform all writes inside a single ctx.sql.transaction(...) so Executor records the step receipt atomically with your data writes.

Example

CREATE TABLE notes (
  id TEXT PRIMARY KEY NOT NULL,
  text TEXT NOT NULL,
  created_at INTEGER NOT NULL
);
import { array, defineApp, mutation, number, object, query, router, string } from "apps";

const noteSchema = object({
  id: string(),
  text: string(),
  createdAt: number(),
});

const listNotes = query(
  {
    description: "List recent notes",
    input: object({}),
    output: array(noteSchema),
  },
  async ({ sql }) =>
    sql
      .exec<{ id: string; text: string; createdAt: number }>(
        "SELECT id, text, created_at AS createdAt FROM notes ORDER BY created_at DESC LIMIT 50",
      )
      .toArray(),
);

const addNote = mutation(
  {
    description: "Save a note",
    input: object({ text: string() }),
    output: noteSchema,
  },
  async ({ sql }, { text }) =>
    sql.transaction((tx) =>
      tx
        .exec<{ id: string; text: string; createdAt: number }>(
          "INSERT INTO notes (id, text, created_at) VALUES (?, ?, ?) RETURNING id, text, created_at AS createdAt",
          crypto.randomUUID(),
          text,
          Date.now(),
        )
        .one(),
    ),
);

export default defineApp({}, { tools: router({ listNotes, addNote }) });

Limits

  • Immutable migration history: removing migrations/ or modifying an applied migration file fails the deploy and leaves the previous version active.
  • Bound parameter limit: one statement can bind at most 100 values (?). For larger lists, pass one JSON array string and expand it with json_each(?), or split into batches.
  • SQLite size limits: a single statement is at most 100 KB; a string, BLOB, or row is at most 2 MB; a table has at most 100 columns. Each migration file runs as one SQL string, so keep each migration file under 100 KB (split larger schemas across numbered files).
  • Reserved tables and PRAGMAs: do not create or modify tables prefixed with _executor_ (_executor_migrations, _executor_step_receipts, _executor_step_events, _executor_legacy_rows). Tool calls cannot change connection PRAGMA settings (foreign_keys, defer_foreign_keys, ignore_check_constraints, legacy_alter_table, recursive_triggers, reverse_unordered_selects, case_sensitive_like); migrations may change them while running.
  • Shared app data and rollback: every caller of an app shares the app’s single SQLite database. Rolling back to an older app version changes code only and does not rewind database rows. Make a copy creates an empty database and does not copy rows.

Was this page helpful?