Skip to main content

systemprompt_runtime/
database_context.rs

1use anyhow::Result;
2use std::sync::Arc;
3use systemprompt_database::{Database, DbPool};
4
5#[derive(Debug, Clone)]
6pub struct DatabaseContext {
7    database: DbPool,
8}
9
10impl DatabaseContext {
11    pub async fn from_url(database_url: &str) -> Result<Self> {
12        let db = Database::new_postgres(database_url).await?;
13        Ok(Self {
14            database: Arc::new(db),
15        })
16    }
17
18    pub async fn from_urls(read_url: &str, write_url: Option<&str>) -> Result<Self> {
19        let db = Database::from_config_with_write("postgres", read_url, write_url).await?;
20        Ok(Self {
21            database: Arc::new(db),
22        })
23    }
24
25    pub const fn db_pool(&self) -> &DbPool {
26        &self.database
27    }
28
29    pub fn db_pool_arc(&self) -> DbPool {
30        Arc::clone(&self.database)
31    }
32}