This app uses Val Town SQLite to manage data. Every Val Town account comes with a free SQLite database, hosted on Turso.
schema.ts
- Drizzle ORM schema definitions (tables, columns, types)migrations.ts
- Database table creation logicqueries.ts
- Query functions used by MCP toolsdb.ts
- Database connection configuration
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 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.
The queries.ts
file exports typed functions for database operations:
getMessages(limit?)
- Fetch messages with optional limitinsertMessage(content)
- Insert a new message
These functions are imported and used by MCP tools in backend/mcp/server.ts
.
SQLite has limited support for altering existing tables. When you need to modify a table schema:
- Create a new table with the updated schema
- Copy data from the old table to the new table
- Drop the old table
- 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.