what2watch/src/db.rs

33 lines
890 B
Rust
Raw Normal View History

2023-05-10 19:08:03 +00:00
use std::time::Duration;
use sqlx::{
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
SqlitePool,
};
const MAX_CONNS: u32 = 100;
const TIMEOUT: u64 = 5;
pub async fn get_pool() -> SqlitePool {
let db_filename = {
std::env::var("DATABASE_FILE").unwrap_or_else(|_| {
let home =
std::env::var("HOME").expect("Could not determine $HOME for finding db file");
format!("{home}/.witch-watch.db")
})
};
let conn_opts = SqliteConnectOptions::new()
.foreign_keys(true)
.auto_vacuum(sqlx::sqlite::SqliteAutoVacuum::Incremental)
2023-05-28 19:20:55 +00:00
.filename(&db_filename)
.busy_timeout(Duration::from_secs(TIMEOUT));
2023-05-10 19:08:03 +00:00
// setup connection pool
SqlitePoolOptions::new()
.max_connections(MAX_CONNS)
.connect_with(conn_opts)
.await
.expect("can't connect to database")
}