Database

This app uses Val Town SQLite to manage data. Every Val Town account comes with a free SQLite database, hosted on Turso.

Structure

  • schema.ts - Drizzle ORM schema definitions (tables, columns, types)
  • migrations.ts - Database table creation logic
  • queries.ts - Query functions used by MCP tools
  • db.ts - Database connection configuration

Tables

The starter includes a messages table for storing message board data:

export const messages = sqliteTable("messages", { id: integer("id").primaryKey({ autoIncrement: true }), content: text("content").notNull(), timestamp: text("timestamp").notNull().default(new Date().toISOString()), });

Migrations

Migrations run automatically on startup via createTables() in migrations.ts, which is imported in queries.ts.

The migration logic uses CREATE TABLE IF NOT EXISTS, so it's safe to run on every startup. You can comment out the import in queries.ts for a slight (30ms) performance improvement on cold starts, but it's left in by default so forks work out of the box.

Queries

The queries.ts file exports typed functions for database operations:

  • getMessages(limit?) - Fetch messages with optional limit
  • insertMessage(content) - Insert a new message

These functions are imported and used by MCP tools in backend/mcp/server.ts.

Schema Changes

SQLite has limited support for altering existing tables. When you need to modify a table schema:

  1. Create a new table with the updated schema
  2. Copy data from the old table to the new table
  3. Drop the old table
  4. Rename the new table

Alternatively, you can just change the table name (e.g., messages_v2) to start fresh.

For help with database operations, reach out in the Val Town Discord.