Skip to content

Commit

Permalink
feat(driver-adapters): enable Wasm on sql-query-connector (#4444)
Browse files Browse the repository at this point in the history
* feat(quaint): allow wasm32-unknown-unknown compilation; currently fails on native

* feat(quaint): split postgres connector into native and wasm submodules

* feat(quaint): split mysql connector into native and wasm submodules

* feat(quaint): recover wasm error for mysql

* feat(quaint): split mssql connector into native and wasm submodules

* feat(quaint): split sqlite connector into native and wasm submodules

* chore(quaint): fix clippy when compiling natively

* chore(quaint): fix clippy when compiling to wasm32-unknown-unknown

* chore(quaint): update README

* chore(quaint): rename "*-connector" feature flag to "*-native"

* feat(quaint): enable pure Wasm SqliteError

* feat(query-connect): allow wasm32-unknown-unknown compilation

* feat(sql-query-connector): allow wasm32-unknown-unknown compilation

* chore(query-engine-wasm): add currently unused local crates to test wasm32-unknown-unknown compilation

* chore: update Cargo.lock

* chore: remove leftover comments

* chore(sql-query-connector): fix clipppy on wasm32

* WIP: refactor mysql module to flatten its structure

* feat(quaint): flatten mssql connector module

* feat(quaint): flatten postgres connector module

* feat(quaint): flatten sqlite connector module

* chore(quaint): export all public definitions in connector "url" modules

* chore(quaint): refactor tests for connectors, addressing feedback

* chore: add comment on MysqlAsyncError

* chore: add comment on ffi.rs for sqlite

* chore: replace awkward "super::super::" with "crate::..."

* fix(driver-adapters): ci for "request-handlers"

---------

Co-authored-by: Miguel Fernandez <[email protected]>
  • Loading branch information
jkomyno and miguelff authored Nov 17, 2023
1 parent aaed047 commit efbc865
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions query-engine/connectors/sql-query-connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ version = "0.1.0"

[features]
vendored-openssl = ["quaint/vendored-openssl"]

# Enable Driver Adapters
driver-adapters = []

[dependencies]
Expand All @@ -18,15 +20,20 @@ once_cell = "1.3"
rand = "0.7"
serde_json = {version = "1.0", features = ["float_roundtrip"]}
thiserror = "1.0"
tokio.workspace = true
tokio = { version = "1.0", features = ["macros", "time"] }
tracing = "0.1"
tracing-futures = "0.2"
uuid.workspace = true
opentelemetry = { version = "0.17", features = ["tokio"] }
tracing-opentelemetry = "0.17.3"
quaint.workspace = true
cuid = { git = "https://github.com/prisma/cuid-rust", branch = "wasm32-support" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
quaint.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
quaint = { path = "../../../quaint" }

[dependencies.connector-interface]
package = "query-connector"
path = "../query-connector"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(target_arch = "wasm32", allow(dead_code))]

use super::{catch, transaction::SqlConnectorTransaction};
use crate::{database::operations::*, Context, SqlError};
use async_trait::async_trait;
Expand Down
19 changes: 11 additions & 8 deletions query-engine/connectors/sql-query-connector/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
mod connection;
#[cfg(feature = "driver-adapters")]
mod js;
mod mssql;
mod mysql;
mod postgresql;
mod sqlite;
mod transaction;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod native {
pub(crate) mod mssql;
pub(crate) mod mysql;
pub(crate) mod postgresql;
pub(crate) mod sqlite;
}

pub(crate) mod operations;

use async_trait::async_trait;
use connector_interface::{error::ConnectorError, Connector};

#[cfg(feature = "driver-adapters")]
pub use js::*;
pub use mssql::*;
pub use mysql::*;
pub use postgresql::*;
pub use sqlite::*;

#[cfg(not(target_arch = "wasm32"))]
pub use native::{mssql::*, mysql::*, postgresql::*, sqlite::*};

#[async_trait]
pub trait FromSource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::connection::SqlConnection;
use crate::database::{catch, connection::SqlConnection};
use crate::{FromSource, SqlError};
use async_trait::async_trait;
use connector_interface::{
Expand Down Expand Up @@ -60,7 +60,7 @@ impl FromSource for Mssql {
#[async_trait]
impl Connector for Mssql {
async fn get_connection<'a>(&'a self) -> connector::Result<Box<dyn Connection + Send + Sync + 'static>> {
super::catch(self.connection_info.clone(), async move {
catch(self.connection_info.clone(), async move {
let conn = self.pool.check_out().await.map_err(SqlError::from)?;
let conn = SqlConnection::new(conn, &self.connection_info, self.features);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::connection::SqlConnection;
use crate::database::{catch, connection::SqlConnection};
use crate::{FromSource, SqlError};
use async_trait::async_trait;
use connector_interface::{
Expand Down Expand Up @@ -65,7 +65,7 @@ impl FromSource for Mysql {
#[async_trait]
impl Connector for Mysql {
async fn get_connection<'a>(&'a self) -> connector::Result<Box<dyn Connection + Send + Sync + 'static>> {
super::catch(self.connection_info.clone(), async move {
catch(self.connection_info.clone(), async move {
let runtime_conn = self.pool.check_out().await?;

// Note: `runtime_conn` must be `Sized`, as that's required by `TransactionCapable`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::connection::SqlConnection;
use crate::database::{catch, connection::SqlConnection};
use crate::{FromSource, SqlError};
use async_trait::async_trait;
use connector_interface::{
Expand Down Expand Up @@ -67,7 +67,7 @@ impl FromSource for PostgreSql {
#[async_trait]
impl Connector for PostgreSql {
async fn get_connection<'a>(&'a self) -> connector_interface::Result<Box<dyn Connection + Send + Sync + 'static>> {
super::catch(self.connection_info.clone(), async move {
catch(self.connection_info.clone(), async move {
let conn = self.pool.check_out().await.map_err(SqlError::from)?;
let conn = SqlConnection::new(conn, &self.connection_info, self.features);
Ok(Box::new(conn) as Box<dyn Connection + Send + Sync + 'static>)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::connection::SqlConnection;
use crate::database::{catch, connection::SqlConnection};
use crate::{FromSource, SqlError};
use async_trait::async_trait;
use connector_interface::{
Expand Down Expand Up @@ -80,7 +80,7 @@ fn invalid_file_path_error(file_path: &str, connection_info: &ConnectionInfo) ->
#[async_trait]
impl Connector for Sqlite {
async fn get_connection<'a>(&'a self) -> connector::Result<Box<dyn Connection + Send + Sync + 'static>> {
super::catch(self.connection_info().clone(), async move {
catch(self.connection_info().clone(), async move {
let conn = self.pool.check_out().await.map_err(SqlError::from)?;
let conn = SqlConnection::new(conn, self.connection_info(), self.features);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@ use std::{
ops::Deref,
usize,
};
use tracing::log::trace;
use user_facing_errors::query_engine::DatabaseConstraint;

#[cfg(target_arch = "wasm32")]
macro_rules! trace {
(target: $target:expr, $($arg:tt)+) => {{
// No-op in WebAssembly
}};
($($arg:tt)+) => {{
// No-op in WebAssembly
}};
}

#[cfg(not(target_arch = "wasm32"))]
macro_rules! trace {
(target: $target:expr, $($arg:tt)+) => {
tracing::log::trace!(target: $target, $($arg)+);
};
($($arg:tt)+) => {
tracing::log::trace!($($arg)+);
};
}

async fn generate_id(
conn: &dyn Queryable,
id_field: &FieldSelection,
Expand Down
5 changes: 4 additions & 1 deletion query-engine/connectors/sql-query-connector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ mod value_ext;
use self::{column_metadata::*, context::Context, query_ext::QueryExt, row::*};
use quaint::prelude::Queryable;

pub use database::FromSource;
#[cfg(feature = "driver-adapters")]
pub use database::{activate_driver_adapter, Js};
pub use database::{FromSource, Mssql, Mysql, PostgreSql, Sqlite};
pub use error::SqlError;

#[cfg(not(target_arch = "wasm32"))]
pub use database::{Mssql, Mysql, PostgreSql, Sqlite};

type Result<T> = std::result::Result<T, error::SqlError>;
3 changes: 3 additions & 0 deletions query-engine/query-engine-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ async-trait = "0.1"
user-facing-errors = { path = "../../libs/user-facing-errors" }
psl.workspace = true
prisma-models = { path = "../prisma-models" }
quaint = { path = "../../quaint" }
connector = { path = "../connectors/query-connector", package = "query-connector" }
sql-query-connector = { path = "../connectors/sql-query-connector" }

thiserror = "1"
connection-string.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl SqlSchemaCalculatorFlavour for MssqlFlavour {
}
}

fn push_connector_data(&self, context: &mut super::super::Context<'_>) {
fn push_connector_data(&self, context: &mut crate::sql_schema_calculator::Context<'_>) {
let mut data = MssqlSchemaExt::default();

for model in context.datamodel.db.walk_models() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl SqlSchemaCalculatorFlavour for PostgresFlavour {
}
}

fn push_connector_data(&self, context: &mut super::super::Context<'_>) {
fn push_connector_data(&self, context: &mut crate::sql_schema_calculator::Context<'_>) {
let mut postgres_ext = PostgresSchemaExt::default();
let db = &context.datamodel.db;

Expand Down

0 comments on commit efbc865

Please sign in to comment.