liveBook · Manning

Database

As explained in Rust Servers, Services, and Apps

Database persistence is the storage of application data in a database so that it survives process termination and can be retrieved later.

Database persistence keeps application data available for later retrieval. A Rust application can connect to PostgreSQL and query it, or use a database to store records beyond the lifetime of a single request. section 4.2 section 4.4.3

In a web application, database persistence supports several parts of the request flow:

  • Connect to PostgreSQL and query it from an async Rust program. section 4.2
  • Store a posted course, then retrieve a course list or individual course details through web APIs. section 4.4.3
  • Retrieve user credentials during sign-in and create, update, or delete records in response to HTTP requests. section 9.1 section 9.8
  • Package the database as a container to make it portable across machines and easier to operate with the web service. section 12.3
Listing 4.10 The main() function for iteration 3
use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
use sqlx::postgres::PgPool;
use std::env;
use std::io;
use std::sync::Mutex;
 
#[path = "../iter3/db_access.rs"]
mod db_access;
#[path = "../iter3/handlers.rs"]
mod handlers;
#[path = "../iter3/models.rs"]
mod models;
#[path = "../iter3/routes.rs"]
mod routes;
#[path = "../iter3/state.rs"]
mod state;
 
use routes::*;
use state::AppState;
 
#[actix_rt::main]
async fn main() -> io::Result<()> {
   dotenv().ok();
 
   let database_url = env::var("DATABASE_URL").expect(
   "DATABASE_URL is not set in .env file");
   let db_pool = PgPool::connect(&database_url).await.unwrap();
 
   let shared_data = web::Data::new(AppState {                      #1
       health_check_response: "I'm good. You've already
       asked me ".to_string(),
       visit_count: Mutex::new(0),
       db: db_pool,
   });
 
   let app = move || {                                              #2
       App::new()
           .app_data(shared_data.clone())                           #3
           .configure(general_routes)                               #4
           .configure(course_routes)                                #4
   };
 
   //Start HTTP server
 
   HttpServer::new(app).bind("127.0.0.1:3000")?.run().await         #5
}

The service entry point reads the database URL, connects a PostgreSQL pool, and shares it through application state. section 4.4.3

How Rust Servers, Services, and Apps covers it

How deep it goes
Builds from an async PostgreSQL connection and query to service-level persistence and CRUD, while leaving distributed errors for later treatment.
Examples
Uses course creation and retrieval, tutor credential lookup, and database container packaging as examples.
What you can do after
Build a Rust web service that stores and retrieves course data through a PostgreSQL-backed API.
How it connects
Connects database persistence to RESTful web services, CRUD operations, authentication, and Docker deployment.

Also explained in

Compare how 24 books explain database →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.