Spacedrive uses SQLite with SeaORM for database operations. The database is embedded within each library, providing fast local queries and simple backup strategies.
Technology Stack
We chose SQLite and SeaORM for specific technical reasons:
SQLite provides embedded database functionality without external dependencies. It runs in-process with zero network overhead and supports advanced features like WAL mode for concurrent access.
SeaORM offers type-safe database access in Rust. It generates compile-time checked queries, manages schema migrations automatically, and provides connection pooling out of the box.
This combination replaced our previous prisma-client-rust dependency, which was abandoned upstream.
Database Configuration
Each library configures SQLite for optimal performance:
These pragmas enable:
WAL Mode: Allows readers and writers to work concurrently.
Normal Synchronous: Balances durability with performance.
Large Cache: Keeps frequently accessed data in memory.
Memory Temp Store: Uses RAM for temporary tables.
Storage Efficiency
Our schema design minimizes storage overhead through careful optimization. For a typical library with 100,000 files:
Compared to naive implementations, this represents a 70% reduction in storage requirements.
Migration System
SeaORM manages schema changes through versioned migrations. Each migration defines forward and rollback operations:
The migration system tracks applied migrations in a seaql_migrations table. This ensures each migration runs exactly once.
Apply migrations during library initialization:
Index Strategy
Indexes are critical for query performance. We maintain indexes for:
UUID Lookups: Every table with a UUID has a unique index for O(1) lookups.
Foreign Keys: All foreign key columns are indexed for fast joins.
Common Filters: Frequently queried columns like kind, size, and favorite have dedicated indexes.
Composite Indexes: Multi-column indexes optimize specific query patterns.
Create partial indexes for better performance:
SeaORM automatically uses prepared statements for repeated queries. This avoids SQL parsing overhead and enables query plan caching.
For UI responsiveness, always paginate large result sets:
Use select_only() to fetch only required columns:
Connection Pooling
SeaORM manages a connection pool automatically. Configure pool settings based on your workload:
These settings balance resource usage with responsiveness. Most operations complete within a single connection.
Backup and Recovery
Libraries support multiple backup strategies:
File-Based Backup
The simplest backup method copies the database file:
SQLite Backup API
For live backups without stopping operations:
This creates a compacted backup while the database remains accessible.
Crash Recovery
WAL mode provides automatic crash recovery. If Spacedrive crashes, SQLite automatically rolls back incomplete transactions on the next startup.
The WAL file contains all pending writes. SQLite replays this journal to restore database consistency.
Never delete the -wal or -shm files manually. SQLite uses these for
recovery.
Maintenance Operations
Database Optimization
Run optimization periodically to maintain performance:
ANALYZE updates table statistics for better query planning.
VACUUM rebuilds the database file, removing deleted data and defragmenting tables.
PRAGMA optimize analyzes recent query patterns to suggest new indexes.
Integrity Checks
Verify database integrity after crashes or disk errors:
This performs extensive validation of database structures and returns any corruption found.
Size Monitoring
Track database growth over time:
Extension Support
Spacedrive extensions can create custom tables at runtime. The database layer provides APIs for:
Table Creation: Extensions define tables with proper namespacing.
Migration Tracking: Each extension manages its own schema versions.
Foreign Keys: Extensions can reference core tables safely.
Cleanup: Tables are removed when extensions uninstall.
See the Data Model documentation for details on extension table design.
Future Enhancements
Planned database improvements include:
Full-Text Search: SQLite FTS5 for searching file content and metadata.
JSON Operations: Native JSON functions for querying structured data.
R-Tree Indexes: Spatial indexing for geographic data.
Encryption: SQLCipher integration for at-rest encryption.
These features will be added as libraries need them, maintaining backward compatibility.