use super::dht::*;
use super::posts::core::OkuPost;
use super::users::*;
use crate::fs::FS_PATH;
use miette::IntoDiagnostic;
use native_db::*;
use std::{path::PathBuf, sync::LazyLock};
pub(crate) static DATABASE_PATH: LazyLock<PathBuf> =
LazyLock::new(|| PathBuf::from(FS_PATH).join("OKU_FS_DATABASE"));
pub static DATABASE: LazyLock<OkuDatabase> = LazyLock::new(|| OkuDatabase::new().unwrap());
pub(crate) static MODELS: LazyLock<Models> = LazyLock::new(|| {
let mut models = Models::new();
models.define::<OkuUser>().unwrap();
models.define::<OkuPost>().unwrap();
models.define::<ReplicaAnnouncement>().unwrap();
models
});
pub struct OkuDatabase {
pub(crate) database: Database<'static>,
}
impl OkuDatabase {
pub fn new() -> miette::Result<Self> {
Ok(Self {
database: native_db::Builder::new()
.create(&MODELS, &*DATABASE_PATH)
.into_diagnostic()?,
})
}
pub fn migrate(&self) -> miette::Result<()> {
let rw = self.database.rw_transaction().into_diagnostic()?;
rw.migrate::<OkuUser>().into_diagnostic()?;
rw.migrate::<OkuPost>().into_diagnostic()?;
rw.migrate::<ReplicaAnnouncement>().into_diagnostic()?;
rw.commit().into_diagnostic()
}
}