Working with a database
Currently, in our application, we are storing user data in a static variable. This is very cumbersome, as it is inflexible and we cannot update the data easily. Most modern applications handling data will use some kind of persistent storage, be it filesystem-backed storage, a document-oriented database, or a traditional RDBMS.
Rust has many libraries to connect to various databases or database-like storage. There's the postgres
crate, which works as a PostgreSQL client for Rust. There are also other clients such as mongodb
and redis
. For object-relational mapping (ORM) and Query Builder, there's diesel
, which can be used to connect to various database systems. For connection pool management, there are the deadpool
and r2d2
crates. All crates have their strengths and limitations, such as not having an asynchronous application.
In this book, we're going to use sqlx
to connect to an RDBMS. sqlx
claims to be an SQL toolkit for Rust. It has abstractions...