djc / bb8

Full-featured async (tokio-based) postgres connection pool (like r2d2)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to store and use values made during on_acquire?

mu-arch opened this issue · comments

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?

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)?

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