diff --git a/Cargo.toml b/Cargo.toml index 91c5265..abee693 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,6 @@ futures-util = { version = "0.3.17", default-features = false, features = [ "std", "sink", ] } -tokio-postgres = { version = "0.7.10", optional = true } -tokio = { version = "1.26", optional = true } mysql_async = { version = "0.36.0", optional = true, default-features = false, features = [ "minimal-rust", ] } @@ -36,6 +34,15 @@ deadpool = { version = "0.12", optional = true, default-features = false, featur mobc = { version = ">=0.7,<0.10", optional = true } scoped-futures = { version = "0.1", features = ["std"] } +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tokio-postgres = { version = "0.7.10", optional = true } +tokio = { version = "1.26", optional = true } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +tokio-postgres = { version = "0.7.10", optional = true, default-features = false, features = ["js"] } +tokio = { version = "1.26", optional = true, default-features = false } +wasm-bindgen-futures = { version = "0.4.50" } + [dependencies.diesel] version = "~2.2.0" default-features = false diff --git a/src/lib.rs b/src/lib.rs index 5ae0136..fd21c0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,6 +149,7 @@ pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send { /// The argument to this method and the method's behavior varies by backend. /// See the documentation for that backend's connection class /// for details about what it accepts and how it behaves. + #[cfg(not(target_arch = "wasm32"))] fn establish(database_url: &str) -> impl Future> + Send; /// Executes the given function inside of a database transaction diff --git a/src/mysql/mod.rs b/src/mysql/mod.rs index 6f2321f..b2137e8 100644 --- a/src/mysql/mod.rs +++ b/src/mysql/mod.rs @@ -71,6 +71,7 @@ impl AsyncConnection for AsyncMysqlConnection { type TransactionManager = AnsiTransactionManager; + #[cfg(not(target_arch = "wasm32"))] async fn establish(database_url: &str) -> diesel::ConnectionResult { let mut instrumentation = DynInstrumentation::default_instrumentation(); instrumentation.on_connection_event(InstrumentationEvent::start_establish_connection( diff --git a/src/pg/mod.rs b/src/pg/mod.rs index ce24ba8..ab8c530 100644 --- a/src/pg/mod.rs +++ b/src/pg/mod.rs @@ -29,6 +29,7 @@ use futures_util::TryFutureExt; use futures_util::{Future, FutureExt, StreamExt}; use std::collections::{HashMap, HashSet}; use std::sync::Arc; +use tokio::io::{AsyncRead, AsyncWrite}; use tokio::sync::broadcast; use tokio::sync::oneshot; use tokio::sync::Mutex; @@ -161,6 +162,7 @@ impl AsyncConnection for AsyncPgConnection { type Backend = diesel::pg::Pg; type TransactionManager = AnsiTransactionManager; + #[cfg(not(target_arch = "wasm32"))] async fn establish(database_url: &str) -> ConnectionResult { let mut instrumentation = DynInstrumentation::default_instrumentation(); instrumentation.on_connection_event(InstrumentationEvent::start_establish_connection( @@ -396,12 +398,13 @@ impl AsyncPgConnection { /// Constructs a new `AsyncPgConnection` from an existing [`tokio_postgres::Client`] and /// [`tokio_postgres::Connection`] - pub async fn try_from_client_and_connection( + pub async fn try_from_client_and_connection( client: tokio_postgres::Client, - conn: tokio_postgres::Connection, + conn: tokio_postgres::Connection, ) -> ConnectionResult where - S: tokio_postgres::tls::TlsStream + Unpin + Send + 'static, + S: AsyncRead + AsyncWrite + Unpin + Send + 'static, + T: tokio_postgres::tls::TlsStream + Unpin + Send + 'static, { let (error_rx, shutdown_tx) = drive_connection(conn); @@ -877,18 +880,20 @@ async fn drive_future( } } -fn drive_connection( - conn: tokio_postgres::Connection, +fn drive_connection( + conn: tokio_postgres::Connection, ) -> ( broadcast::Receiver>, oneshot::Sender<()>, ) where - S: tokio_postgres::tls::TlsStream + Unpin + Send + 'static, + S: AsyncRead + AsyncWrite + Unpin + Send + 'static, + T: tokio_postgres::tls::TlsStream + Unpin + Send + 'static, { let (error_tx, error_rx) = tokio::sync::broadcast::channel(1); let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel(); + #[cfg(not(target_arch = "wasm32"))] tokio::spawn(async move { match futures_util::future::select(shutdown_rx, conn).await { Either::Left(_) | Either::Right((Ok(_), _)) => {} @@ -898,6 +903,16 @@ where } }); + #[cfg(target_arch = "wasm32")] + wasm_bindgen_futures::spawn_local(async move { + match futures_util::future::select(shutdown_rx, conn).await { + Either::Left(_) | Either::Right((Ok(_), _)) => {} + Either::Right((Err(e), _)) => { + let _ = error_tx.send(Arc::new(e)); + } + } + }); + (error_rx, shutdown_tx) } diff --git a/src/pooled_connection/mod.rs b/src/pooled_connection/mod.rs index 4674d22..d104271 100644 --- a/src/pooled_connection/mod.rs +++ b/src/pooled_connection/mod.rs @@ -193,6 +193,7 @@ where type TransactionManager = PoolTransactionManager<::TransactionManager>; + #[cfg(not(target_arch = "wasm32"))] async fn establish(_database_url: &str) -> diesel::ConnectionResult { Err(diesel::result::ConnectionError::BadConnection( String::from("Cannot directly establish a pooled connection"), diff --git a/src/sync_connection_wrapper/mod.rs b/src/sync_connection_wrapper/mod.rs index 9f28e5b..6a4ccca 100644 --- a/src/sync_connection_wrapper/mod.rs +++ b/src/sync_connection_wrapper/mod.rs @@ -112,6 +112,7 @@ where type Backend = ::Backend; type TransactionManager = SyncTransactionManagerWrapper<::TransactionManager>; + #[cfg(not(target_arch = "wasm32"))] async fn establish(database_url: &str) -> ConnectionResult { let database_url = database_url.to_string(); tokio::task::spawn_blocking(move || C::establish(&database_url))