SyncConnectionWrapper

Type Alias SyncConnectionWrapper 

Source
pub type SyncConnectionWrapper<C, B = Tokio> = SyncConnectionWrapper<C, B>;
Available on crate features 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:

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>

Source

pub async fn immediate_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a, E: From<Error> + Send + 'a, R: Send + 'a,

Available on crate feature 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()).await
Source

pub async fn exclusive_transaction<'a, R, E, F>(&mut self, f: F) -> Result<R, E>
where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a, E: From<Error> + Send + 'a, R: Send + 'a,

Available on crate feature 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