Skip to content

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


  • 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 with FromRow
  • How to run transactions in SQLx — begin/commit/rollback, the RAII Transaction guard, 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 no async/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_many associations, 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 generic deadpool/bb8 poolers, and the sizing and lifecycle knobs that keep a service healthy under load
  • How to run database migrations with sqlx migrate and Diesel’s migration system — up/down scripts and running them at startup
  • How to choose between SQLx, Diesel, and SeaORM for a given project

TopicDescription
SQLx IntroSQL databases with SQLx: async, compile-time-checked queries, setup, and connecting to PostgreSQL/SQLite.
SQLx QueriesWriting queries with query!/query_as!; binding parameters (which prevents SQL injection) and mapping rows with FromRow.
SQLx TransactionsTransactions in SQLx: begin/commit/rollback, the Transaction guard, and atomicity.
Diesel IntroThe Diesel ORM (like TypeORM): the table! schema, models, project setup, and the synchronous model.
Diesel QueriesDiesel’s query builder: filter/select/order/insert/update/delete and the typed DSL.
Diesel RelationsRelationships and joins in Diesel: belongs_to/has_many associations and eager loading.
MongoDBMongoDB with the official driver: BSON, documents, CRUD, and typed collections via serde.
RedisRedis with the redis crate: commands, async connections, and common patterns (cache, counters).
Connection PoolingConnection pool management: sqlx::Pool, deadpool/bb8, and sizing and lifecycle.
MigrationsDatabase migrations: sqlx migrate and Diesel migrations; up/down scripts and running at startup.
ORM ComparisonComparing SQLx vs Diesel vs SeaORM: compile-checked SQL vs ORM ergonomics, and when to use which.

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 with FromRow
  • Wrap multi-statement operations in a transaction and rely on the Transaction guard to roll back on early return
  • Set up a Diesel project, generate schema.rs from 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_many associations 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

  • Section 11: Async — SQLx, MongoDB, Redis, and SeaORM are all async; you need async/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/Deserialize drive typed MongoDB documents and JSON columns, and FromRow/Queryable follow 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.


Approximately 14 hours, including reading, hands-on practice, and the per-topic exercises.

Tip: If you only have time for one track, read sqlx-introsqlx-queriessqlx-transactionsconnection-poolingmigrations. 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 read orm-comparison once 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.