fluffle/database/
mod.rs

1mod entries;
2mod sql;
3
4use crate::config::Config;
5use oiseau::{execute, postgres::DataManager as OiseauManager, postgres::Result as PgResult};
6use tetratto_core::model::{Error, Result};
7
8pub const NAME_REGEX: &str = r"[^\w_\-\.,!]+";
9
10#[derive(Clone)]
11pub struct DataManager(pub OiseauManager<Config>);
12
13impl DataManager {
14    /// Create a new [`DataManager`].
15    pub async fn new(config: Config) -> PgResult<Self> {
16        Ok(Self(OiseauManager::new(config).await?))
17    }
18
19    /// Initialize tables.
20    pub async fn init(&self) -> Result<()> {
21        let conn = match self.0.connect().await {
22            Ok(c) => c,
23            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
24        };
25
26        execute!(&conn, sql::CREATE_TABLE_ENTRIES).unwrap();
27
28        Ok(())
29    }
30}