use crate::PostgresError;
use derive_more::Debug;
use futures::executor::block_on;
use tokio_postgres::{Client, Connection, NoTls, Socket, tls::NoTlsStream};
pub const DEFAULT_URL: &str = "postgres://postgres@localhost:5432";
#[cfg_attr(doc, aquamarine::aquamarine)]
#[derive(Debug)]
pub struct PostgresStore {
pub client: Client,
#[debug(skip)]
pub connection: Connection<Socket, NoTlsStream>,
}
impl PostgresStore {
pub async fn open(url: impl AsRef<str>) -> Result<Self, PostgresError> {
let (client, connection) = tokio_postgres::connect(url.as_ref(), NoTls).await?;
Ok(Self { client, connection })
}
}
impl Default for PostgresStore {
fn default() -> Self {
block_on(Self::open(DEFAULT_URL))
.expect("should connect to postgres://postgres@localhost:5432")
}
}