use sqlx::postgres::PgPool;
use super::{Client, opts};
use crate::{Error, utils};
#[derive(Debug, Clone)]
pub struct ClientBuilder {
schema: String,
}
impl Default for ClientBuilder {
fn default() -> Self {
ClientBuilder {
schema: "pgboss".to_string(),
}
}
}
impl ClientBuilder {
pub fn schema<S>(mut self, schema: S) -> Self
where
S: Into<String>,
{
self.schema = schema.into();
self
}
pub async fn connect(self) -> Result<Client, Error> {
let pool = utils::create_pool(None).await?;
self.with_pool(pool).await
}
pub async fn connect_to<S>(self, url: S) -> Result<Client, Error>
where
S: AsRef<str>,
{
let pool = utils::create_pool(Some(url.as_ref())).await?;
self.with_pool(pool).await
}
pub async fn with_pool(self, pool: PgPool) -> Result<Client, Error> {
let opts = opts::ClientOptions {
schema: self.schema,
};
Ok(Client::new(pool, opts).await?)
}
}