use crate::{Turso, TursoConnectOptions};
use sqlx::{Error, pool::PoolOptions};
use std::sync::{Arc, atomic::AtomicBool};
pub type TursoPool = sqlx::Pool<Turso>;
pub async fn connect_pool(
mut options: TursoConnectOptions,
max_connections: u32,
) -> Result<TursoPool, Error> {
if max_connections == 0 || (options.path.is_none() && max_connections != 1) {
return Err(Error::Configuration(
"sqlx-turso: capacity must be positive; memory pools require capacity 1".into(),
));
}
let mut pool = PoolOptions::<Turso>::new().max_connections(max_connections);
if options.path.is_none() {
options.memory_pool_opened = Some(Arc::new(AtomicBool::new(false)));
pool = pool.idle_timeout(None).max_lifetime(None);
}
pool.connect_with(options).await
}