Skip to main content

jax_daemon/database/
mod.rs

1pub mod bucket_log_provider;
2mod bucket_queries;
3pub mod models;
4mod sqlite;
5pub mod types;
6
7use std::ops::Deref;
8
9use sqlx::SqlitePool;
10
11#[derive(Clone, Debug)]
12pub struct Database(SqlitePool);
13
14#[allow(dead_code)]
15pub type DatabaseConnection = sqlx::SqliteConnection;
16
17impl Database {
18    pub async fn connect(database_url: &url::Url) -> Result<Self, DatabaseSetupError> {
19        if database_url.scheme() == "sqlite" {
20            let db = sqlite::connect_sqlite(database_url).await?;
21            sqlite::migrate_sqlite(&db).await?;
22            return Ok(Database::new(db));
23        }
24
25        Err(DatabaseSetupError::UnknownDbType(
26            database_url.scheme().to_string(),
27        ))
28    }
29
30    pub fn new(pool: SqlitePool) -> Self {
31        Self(pool)
32    }
33}
34
35impl Deref for Database {
36    type Target = SqlitePool;
37
38    fn deref(&self) -> &Self::Target {
39        &self.0
40    }
41}
42
43#[derive(Debug, thiserror::Error)]
44pub enum DatabaseSetupError {
45    #[error("error occurred while attempting database migration: {0}")]
46    MigrationFailed(sqlx::migrate::MigrateError),
47
48    #[error("unable to perform initial connection and check of the database: {0}")]
49    Unavailable(sqlx::Error),
50
51    #[error("requested database type was not recognized: {0}")]
52    UnknownDbType(String),
53}