use std::path::Path;
use std::str::FromStr;
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous};
use sqlx::SqlitePool;
use crate::error::Result;
pub async fn open(path: &Path) -> Result<SqlitePool> {
if let Some(parent) = path.parent() {
tokio::fs::create_dir_all(parent).await?;
}
let opts = SqliteConnectOptions::from_str(&format!("sqlite://{}", path.display()))?
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal)
.synchronous(SqliteSynchronous::Normal)
.foreign_keys(true)
.busy_timeout(std::time::Duration::from_secs(5));
let pool = SqlitePoolOptions::new()
.max_connections(8)
.min_connections(1)
.connect_with(opts)
.await?;
Ok(pool)
}