Canyon-SQL is an async Rust ORM and query builder for PostgreSQL, MySQL, and SQL Server. It grew around a simple frustration: the same table knowledge tends to be repeated in a model, a row mapper, a repository, and a collection of SQL strings. Canyon lets a Rust struct carry that knowledge and generates the routine work around it.
Tip
Looking for the full reference? The Canyon book is where configuration, generated methods, query building, relationships, and backend behavior are documented. The book is being refreshed to match the current API.
You define an entity with #[canyon_entity] and derive the capabilities it needs. CanyonMapper turns database rows into that Rust type. Crud supplies the familiar read, insert, update, and delete operations; Read, Insert, Update, and Delete can also be derived separately. When a generated operation is too simple, Fields and the query builder give you typed column names and bound values. You can always use a connection directly for SQL that is best written by hand.
A project may configure several named datasources, even across different database engines. The first active one is the default; _with methods let you choose another by name or pass a compatible connection. Operations return CanyonResult<T>, with typed errors for configuration, connections, queries, query building, and mapping. A missing row is an Option::None, not a swallowed error.
Choose at least one backend feature. Canyon has no default SQL backend; this example uses PostgreSQL and a Tokio runtime:
[dependencies] canyon_sql = { version = "0.5.1", features = ["postgres"] } tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
The same crate can be built with mysql, mssql, or a combination of the three. Next, create a canyon.toml beside the directory where you run the application:
[canyon_sql] [[canyon_sql.datasources]] name = "main" [canyon_sql.datasources.auth] postgresql = { basic = { username = "postgres", password = "postgres" } } [canyon_sql.datasources.properties] host = "localhost" port = 5432 db_name = "app"
These credentials are illustrative; keep real ones out of version control. Canyon discovers a canyon*.toml file from the current working directory and nearby subdirectories. Keep only one matching file there so startup does not pick an unintended configuration.
The example assumes the table exists. For PostgreSQL, you could create it with:
CREATE TABLE teams ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, name TEXT NOT NULL );
Now give that table a Rust model. The explicit Canyon::init().await? call lets the application handle startup failures as typed errors:
use canyon_sql::{ core::Canyon, crud::Read, macros::{canyon_entity, CanyonMapper, Crud, Fields}, CanyonResult, }; #[derive(Debug, Fields, Crud, CanyonMapper)] #[canyon_entity(table_name = "teams")] struct Team { #[primary_key] id: i64, name: String, } #[tokio::main] async fn main() -> CanyonResult<()> { Canyon::init().await?; for team in Team::find_all().await? { println!("{}: {}", team.id, team.name); } Ok(()) }
#[primary_key] tells Canyon which field identifies a row. A numeric key is treated as database-generated by default; insert(&mut self) writes the new key back to the struct. If the application supplies the key, use #[primary_key(autoincremental = false)]. Existing tables with a different name can be mapped with table_name and schema on #[canyon_entity].
Team::find_all() reads the table. Team::find_by_pk(&id) returns CanyonResult<Option<Team>>, and Team::count() returns a count. A filter can instead be built with the field helpers generated by Fields:
use canyon_sql::{ crud::Read, query::{operators::Operator, querybuilder::QueryBuilderExt}, }; let name = TeamFieldValue::name("Blue".to_owned()); let teams: Vec<Team> = Team::select_query()? .where_value(&name, Operator::Eq) .build()? .launch_default() .await?;
The value remains a bound parameter, while Canyon renders identifiers and placeholders for the selected backend. Fields currently generates TeamTable, TeamField, and TeamFieldValue; these enums are the supported API today. A future major version may use typed column descriptors instead, but no replacement is scheduled. The builder also supports joins, ordering, conditional updates, and deletes.
Relationships start with the Rust field that stores the foreign key:
#[foreign_key(references = Team::id)] team_id: i64,
On a child model such as Player, this generates player.find_team() and Player::find_all_by_team(&team), as well as _with variants. The annotation does not create a SQL foreign-key constraint; define that in your database schema. Custom physical table and schema names come from the related entity's metadata rather than another string in the foreign-key annotation.
Canyon maps tables; the ordinary CRUD path does not create or migrate them. The migrations Cargo feature exists but is experimental and incomplete. Do not rely on it for production schema changes.
The public API is still evolving. Pin the crate version when stability matters, and consult the changelog when upgrading. The integration tests are also concrete examples of what the current code supports.
If you are working on Canyon itself, you can run formatting and library tests without the database containers:
cargo fmt --all -- --check
cargo test --workspace --lib --all-featuresThe full integration suite needs PostgreSQL, MySQL, and SQL Server fixtures; its setup is described in the book. Contributions are welcome—start with CONTRIBUTING.md.