Skip to content

Commit

Permalink
Couple of fixes
Browse files Browse the repository at this point in the history
1. Build schema and connect sequentually
2. Print full stacktrace for WASM error
3. Expand example to attempt a query
  • Loading branch information
SevInf committed Nov 22, 2023
1 parent d054114 commit a004fe6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 53 deletions.
10 changes: 6 additions & 4 deletions query-engine/driver-adapters/src/wasm/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use js_sys::Reflect;
use quaint::error::Error as QuaintError;
use wasm_bindgen::JsValue;

type WasmError = JsValue;

/// transforms a Wasm error into a Quaint error
pub(crate) fn into_quaint_error(wasm_err: WasmError) -> QuaintError {
pub(crate) fn into_quaint_error(wasm_err: JsValue) -> QuaintError {
let status = "WASM_ERROR".to_string();
let reason = wasm_err.as_string().unwrap_or_else(|| "unknown error".to_string());
let reason = Reflect::get(&wasm_err, &JsValue::from_str("stack"))
.ok()
.and_then(|value| value.as_string())
.unwrap_or_else(|| "Unknown error".to_string());
QuaintError::raw_connector_error(status, reason)
}
17 changes: 16 additions & 1 deletion query-engine/query-engine-wasm/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Pool } from '@neondatabase/serverless'
import { PrismaNeon } from '@prisma/adapter-neon'
import { bindAdapter } from '@prisma/driver-adapter-utils'
import { init, QueryEngine, getBuildTimeInfo } from './pkg/query_engine_wasm.js'
import { init, QueryEngine, getBuildTimeInfo } from './pkg/query_engine.js'

async function main() {
// Always initialize the Wasm library before using it.
Expand Down Expand Up @@ -48,7 +48,22 @@ async function main() {
const queryEngine = new QueryEngine(options, callback, driverAdapter)

await queryEngine.connect('trace')
const res = await queryEngine.query(JSON.stringify({
modelName: 'User',
action: 'findMany',
query: {
arguments: {},
selection: {
$scalars: true
}
}
}), 'trace')
const parsed = JSON.parse(res);
console.log('query result = ', parsed)
await queryEngine.disconnect('trace')
console.log('after disconnect')
queryEngine.free()
await driverAdapter.close()
}

main()
22 changes: 11 additions & 11 deletions query-engine/query-engine-wasm/package-lock.json

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

69 changes: 32 additions & 37 deletions query-engine/query-engine-wasm/src/wasm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,24 @@ impl QueryEngine {

let preview_features = arced_schema.configuration.preview_features();

let executor = async {
let executor = load_executor(self.connector_mode, data_source, preview_features, &url).await?;
let connector = executor.primary_connector();
let executor = load_executor(self.connector_mode, data_source, preview_features, &url).await?;
let connector = executor.primary_connector();

let conn_span = tracing::info_span!(
"prisma:engine:connection",
user_facing = true,
"db.type" = connector.name(),
);
let conn_span = tracing::info_span!(
"prisma:engine:connection",
user_facing = true,
"db.type" = connector.name(),
);

connector.get_connection().instrument(conn_span).await?;
connector.get_connection().instrument(conn_span).await?;

crate::Result::<_>::Ok(executor)
}
.await;

let query_schema = {
let enable_raw_queries = true;
schema::build(arced_schema_2, enable_raw_queries)
};
let query_schema_span = tracing::info_span!("prisma:engine:schema");
let query_schema = query_schema_span.in_scope(|| schema::build(arced_schema_2, true));

Ok(ConnectedEngine {
schema: builder.schema.clone(),
query_schema: Arc::new(query_schema),
executor: executor?,
executor,
config_dir: builder.config_dir.clone(),
env: builder.env.clone(),
engine_protocol: builder.engine_protocol,
Expand All @@ -285,30 +278,32 @@ impl QueryEngine {
/// Disconnect and drop the core. Can be reconnected later with `#connect`.
#[wasm_bindgen]
pub async fn disconnect(&self, trace: String) -> Result<(), wasm_bindgen::JsError> {
async_panic_to_js_error(async {
let span = tracing::info_span!("prisma:engine:disconnect");
// async_panic_to_js_error(async {
// let span = tracing::info_span!("prisma:engine:disconnect");

// TODO: when using Node Drivers, we need to call Driver::close() here.
// TODO: when using Node Drivers, we need to call Driver::close() here.

async {
let mut inner = self.inner.write().await;
let engine = inner.as_engine()?;
// async {
let mut inner = self.inner.write().await;
let engine = inner.as_engine()?;

let builder = EngineBuilder {
schema: engine.schema.clone(),
config_dir: engine.config_dir.clone(),
env: engine.env.clone(),
engine_protocol: engine.engine_protocol(),
};
let builder = EngineBuilder {
schema: engine.schema.clone(),
config_dir: engine.config_dir.clone(),
env: engine.env.clone(),
engine_protocol: engine.engine_protocol(),
};

*inner = Inner::Builder(builder);
log::info!("Recreated builder");
*inner = Inner::Builder(builder);
log::info!("Recreated inner builder");

Ok(())
}
.instrument(span)
.await
})
.await
Ok(())
// }
// .instrument(span)
// .await
// })
// .await
}

/// If connected, sends a query to the core and returns the response.
Expand Down

0 comments on commit a004fe6

Please sign in to comment.