gitmyhub

diesel

Rust ★ 14k updated 1d ago

A safe, extensible ORM and Query Builder for Rust

A Rust library that connects your program to PostgreSQL, MySQL, or SQLite databases and catches SQL errors at compile time so bugs never reach production.

RustPostgreSQLMySQLSQLitesetup: moderatecomplexity 3/5

Diesel is a library for Rust programs that need to read and write data to a database. It works with PostgreSQL, MySQL, and SQLite. The core idea is that Diesel handles the translation between your Rust data structures and the database rows automatically, so you do not have to write that conversion code yourself every time.

One of the main things Diesel is known for is catching database errors before your program ever runs. Because it uses Rust's type system, many mistakes that would normally only surface at runtime, like referencing a column that does not exist or passing the wrong kind of value, get flagged during compilation instead. This can save a lot of debugging time.

The query builder lets you write database queries using Rust syntax rather than raw SQL strings. Simple operations like loading all rows from a table, filtering by a condition, or inserting new records look like ordinary Rust code. For complex queries with subqueries, ordering, and date filtering, the builder chains together cleanly and generates the correct SQL behind the scenes. If you need to write raw SQL directly, Diesel provides a way to do that too, with support for binding parameters safely.

A code generation tool handles the repetitive parts. When you define a struct that maps to a database table, Diesel can automatically generate the code needed to read rows into that struct or insert the struct's values into the table. Without it, you would have to write that mapping code by hand for every field in every table.

Diesel is available as a Rust package, configured by adding it to your project's dependency file and specifying which database you are using. The project has documentation, a getting-started guide on its website, and a GitHub Discussions forum where contributors and users ask questions and share projects.

Where it fits