Skip to main content

systemprompt_runtime/
database_context.rs

1//! Lightweight database-only runtime context.
2//!
3//! [`DatabaseContext`] is used by CLI / tooling paths that need a
4//! `DbPool` without spinning up the full [`crate::AppContext`].
5
6use crate::error::RuntimeResult;
7use std::sync::Arc;
8use systemprompt_database::{Database, DbPool};
9
10#[derive(Debug, Clone)]
11pub struct DatabaseContext {
12    database: DbPool,
13}
14
15impl DatabaseContext {
16    pub async fn from_url(database_url: &str) -> RuntimeResult<Self> {
17        let db = Database::new_postgres(database_url).await?;
18        Ok(Self {
19            database: Arc::new(db),
20        })
21    }
22
23    pub async fn from_urls(read_url: &str, write_url: Option<&str>) -> RuntimeResult<Self> {
24        let db = Database::from_config_with_write("postgres", read_url, write_url).await?;
25        Ok(Self {
26            database: Arc::new(db),
27        })
28    }
29
30    pub const fn db_pool(&self) -> &DbPool {
31        &self.database
32    }
33
34    pub fn db_pool_arc(&self) -> DbPool {
35        Arc::clone(&self.database)
36    }
37}