what2watch/src/db.rs
2023-05-28 12:20:55 -07:00

32 lines
890 B
Rust

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)
.filename(&db_filename)
.busy_timeout(Duration::from_secs(TIMEOUT));
// setup connection pool
SqlitePoolOptions::new()
.max_connections(MAX_CONNS)
.connect_with(conn_opts)
.await
.expect("can't connect to database")
}