Skip to content

Commit

Permalink
feat: remove ducktor
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Nov 22, 2023
1 parent 9818aee commit d6d09d6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 32 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion query-engine/driver-adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ serde-wasm-bindgen.workspace = true
wasm-bindgen.workspace = true
wasm-bindgen-futures.workspace = true
tsify.workspace = true
ducktor = "0.1.0"
pin-project = "1"
14 changes: 14 additions & 0 deletions query-engine/driver-adapters/src/wasm/async_js_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ where
_phantom_return: PhantomData<ReturnType>,
}

impl<T, R> From<JsFunction> for AsyncJsFunction<T, R>
where
T: Serialize,
R: DeserializeOwned,
{
fn from(js_fn: JsFunction) -> Self {
Self {
threadsafe_fn: js_fn,
_phantom_arg: PhantomData::<T> {},
_phantom_return: PhantomData::<R> {},
}
}
}

impl<T, R> AsyncJsFunction<T, R>
where
T: Serialize,
Expand Down
10 changes: 10 additions & 0 deletions query-engine/driver-adapters/src/wasm/js_object_extern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use js_sys::JsString;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

#[wasm_bindgen]
extern "C" {
pub type JsObjectExtern;

#[wasm_bindgen(method, catch, structural, indexing_getter)]
pub fn get(this: &JsObjectExtern, key: JsString) -> Result<JsValue, JsValue>;
}
3 changes: 3 additions & 0 deletions query-engine/driver-adapters/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
mod async_js_function;
mod conversion;
mod error;
mod js_object_extern;
mod proxy;
mod queryable;
mod send_future;
mod transaction;

pub use js_object_extern::JsObjectExtern;
pub use queryable::{from_wasm, JsQueryable};
39 changes: 27 additions & 12 deletions query-engine/driver-adapters/src/wasm/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use ducktor::FromJsValue as DuckType;
use futures::Future;
use js_sys::{Function as JsFunction, Object as JsObject};
use js_sys::{Function as JsFunction, JsString, Object as JsObject};
use tsify::Tsify;

use super::{async_js_function::AsyncJsFunction, send_future::SendFuture, transaction::JsTransaction};
pub use crate::types::{ColumnType, JSResultSet, Query, TransactionOptions};
use crate::JsObjectExtern;
use metrics::increment_gauge;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};

type JsResult<T> = core::result::Result<T, JsValue>;

/// Proxy is a struct wrapping a javascript object that exhibits basic primitives for
/// querying and executing SQL (i.e. a client connector). The Proxy uses Wasm's JsFunction to
/// invoke the code within the node runtime that implements the client connector.
#[wasm_bindgen(getter_with_clone)]
#[derive(DuckType, Default)]
#[derive(Default)]
pub(crate) struct CommonProxy {
/// Execute a query given as SQL, interpolating the given parameters.
query_raw: AsyncJsFunction<Query, JSResultSet>,
Expand All @@ -29,15 +30,14 @@ pub(crate) struct CommonProxy {
/// This is a JS proxy for accessing the methods specific to top level
/// JS driver objects
#[wasm_bindgen(getter_with_clone)]
#[derive(DuckType)]
pub(crate) struct DriverProxy {
start_transaction: AsyncJsFunction<(), JsTransaction>,
}

/// This a JS proxy for accessing the methods, specific
/// to JS transaction objects
#[wasm_bindgen(getter_with_clone)]
#[derive(DuckType, Default)]
#[derive(Default)]
pub(crate) struct TransactionProxy {
/// transaction options
options: TransactionOptions,
Expand All @@ -54,8 +54,14 @@ pub(crate) struct TransactionProxy {
}

impl CommonProxy {
pub fn new(object: &JsObject) -> Self {
CommonProxy::from(&object.into())
pub fn new(object: &JsObjectExtern) -> JsResult<Self> {
let flavour: String = JsString::from(object.get("value".into())?).into();

Ok(Self {
query_raw: JsFunction::from(object.get("queryRaw".into())?).into(),
execute_raw: JsFunction::from(object.get("executeRaw".into())?).into(),
flavour,
})
}

pub async fn query_raw(&self, params: Query) -> quaint::Result<JSResultSet> {
Expand All @@ -68,8 +74,10 @@ impl CommonProxy {
}

impl DriverProxy {
pub fn new(object: &JsObject) -> Self {
Self::from(&object.into())
pub fn new(object: &JsObjectExtern) -> JsResult<Self> {
Ok(Self {
start_transaction: JsFunction::from(object.get("startTransaction".into())?).into(),
})
}

async fn start_transaction_inner(&self) -> quaint::Result<Box<JsTransaction>> {
Expand All @@ -91,8 +99,15 @@ impl DriverProxy {
}

impl TransactionProxy {
pub fn new(object: &JsObject) -> Self {
Self::from(&object.into())
pub fn new(object: &JsObjectExtern) -> JsResult<Self> {
let options = object.get("options".into())?;

Ok(Self {
options: TransactionOptions::from_js(options).unwrap(),
commit: JsFunction::from(object.get("commit".into())?).into(),
rollback: JsFunction::from(object.get("dispose".into())?).into(),
dispose: object.get("dispose".into())?.into(),
})
}

pub fn options(&self) -> &TransactionOptions {
Expand Down
11 changes: 6 additions & 5 deletions query-engine/driver-adapters/src/wasm/queryable.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::JsObjectExtern;

use super::{
conversion,
proxy::{CommonProxy, DriverProxy, Query},
send_future::SendFuture,
};
use async_trait::async_trait;
use ducktor::FromJsValue as DuckType;
use futures::Future;
use js_sys::Object as JsObject;
use psl::datamodel_connector::Flavour;
Expand All @@ -30,7 +31,7 @@ use wasm_bindgen::prelude::wasm_bindgen;
/// into a `quaint::connector::result_set::ResultSet`. A quaint `ResultSet` is basically a vector
/// of `quaint::Value` but said type is a tagged enum, with non-unit variants that cannot be converted to javascript as is.
#[wasm_bindgen(getter_with_clone)]
#[derive(DuckType, Default)]
#[derive(Default)]
pub(crate) struct JsBaseQueryable {
pub(crate) proxy: CommonProxy,
pub flavour: Flavour,
Expand Down Expand Up @@ -313,9 +314,9 @@ impl TransactionCapable for JsQueryable {
}
}

pub fn from_wasm(driver: JsObject) -> JsQueryable {
let common = CommonProxy::new(&driver);
let driver_proxy = DriverProxy::new(&driver);
pub fn from_wasm(driver: JsObjectExtern) -> JsQueryable {
let common = CommonProxy::new(&driver).unwrap();
let driver_proxy = DriverProxy::new(&driver).unwrap();

JsQueryable {
inner: JsBaseQueryable::new(common),
Expand Down
5 changes: 3 additions & 2 deletions query-engine/query-engine-wasm/src/wasm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{
error::ApiError,
logger::{LogCallback, Logger},
};
use js_sys::{Function as JsFunction, Object as JsObject};
use driver_adapters::JsObjectExtern;
use js_sys::Function as JsFunction;
use request_handlers::ConnectorMode;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -103,7 +104,7 @@ impl QueryEngine {
pub fn new(
options: ConstructorOptions,
callback: JsFunction,
maybe_adapter: Option<JsObject>,
maybe_adapter: Option<JsObjectExtern>,
) -> Result<QueryEngine, wasm_bindgen::JsError> {
log::info!("Called `QueryEngine::new()`");

Expand Down

0 comments on commit d6d09d6

Please sign in to comment.