Toasty Architecture Overview
Project Structure
Toasty is an ORM for Rust that supports SQL and NoSQL databases. The codebase is a Cargo workspace with separate crates for each layer.
Crates
1. toasty
User-facing crate with query engine and runtime.
Key Components:
engine/: Multi-phase query compilation and execution pipeline- See Query Engine Architecture for detailed documentation
stmt/: Typed statement builders (wrappers aroundtoasty_core::stmttypes)relation/: Relationship abstractions (HasMany, BelongsTo, HasOne)model.rs: Model trait and ID generation
Query Execution Pipeline (high-level):
Statement AST → Simplify → Lower → Plan → Execute → Results
The engine compiles queries into a mini-program of actions executed by an interpreter. For details on HIR, MIR, and the full compilation pipeline, see Query Engine Architecture.
2. toasty-core
Shared types used by all other crates: schema representations, statement AST, and driver interface.
Key Components:
schema/: Model and database schema definitionsapp/: Model-level definitions (fields, relations, constraints)db/: Database-level table and column definitionsmapping/: Maps between models and database tablesbuilder/: Schema construction utilitiesverify/: Schema validation
stmt/: Statement AST nodes for queries, inserts, updates, deletesdriver/: Driver interface, capabilities, and operations
3. toasty-codegen
Generates Rust code from the #[derive(Model)] macro.
Key Components:
schema/: Parses model attributes into schema representationexpand/: Generates implementations for modelsmodel.rs: Model trait implementationquery.rs: Query builder methodscreate.rs: Create/insert buildersupdate.rs: Update buildersrelation.rs: Relationship methodsfields.rs: Field accessorsfilters.rs: Filter method generationschema.rs: Runtime schema generation
4. toasty-driver-*
Database-specific driver implementations.
Supported Databases:
toasty-driver-sqlite: SQLite implementationtoasty-driver-postgresql: PostgreSQL implementationtoasty-driver-mysql: MySQL implementationtoasty-driver-dynamodb: DynamoDB implementation
5. toasty-sql
Converts statement AST to SQL strings. Used by SQL-based drivers.
Key Components:
serializer/: SQL generation with dialect supportflavor.rs: Database-specific SQL dialectsstatement.rs: Statement serializationexpr.rs: Expression serializationty.rs: Type serialization
stmt/: SQL-specific statement types
Further Reading
- Query Engine Architecture - Query compilation and execution pipeline
- Type System - Type system design and conversions