pub type SyncConnectionWrapper<C, B = Tokio> = SyncConnectionWrapper<C, B>;sync-connection-wrapper and tokio only.Expand description
A wrapper of a diesel::connection::Connection usable in async context.
It implements AsyncConnection if diesel::connection::Connection fullfils requirements:
- it’s a
diesel::connection::LoadConnection - its
diesel::connection::Connection::Backendhas adiesel::query_builder::BindCollectorimplementingdiesel::query_builder::MoveableBindCollector - its
diesel::connection::LoadConnection::Rowimplementsdiesel::row::IntoOwnedRow
Internally this wrapper type will use spawn_blocking on tokio
to execute the request on the inner connection. This implies a
dependency on tokio and that the runtime is running.
Note that only SQLite is supported at the moment.
§Examples
use diesel_async::RunQueryDsl;
use schema::users;
async fn some_async_fn() {
use diesel_async::AsyncConnection;
use diesel::sqlite::SqliteConnection;
let mut conn =
SyncConnectionWrapper::<SqliteConnection>::establish(&database_url).await.unwrap();
let all_users = users::table.load::<(i32, String)>(&mut conn).await.unwrap();
}
Aliased Type§
pub struct SyncConnectionWrapper<C, B = Tokio> { /* private fields */ }Implementations§
Source§impl SyncConnectionWrapper<SqliteConnection>
impl SyncConnectionWrapper<SqliteConnection>
Sourcepub async fn immediate_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
Available on crate feature sqlite only.
pub async fn immediate_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
sqlite only.Run a transaction with BEGIN IMMEDIATE
This method will return an error if a transaction is already open.
WARNING: Canceling the returned future does currently not close an already open transaction. You may end up with a connection containing a dangling transaction.
§Example
use diesel::result::Error;
use scoped_futures::ScopedFutureExt;
use diesel_async::{RunQueryDsl, AsyncConnection};
conn.immediate_transaction(|conn| async move {
diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn)
.await?;
let all_names = users.select(name).load::<String>(conn).await?;
assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);
Ok(())
}.scope_boxed()).awaitSourcepub async fn exclusive_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
Available on crate feature sqlite only.
pub async fn exclusive_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
sqlite only.Run a transaction with BEGIN EXCLUSIVE
This method will return an error if a transaction is already open.
WARNING: Canceling the returned future does currently not close an already open transaction. You may end up with a connection containing a dangling transaction.
§Example
use diesel::result::Error;
use scoped_futures::ScopedFutureExt;
use diesel_async::{RunQueryDsl, AsyncConnection};
conn.exclusive_transaction(|conn| async move {
diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn)
.await?;
let all_names = users.select(name).load::<String>(conn).await?;
assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);
Ok(())
}.scope_boxed()).await