fire_postgres/
database.rs1use crate::table::{Table, TableTemplate};
2
3use deadpool_postgres::{Pool, Runtime};
4
5use tokio_postgres::NoTls;
6
7#[derive(Debug, Clone)]
8pub struct Database {
9 pool: Pool,
10}
11
12impl Database {
13 pub async fn new(name: &str, user: &str, password: &str) -> Self {
15 Self::with_host("localhost", name, user, password).await
16 }
17
18 pub async fn with_host(
20 host: &str,
21 name: &str,
22 user: &str,
23 password: &str,
24 ) -> Self {
25 let config = deadpool_postgres::Config {
26 host: Some(host.to_string()),
27 dbname: Some(name.to_string()),
28 user: Some(user.to_string()),
29 password: Some(password.to_string()),
30 ..Default::default()
31 };
32
33 let pool = config.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
34
35 let _client =
37 pool.get().await.expect("could not get a postgres client");
38
39 Self { pool }
40 }
41
42 pub fn table<T>(&self, name: &'static str) -> Table<T>
44 where
45 T: TableTemplate,
46 {
47 Table::new(self.pool.clone(), name)
48 }
49}