Turso Database Development
Overview
Turso is a distributed SQLite database service that provides global replication and edge performance. It’s built on libSQL, an open-source fork of SQLite that adds additional features while maintaining full SQLite compatibility. DeployStack integrates with Turso using the official@libsql/client
driver through Drizzle ORM, providing excellent performance and developer experience.
Key Features
- Global Replication: Automatic multi-region database replication
- Edge Performance: Low-latency access from anywhere in the world
- SQLite Compatibility: Full compatibility with SQLite syntax and features
- Scalability: Automatic scaling based on usage patterns
- libSQL Protocol: Enhanced SQLite with additional networking capabilities
Migration Compatibility: Turso requires SQL statements to be executed individually rather than in batches. DeployStack automatically handles this requirement by intelligently splitting migration files into individual statements during execution. You don’t need to modify your migrations or write them differently - we handle the complexity for you.
Setup and Configuration
Prerequisites
- Turso Account: Sign up at turso.tech
- Turso CLI: Install the Turso CLI tool
- Database Creation: Create a Turso database instance
Installing Turso CLI
Creating a Turso Database
Environment Configuration
Add the following environment variables to your.env
file:
- The database URL should start with
libsql://
- Keep your auth token secure and never commit it to version control
- Use different databases for different environments (dev/staging/prod)
Database Setup in DeployStack
Using the Setup API
Once your environment variables are configured, use the DeployStack setup API:Verification
Check that the database is properly configured:Development Workflow
Schema Development
Turso uses the same SQLite schema as other database types. All schema changes are made insrc/db/schema.sqlite.ts
:
Migration Generation
Generate migrations using the standard Drizzle commands:Database Operations
All standard Drizzle operations work with Turso:Performance Considerations
Connection Management
Turso connections are managed automatically by the libSQL client:- Connection Pooling: Automatic connection pooling for optimal performance
- Keep-Alive: Connections are kept alive to reduce latency
- Automatic Reconnection: Handles network interruptions gracefully
Query Optimization
- Prepared Statements: Use prepared statements for repeated queries
- Batch Operations: Group multiple operations when possible (note: migrations are automatically split for compatibility)
- Indexing: Add appropriate indexes for frequently queried columns
- Migration Performance: Initial migrations execute individually for compatibility, but runtime queries maintain full performance
Regional Performance
- Edge Locations: Turso automatically routes queries to the nearest edge location
- Read Replicas: Read operations are served from local replicas
- Write Consistency: Writes are replicated globally with eventual consistency
Best Practices
Environment Management
Security
- Token Rotation: Regularly rotate authentication tokens
- Environment Isolation: Use separate databases for each environment
- Access Control: Use Turso’s built-in access control features
- Encryption: Data is encrypted in transit and at rest
Monitoring
Debugging and Troubleshooting
Common Issues
Connection Errors- Verify
TURSO_DATABASE_URL
is correct and starts withlibsql://
- Check that
TURSO_AUTH_TOKEN
is valid and not expired - Ensure network connectivity to Turso servers
- Regenerate the auth token:
turso db tokens create your-database
- Verify the token has proper permissions
- Check that the token matches the database
- Status: This error is automatically handled by DeployStack as of version 1.0+
- Cause: Turso’s libSQL client requires individual statement execution
- Solution: DeployStack automatically splits and executes statements individually
- If you encounter this error, ensure you’re running the latest version of DeployStack
- Check migration SQL syntax is valid SQLite
- Verify no conflicting schema changes
- Review migration order and dependencies
- Ensure your Turso database has sufficient resources
Debug Logging
Enable detailed logging to troubleshoot issues:Database Inspection
Performance Monitoring
Technical Implementation Details
How DeployStack Handles Turso Migrations
Unlike SQLite which can execute multiple SQL statements in a single call, Turso’s libSQL client requires each statement to be executed individually. DeployStack solves this transparently:-
Automatic Statement Splitting: Migration files are intelligently parsed to separate:
- Statements divided by
--> statement-breakpoint
markers - Multiple statements on the same line (like consecutive CREATE INDEX commands)
- Complex migrations with mixed DDL operations
- Statements divided by
- Sequential Execution: Each statement is executed in order with proper error handling
- Transaction Safety: Failed statements properly roll back to maintain database consistency
- Performance Impact: Migration execution is slightly slower than SQLite (milliseconds per statement), but this only affects initial setup and schema changes, not runtime performance
Migration File Compatibility
Your existing Drizzle migration files work without modification:Advanced Features
Multi-Region Setup
Database Branching
Backup and Restore
Integration with DeployStack Features
Global Settings
Turso works seamlessly with DeployStack’s global settings system:- Batch Operations: Efficient batch creation of settings
- Encryption: Settings are encrypted before storage
- Performance: Optimized for Turso’s distributed architecture
Plugin System
Plugins can extend the database schema with Turso:Authentication
Lucia authentication works perfectly with Turso:- Session Management: Distributed session storage
- User Data: Global user data replication
- Performance: Fast authentication checks worldwide
Migration from Other Databases
From SQLite
Since Turso is SQLite-compatible, migration is straightforward:- Export SQLite data:
sqlite3 database.db .dump > export.sql
- Import to Turso:
turso db shell your-database < export.sql
- Update environment variables: Switch to Turso configuration
- Test application: Verify all functionality works
From D1 (if previously used)
- Export D1 data: Use Wrangler to export data
- Convert to SQLite format: Ensure compatibility
- Import to Turso: Load data into Turso database
- Update configuration: Switch database type to Turso
Known Limitations and Solutions
Statement Execution
Limitation: Turso cannot execute multiple SQL statements in a single database call. DeployStack Solution: Automatic statement splitting and sequential execution. This is completely transparent - you never need to think about it.Migration Speed
Limitation: Migrations apply slightly slower than with local SQLite due to individual statement execution and network latency. DeployStack Solution: Migrations are a one-time operation during setup or updates. Runtime performance is unaffected. For large migrations, DeployStack provides progress logging to track execution.Cost Optimization
Usage Monitoring
Optimization Strategies
- Query Efficiency: Optimize queries to reduce database load
- Connection Reuse: Leverage connection pooling
- Regional Placement: Choose regions close to your users
- Data Archiving: Archive old data to reduce storage costs
Support and Resources
- Turso Documentation: docs.turso.tech
- libSQL Documentation: github.com/libsql/libsql
- Community Discord: discord.gg/turso
- GitHub Issues: github.com/tursodatabase/turso-cli
Next Steps
- Set up your Turso database following the configuration steps above
- Configure environment variables in your
.env
file - Run the database setup using the DeployStack API
- Start developing with global SQLite performance
- Monitor and optimize your database usage