Skip to main content
This guide walks you through building a complete todo list application with TrailBase, covering database setup, type-safe APIs, and realtime updates.

What You’ll Build

A fully functional todo app with:
  • Create, read, update, and delete operations
  • Realtime synchronization across tabs and devices
  • Type-safe client code
  • Authentication (optional)

Prerequisites

  • TrailBase installed (installation guide)
  • Node.js and npm/pnpm (for the frontend)
  • Basic knowledge of TypeScript/JavaScript

Step 1: Initialize Your Project

Create a new directory and initialize TrailBase:
TrailBase will create a traildepot/ directory containing:
  • data/ - SQLite database files
  • migrations/ - Database migration files
  • config.textproto - API and auth configuration
  • secrets/ - Secure credentials
On first run, TrailBase displays admin credentials in the terminal. Save these to access the admin dashboard at http://localhost:4000/_/admin.

Step 2: Create the Database Schema

Create a migration file for the todos table:
This creates a file like traildepot/migrations/main/U1234567890__create_table_todos.sql. Edit it:
traildepot/migrations/main/U1234567890__create_table_todos.sql
Key Requirements:
  • Tables must use STRICT typing
  • Primary key must be INTEGER or BLOB (for UUIDv7)
  • Column names should be quoted to avoid SQL keyword conflicts
Restart TrailBase to apply the migration:
You’ll see a log message confirming the migration was applied.

Step 3: Configure the Record API

Edit traildepot/config.textproto to expose the todos table as an API:
traildepot/config.textproto
acl_world grants public access. For production, use acl_authenticated and implement proper access controls.
Restart TrailBase to load the configuration.

Step 4: Generate Type Definitions

Generate TypeScript types from your schema:
Install the TypeScript client and code generation tools:
Generate TypeScript types:
Your generated Todo type will include:
src/types/todo.ts

Step 5: Build the Frontend

Create a React component using the TrailBase client:
src/App.tsx

Step 6: Add Realtime Updates

Subscribe to live changes to sync todos across tabs:
src/App.tsx
For production apps, use a debounce mechanism to avoid excessive reloads when handling rapid updates.

Step 7: Test Your App

Start your development server:
Open multiple browser tabs to http://localhost:5173 and test:
  1. Add a todo in one tab
  2. Watch it appear in other tabs instantly
  3. Toggle completion status
  4. Delete todos

Step 8: Add Authentication (Optional)

Update your API configuration to require authentication:
traildepot/config.textproto
Add a user column to track todo ownership:
Update the API to auto-fill the user column:
traildepot/config.textproto
Add authentication to your frontend:
src/App.tsx

Next Steps

Database Setup

Learn advanced schema design patterns

Authentication

Implement OAuth and password auth

Realtime Subscriptions

Master WebSocket subscriptions

File Uploads

Handle file uploads and storage

Complete Example

The TanStack Todo Example in the TrailBase repository demonstrates automatic cross-device synchronization using TanStack/db.