GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - gbm25/Canyon-SQL: A Rust ORM and query builder for multiple databases

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

579 Commits
579 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
.vscode
.vscode
 
 
canyon_core
canyon_core
 
 
canyon_crud
canyon_crud
 
 
canyon_entities
canyon_entities
 
 
canyon_macros
canyon_macros
 
 
canyon_migrations
canyon_migrations
 
 
docker
docker
 
 
src
src
 
 
tests
tests
 
 
.DS_Store
.DS_Store
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
bash_aliases.sh
bash_aliases.sh
 
 

Repository files navigation

Canyon-SQL

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.

Crates.io CI Code quality Coverage License: MIT

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.

🧭 How Canyon fits into an application

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.

🚀 Read your first table

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].

🔎 When a simple read is not enough

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.

⚠️ What is still evolving

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-features

The full integration suite needs PostgreSQL, MySQL, and SQL Server fixtures; its setup is described in the book. Contributions are welcome—start with CONTRIBUTING.md.

About

A Rust ORM and query builder for multiple databases

zerodaycode.github.io/canyon-book/

Resources

Readme
MIT license

Code of conduct

Code of conduct

Contributing

Contributing
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.