use deadpool_sqlite::{Config, Hook, Pool, Runtime};
use wacore::store::error::{Result, StoreError};
#[derive(Clone)]
pub struct Store {
pub(super) pool: Pool,
pub(super) device_id: i32,
}
impl Store {
pub async fn new(path: &str) -> Result<Self> {
let pool = Config::new(path)
.builder(Runtime::Tokio1)
.map_err(|e| StoreError::Connection(e.to_string().into()))?
.max_size(1)
.post_create(Hook::async_fn(|conn, _| {
Box::pin(async move {
conn.interact(|conn| {
conn.execute_batch(
"PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 10000;",
)
})
.await
.map_err(|e| deadpool_sqlite::HookError::Message(e.to_string().into()))?
.map_err(|e| deadpool_sqlite::HookError::Message(e.to_string().into()))?;
Ok(())
})
}))
.build()
.map_err(|e| StoreError::Connection(e.to_string().into()))?;
let store = Self { pool, device_id: 1 };
store.run_migrations().await?;
Ok(store)
}
}