-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(js connectors): JSON creation,
conversion::conv_params
à la `my…
…sql` (#4184) * feat(js-connectors): use one schema per database provider * fix(js-connectors): fix JSON creation, add missing "conversion::conv_params", similarly to mysql's * chore: fix typo. * chore: add clarification comment
- Loading branch information
Showing
7 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use napi::bindgen_prelude::{FromNapiValue, ToNapiValue}; | ||
use quaint::ast::Value as QuaintValue; | ||
use serde::Serialize; | ||
use serde_json::value::Value as JsonValue; | ||
|
||
#[derive(Debug, Serialize)] | ||
#[serde(untagged)] | ||
pub enum JSArg { | ||
RawString(String), | ||
Value(serde_json::Value), | ||
} | ||
|
||
impl From<JsonValue> for JSArg { | ||
fn from(v: JsonValue) -> Self { | ||
JSArg::Value(v) | ||
} | ||
} | ||
|
||
// FromNapiValue is the napi equivalent to serde::Deserialize. | ||
// Note: we can safely leave this unimplemented as we don't need deserialize JSArg back to napi_value | ||
// (nor we need to). However, removing this altogether would cause a compile error. | ||
impl FromNapiValue for JSArg { | ||
unsafe fn from_napi_value(_env: napi::sys::napi_env, _napi_value: napi::sys::napi_value) -> napi::Result<Self> { | ||
unreachable!() | ||
} | ||
} | ||
|
||
// ToNapiValue is the napi equivalent to serde::Serialize. | ||
impl ToNapiValue for JSArg { | ||
unsafe fn to_napi_value(env: napi::sys::napi_env, value: Self) -> napi::Result<napi::sys::napi_value> { | ||
match value { | ||
JSArg::RawString(s) => ToNapiValue::to_napi_value(env, s), | ||
JSArg::Value(v) => ToNapiValue::to_napi_value(env, v), | ||
} | ||
} | ||
} | ||
|
||
pub fn conv_params(params: &[QuaintValue<'_>]) -> serde_json::Result<Vec<JSArg>> { | ||
let mut values = Vec::with_capacity(params.len()); | ||
|
||
for pv in params { | ||
let res = match pv { | ||
QuaintValue::Json(s) => match s { | ||
Some(ref s) => { | ||
let json_str = serde_json::to_string(s)?; | ||
JSArg::RawString(json_str) | ||
} | ||
None => JsonValue::Null.into(), | ||
}, | ||
quaint_value => { | ||
let json: JsonValue = quaint_value.clone().into(); | ||
json.into() | ||
} | ||
}; | ||
|
||
values.push(res); | ||
} | ||
|
||
Ok(values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
//! | ||
|
||
mod async_js_function; | ||
mod conversion; | ||
mod error; | ||
mod proxy; | ||
mod queryable; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters