1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::path::Path;

use sqlx::prelude::*;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::SqliteConnection;

pub mod models;

pub type DbError = sqlx::Error;

pub struct Db {
    connection: SqliteConnection,
}

impl Db {
    pub async fn open(path: &Path) -> Result<Self, DbError> {
        debug!(path = %path.display(), "loading database");

        Ok(Self {
            connection: SqliteConnection::connect_with(&SqliteConnectOptions::new().filename(path))
                .await?,
        })
    }
}

impl std::ops::Deref for Db {
    type Target = SqliteConnection;

    fn deref(&self) -> &Self::Target {
        &self.connection
    }
}

impl std::ops::DerefMut for Db {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.connection
    }
}