Data Model
TrailBase’s data model is built on SQLite with automatic API generation, type safety through JSON schemas, and support for relationships and foreign key expansion.Database Schema
SQLite Foundation
TrailBase uses SQLite as its primary database engine with several enhancements:- STRICT Tables: Enforced type checking for data integrity
- Foreign Keys: Enabled by default for referential integrity
- WAL Mode: Write-Ahead Logging for better concurrency
- Custom Extensions: JSON validation, UUID generation, and more
All schema changes are tracked through migrations stored in
traildepot/migrations/.Built-in Tables
TrailBase automatically creates system tables during initialization:_user Table
User Model
The
DbUser struct represents database records from the _user table.Other System Tables
_refresh_token: Stores refresh tokens for authentication_session: Tracks user sessions_chat: Stores chat messages (if enabled)_file_deletions: Queues file cleanup operations_http_logs: Request/response logging
Schema Metadata
Connection Metadata
TrailBase parses SQLite schema into structured metadata for API generation.
ConnectionMetadata struct provides a complete representation of your database schema:
Column Types
TrailBase understands SQLite’s type affinity system:Column Metadata
Each column is analyzed for:- Data Type: SQLite affinity and explicit type
- Nullability: Whether NULL values are allowed
- Primary Key: Is this the record identifier?
- Foreign Keys: References to other tables
- File Columns: Special handling for file uploads
- JSON Schema: Custom validation rules
Record APIs
Record API System
Record APIs provide type-safe, auto-generated REST endpoints for tables and views.
What is a Record API?
A Record API exposes a SQLite table or view through REST endpoints with automatic:- Type validation
- Access control
- JSON serialization
- Foreign key expansion
- Real-time subscriptions
Configuration
Record APIs are configured inconfig.textproto:
Record API Structure
JSON Schema Generation
TrailBase automatically generates JSON schemas for validation and TypeScript type generation.
Schema Modes
JSON schemas are generated with different strictness levels:Generated Schema Example
For a table like:Custom JSON Schemas
You can define custom validation schemas:Relationships
Foreign Keys
TrailBase automatically detects and handles foreign key relationships:Foreign Key Expansion
Expansion Feature
Foreign key expansion allows nested loading of related records.
?expand=post_id,user_id:
File Handling
File Columns
TrailBase provides special handling for file upload columns.
File Column Detection
Columns with JSON schema containingcontentMediaType and contentEncoding are treated as file columns:
File Storage
Files are stored in:- Local:
traildepot/uploads/directory - S3: Configured S3-compatible object storage
Automatic Cleanup
TrailBase installs triggers to clean up files when records are updated or deleted:Views and Read-Only APIs
Views can be exposed through Record APIs for read-only access:Multi-Database Support
TrailBase supports attaching multiple SQLite databases:ConnectionManager handles database attachment and maintains separate metadata per connection.
Best Practices
Use STRICT Tables
Always create tables with STRICT mode for type safety
Define Primary Keys
Use INTEGER PRIMARY KEY or BLOB PRIMARY KEY with UUIDv7
Enable Foreign Keys
Use REFERENCES for relationships and ON DELETE CASCADE
Index Frequently Queried Columns
Create indexes for foreign keys and filter columns
Next Steps
APIs
Learn how to use Record APIs in your application
Authentication
Understand user management and access control