pub type AsyncConnectionWrapper<C, B = Tokio> = AsyncConnectionWrapper<C, B>;
Expand description
A helper type that wraps an AsyncConnection
to
provide a sync diesel::Connection
implementation.
Internally this wrapper type will use block_on
to wait for
the execution of futures from the inner connection. This implies you
cannot use functions of this type in a scope with an already existing
tokio runtime. If you are in a situation where you want to use this
connection wrapper in the scope of an existing tokio runtime (for example
for running migrations via diesel_migration
) you need to wrap
the relevant code block into a tokio::task::spawn_blocking
task.
§Examples
use schema::users;
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
use diesel::prelude::{RunQueryDsl, Connection};
let mut conn = AsyncConnectionWrapper::<DbConnection>::establish(&database_url)?;
let all_users = users::table.load::<(i32, String)>(&mut conn)?;
If you are in the scope of an existing tokio runtime you need to use
tokio::task::spawn_blocking
to encapsulate the blocking tasks
use schema::users;
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
async fn some_async_fn() {
// need to use `spawn_blocking` to execute
// a blocking task in the scope of an existing runtime
let res = tokio::task::spawn_blocking(move || {
use diesel::prelude::{RunQueryDsl, Connection};
let mut conn = AsyncConnectionWrapper::<DbConnection>::establish(&database_url)?;
let all_users = users::table.load::<(i32, String)>(&mut conn)?;
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(())
}).await;
}
Aliased Type§
struct AsyncConnectionWrapper<C, B = Tokio> { /* private fields */ }