Back to Rivet

SQLite Raw Example

examples/sqlite-raw/README.md

2.3.21.9 KB
Original Source

SQLite Raw Example

This example demonstrates using the raw SQLite driver with RivetKit actors.

Getting Started

sh
git clone https://github.com/rivet-dev/rivet.git
cd rivet/examples/sqlite-raw
npm install
npm run dev

Features

  • Raw SQLite API: Direct SQL access using @rivetkit/db/raw
  • Migrations on Wake: Uses onMigrate to create tables on actor wake
  • Todo List: Simple CRUD operations with raw SQL queries

Running the Example

bash
pnpm install
pnpm dev

Large Insert Benchmark

To benchmark a large payload insert against a local RivetKit actor and compare it to native SQLite on disk:

bash
pnpm bench:large-insert

Environment variables:

  • BENCH_MB: Total payload size in MiB. Defaults to 10.
  • BENCH_ROWS: Number of rows to split the payload across. Defaults to 1.
  • RIVET_ENDPOINT: Engine endpoint. Defaults to http://127.0.0.1:6420.

The benchmark prints:

  • Actor-side SQLite insert time
  • End-to-end action latency
  • Native SQLite baseline latency
  • Relative slowdown versus native SQLite

Usage

The example creates a todoList actor with the following actions:

  • addTodo(title: string) - Add a new todo
  • getTodos() - Get all todos
  • toggleTodo(id: number) - Toggle todo completion status
  • deleteTodo(id: number) - Delete a todo

Code Structure

  • src/index.ts - Actor definition, migrations, and registry startup
  • scripts/client.ts - Simple todo client
  • scripts/bench-large-insert.ts - Large-payload benchmark runner

Database

The database uses the KV-backed SQLite VFS, which stores data in a key-value store. The schema is created using raw SQL in the onMigrate hook:

typescript
db: db({
  onMigrate: async (db) => {
    await db.execute(`
      CREATE TABLE IF NOT EXISTS todos (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT NOT NULL,
        completed INTEGER DEFAULT 0,
        created_at INTEGER NOT NULL
      )
    `);
  },
})

License

MIT