-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for wrapping PostgresConnectionManager #112
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be helpful, so thanks for putting it together!
With some style tweaks, I'm happy to merge this.
With this example we show how easy it is to wrap the postgres connection pool to prepare several queries when a connection is created. Then, once a connection is checked out, this examples shows how easy it is to use the custom state to pull a prepared statement. Inspired by djc#110 which requests this kind of behavior in bb8 directly, but for now it's easy to extend any connection manager with your custom needs.
It's actually not alphabetic order but top-down order. But in this case they somewhat coincide. |
I'm trying to do something similar to this, but it doesn't seem like it works. For me at least, |
I recommend you start from this exact example, which is compile checked in CI and thus should work. |
Ah, it looks like the connection having a type of let connection: CustomPostgresConnection = pool.get().await.expect("pool error"); So my issue seems to be more related to #57. It's not clear what type annotations are needed so bb8 pooled connections can be passed to a function. This for example fails with:
async fn run_query<M: bb8::ManageConnection>(connection: PooledConnection<'_, M>) {
connection.query("", &[]).await.unwrap();
} Sorry if this has been asked somewhere else, but if it has I can't find it. |
Your code sample presupposes the existence of a |
Right, so how do I do that? 😅 The postgres glue code isn't implementing a trait from the tokio_postgres crate so it's not clear how these function calls are passed through to the postgres connection as it is. Edit: so, this does work. But now every function call (I have many) is specifying async fn run_query(connection: PooledConnection<'_, CustomPostgresConnectionManager<NoTls>>) {
connection.query("", &[]).await.unwrap();
} |
Make it abstract over |
I tried multiple versions of that but couldn't work around this error:
I also tried defining a type like #57 (comment) but ran into the same error. |
Add a |
Yes I don't have much experience with Rust, but this is a basic usability / documentation issue with bb8. I've tried multiple variations here, including adding Re: posting this elsewhere, I doubt anyone's going to put in the time to meaningfully help if they're not already familiar with this project. |
With this example we show how easy it is to wrap the postgres connection
pool to prepare several queries when a connection is created. Then, once
a connection is checked out, this examples shows how easy it is to use
the custom state to pull a prepared statement.
Inspired by #110 which requests this kind of behavior in bb8 directly,
but for now it's easy to extend any connection manager with custom
needs.
Happy hacktoberfest!