Overview
5 min read
In TypeScript you reach for TypeORM, Prisma, or Knex and trust that your queries are correct until they run. Rust’s database story stakes out the same positions but moves the safety net earlier: SQLx lets you write raw SQL that the compiler type-checks against your real database at build time, Diesel is a synchronous ORM with an end-to-end statically typed query DSL, and SeaORM offers async ActiveRecord ergonomics on top of SQLx. This section covers all three plus the MongoDB and Redis drivers, connection pooling, and migrations — everything you need to talk to a real datastore from a Rust service.
The current stable toolchain is Rust 1.96.0 on the latest stable edition (2024); cargo new selects it automatically. Crate examples are compile-verified against the versions pinned throughout this section (SQLx 0.8.6, Diesel 2.3, SeaORM 1.1, the mongodb driver 3.7, the redis crate 1.2, deadpool 0.14, bb8 0.9) and run against in-memory or bundled SQLite wherever a server is not needed. (SQLx 0.9 shipped in May 2026; the examples deliberately pin the 0.8 line.)
What You’ll Learn
Section titled “What You’ll Learn”- How SQLx checks raw SQL against your live schema at compile time, and how to connect to PostgreSQL and SQLite from a single async API
- How to write queries with the
query!/query_as!macros, bind parameters (which is what actually prevents SQL injection), and map rows into structs withFromRow - How to run transactions in SQLx —
begin/commit/rollback, the RAIITransactionguard, and the atomicity guarantees it gives you - How Diesel’s synchronous ORM works: the generated
schema.rs, model structs, project setup, and why there is noasync/await - How to build queries with Diesel’s typed DSL —
filter/select/order/insert/update/delete - How to model relationships in Diesel with
belongs_to/has_manyassociations, joins, and eager loading - How to use MongoDB from Rust: BSON, documents, CRUD, and typed collections backed by serde
- How to use Redis from Rust: async connections, typed command replies, and the everyday patterns (caching, counters, rate limiting)
- How to manage connection pools:
sqlx::Pool, the genericdeadpool/bb8poolers, and the sizing and lifecycle knobs that keep a service healthy under load - How to run database migrations with
sqlx migrateand Diesel’s migration system — up/down scripts and running them at startup - How to choose between SQLx, Diesel, and SeaORM for a given project
Topics
Section titled “Topics”| Topic | Description |
|---|---|
| SQLx Intro | SQL databases with SQLx: async, compile-time-checked queries, setup, and connecting to PostgreSQL/SQLite. |
| SQLx Queries | Writing queries with query!/query_as!; binding parameters (which prevents SQL injection) and mapping rows with FromRow. |
| SQLx Transactions | Transactions in SQLx: begin/commit/rollback, the Transaction guard, and atomicity. |
| Diesel Intro | The Diesel ORM (like TypeORM): the table! schema, models, project setup, and the synchronous model. |
| Diesel Queries | Diesel’s query builder: filter/select/order/insert/update/delete and the typed DSL. |
| Diesel Relations | Relationships and joins in Diesel: belongs_to/has_many associations and eager loading. |
| MongoDB | MongoDB with the official driver: BSON, documents, CRUD, and typed collections via serde. |
| Redis | Redis with the redis crate: commands, async connections, and common patterns (cache, counters). |
| Connection Pooling | Connection pool management: sqlx::Pool, deadpool/bb8, and sizing and lifecycle. |
| Migrations | Database migrations: sqlx migrate and Diesel migrations; up/down scripts and running at startup. |
| ORM Comparison | Comparing SQLx vs Diesel vs SeaORM: compile-checked SQL vs ORM ergonomics, and when to use which. |
Learning Objectives
Section titled “Learning Objectives”By the end of this section, you will be able to:
- Explain how SQLx’s compile-time query checking differs from the runtime-only validation of TypeORM, Prisma, and Knex, and connect to PostgreSQL and SQLite
- Write parameterized queries with the
query!/query_as!macros and map results into your own types withFromRow - Wrap multi-statement operations in a transaction and rely on the
Transactionguard to roll back on early return - Set up a Diesel project, generate
schema.rsfrom migrations, and define model structs for reads and writes - Build reads and writes through Diesel’s typed DSL and reason about the compiler errors when a type or column is wrong
- Model and traverse relationships with
belongs_to/has_manyassociations and load related rows efficiently - Perform CRUD against MongoDB with typed, serde-backed documents and against Redis with typed command replies
- Configure a connection pool with appropriate size, timeouts, and lifetimes, and share it across handlers as a cheap clonable handle
- Author and run up/down migrations with both SQLx and Diesel, including at application startup
- Pick the right database layer — compile-checked SQL vs. ORM ergonomics — for a given project and justify the choice
Prerequisites
Section titled “Prerequisites”- Section 11: Async — SQLx, MongoDB, Redis, and SeaORM are all
async; you needasync/await, the Tokio runtime, and the lazy-future model. Diesel is the synchronous exception, which this section contrasts against the async crates. - Section 15: Serialization — serde’s
Serialize/Deserializedrive typed MongoDB documents and JSON columns, andFromRow/Queryablefollow the same “map a row into a typed struct” idea.
A working knowledge of error handling (Result, ?, anyhow/thiserror) is assumed throughout — every database call returns a Result you propagate.
Estimated Time
Section titled “Estimated Time”Approximately 14 hours, including reading, hands-on practice, and the per-topic exercises.
Tip: If you only have time for one track, read
sqlx-intro→sqlx-queries→sqlx-transactions→connection-pooling→migrations. That is the most common path for a new Rust web service. Treat the Diesel pages, MongoDB, and Redis as a toolbox to reach for when a specific need arises, and readorm-comparisononce to make the SQLx-vs-Diesel-vs-SeaORM decision deliberately.
Continue to Section 18: CLI Tools to build self-contained command-line tools with clap, ratatui, and indicatif.