djc / bb8

Full-featured async (tokio-based) postgres connection pool (like r2d2)
MIT License
753 stars 110 forks source link

How to store and use values made during on_acquire? #110

Open mu-arch opened 3 years ago

mu-arch commented 3 years ago

I have the following code:

#[derive(Debug)]
struct ConnectionCustomizer {}

#[async_trait]
impl bb8_postgres::bb8::CustomizeConnection<Client, bb8_postgres::tokio_postgres::Error> for ConnectionCustomizer {
    async fn on_acquire(
        &self,
        connection: & mut bb8_postgres::tokio_postgres::Client,
    ) -> Result<(), bb8_postgres::tokio_postgres::Error> {
        println!("test");
        Ok(())
    }
}

I want to prepare a number of queries using this for every connection. However, I'm struggling to think of a way to actually get the reference to the prepared statements for a specific connection. Is there a way to bind some data to the connection object, or at least a way to get some unique identifier for a connection that I could use to lookup their prepared statements in a hash table?

djc commented 3 years ago

Maybe you should write your own ManageConnection instead of using bb8-postgres, which can deal with a type that is wrapped around tokio_postgres::Client (and potentially Derefs to it)?

djc commented 3 years ago

(If it's generic enough I'd be happy to accept it into bb8-postgres and maintain it.)