1use std::path::Path;
2
3use sqlx::prelude::*;
4use sqlx::sqlite::SqliteConnectOptions;
5use sqlx::SqliteConnection;
6
7pub mod models;
8
9pub type DbError = sqlx::Error;
10
11pub struct Db {
12 connection: SqliteConnection,
13}
14
15impl Db {
16 pub async fn open(path: &Path) -> Result<Self, DbError> {
17 debug!(path = %path.display(), "loading database");
18
19 Ok(Self {
20 connection: SqliteConnection::connect_with(&SqliteConnectOptions::new().filename(path))
21 .await?,
22 })
23 }
24}
25
26impl std::ops::Deref for Db {
27 type Target = SqliteConnection;
28
29 fn deref(&self) -> &Self::Target {
30 &self.connection
31 }
32}
33
34impl std::ops::DerefMut for Db {
35 fn deref_mut(&mut self) -> &mut Self::Target {
36 &mut self.connection
37 }
38}