liveBook · Manning

Database

As explained in Code Like a Pro in Rust

A database is an organized system for persistently storing, retrieving, and modifying structured data.

A database provides persistent state for a web service, allowing application code to create, read, update, delete, and list structured records. section 9.0

A practical service architecture treats the database as a stateful tier:

  • Use SQLite for the working implementation; a production deployment might instead use a SQL database such as PostgreSQL. section 9.2
  • Let each horizontally scalable API instance connect independently to the database for storing and retrieving state. section 9.2
  • Configure the database connection through an environment variable alongside logging configuration. section 9.2
  • Model records with Rust structs and derive sqlx::FromRow so SQLx can map query results into application types. section 9.6.2

Todo persistence with SQLx

Implement list and read operations with parameter binding: query_as("select * from todos").fetch_all(&dbpool) and query_as("select * from todos where id = ?").bind(id).fetch_one(&dbpool). Await each query and map sqlx::Error into the service error type. Apply the same pattern to create, update, and delete operations, taking care to bind values in SQL placeholder order. section 9.6.2

Figure 9.1 Web service architecture
CH09_F01_Matthews

Use the architecture diagram to trace requests from the load balancer through API instances to the stateful database tier. section 9.2

How Code Like a Pro in Rust covers it

How deep it goes
Explains database placement, configuration, typed query mapping, CRUD methods, binding order, and error conversion.
Examples
Uses a todo table, Todo struct, SQLite queries, connection pools, and create, read, update, delete, and list methods.
What you can do after
Apply it via Rust and SQLx to implement typed asynchronous CRUD access for a web service.
How it connects
Connects databases with REST APIs, SQLite, SQL databases, connection pools, SQLx, CRUD, and environment configuration.

Also explained in

Compare how 24 books explain database →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.