Connecting our application to PostgreSQL
In the previous section, we managed to connect to the PostgreSQL database using the terminal. However, we now need our app to manage the reading and writing to the database for our to-do items. In this section, we will connect our application to the database running in Docker. In order to connect, we have to build a function that establishes a connection, and then returns it. In the src/database.rs
file, we define the function with the following code block:
use diesel::prelude::*; use diesel::pg::PgConnection; use dotenv::dotenv; use std::env; pub fn establish_connection() -> PgConnection { Â Â Â Â dotenv().ok(); Â Â Â Â let database_url = env::var("DATABASE_URL") Â Â Â Â Â Â Â Â .expect("DATABASE_URL must be set"); Â Â Â Â PgConnection::establish(&database_url) Â Â Â Â Â Â Â Â .unwrap_or_else(|_| panic...