Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-prisma committed Jan 17, 2025
1 parent 8ff95d9 commit f81541a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions query-compiler/query-compiler-wasm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ impl QueryCompiler {
}

#[wasm_bindgen]
pub fn compile(
&self,
request: String,
_human_readable: bool, // ignored on wasm to not compile it in
) -> Result<String, wasm_bindgen::JsError> {
pub fn compile(&self, request: String) -> Result<String, wasm_bindgen::JsError> {
let request = RequestBody::try_from_str(&request, self.protocol)?;
let query_doc = request.into_doc(&self.schema)?;
let plan = query_compiler::compile(&self.schema, query_doc, &self.connection_info)?;
Expand Down
2 changes: 1 addition & 1 deletion query-engine/driver-adapters/executor/src/demo-qc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { err } from "./utils";
import { setupDriverAdaptersManager } from "./setup";

/**
* Example run: `DRIVER_ADAPTER="libsql" pnpm demo:se`
* Example run: `DRIVER_ADAPTER="libsql" pnpm demo:qc`
*/
async function main(): Promise<void> {
const env = S.decodeUnknownSync(Env)(process.env);
Expand Down
1 change: 1 addition & 0 deletions query-engine/query-engine-node-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ query-core = { path = "../core", features = ["metrics"] }
request-handlers = { path = "../request-handlers", features = ["all"] }
query-connector = { path = "../connectors/query-connector" }
query-engine-common = { path = "../../libs/query-engine-common" }
query-compiler = { path = "../../query-compiler/query-compiler" }
user-facing-errors = { path = "../../libs/user-facing-errors" }
telemetry = { path = "../../libs/telemetry" }
psl = { workspace = true, features = ["all"] }
Expand Down
46 changes: 45 additions & 1 deletion query-engine/query-engine-node-api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use napi::{threadsafe_function::ThreadSafeCallContext, Env, JsFunction, JsObject
use napi_derive::napi;
use prisma_metrics::{MetricFormat, WithMetricsInstrumentation};
use psl::PreviewFeature;
use quaint::connector::ExternalConnector;
use quaint::{connector::ExternalConnector, prelude::ConnectionInfo};
use query_core::{protocol::EngineProtocol, relation_load_strategy, schema, TransactionOptions, TxId};
use query_engine_common::{
engine::{
Expand Down Expand Up @@ -354,6 +354,50 @@ impl QueryEngine {
.await
}

#[napi]
pub async fn compile(&self, request: String, human_readable: bool) -> napi::Result<String> {
let dispatcher = self.logger.dispatcher();
let recorder = self.logger.recorder();

async_panic_to_js_error(async {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;

let request = RequestBody::try_from_str(&request, engine.engine_protocol())?;
let query_doc = request
.into_doc(engine.query_schema())
.map_err(|err| napi::Error::from_reason(err.to_string()))?;

let connection_info = match self.connector_mode {
ConnectorMode::Js { ref adapter } => ConnectionInfo::External(
adapter
.get_connection_info()
.await
.map_err(|err| napi::Error::from_reason(err.to_string()))?,
),
ConnectorMode::Rust => {
return Err(napi::Error::from_reason(
"Query compiler requires JS driver adapter".to_string(),
))
}
};

let plan = query_compiler::compile(engine.query_schema(), query_doc, &connection_info)
.map_err(|err| napi::Error::from_reason(err.to_string()))?;

let response = if human_readable {
plan.to_string()
} else {
serde_json::to_string(&plan)?
};

Ok(response)
})
.with_subscriber(dispatcher)
.with_optional_recorder(recorder)
.await
}

/// If connected, attempts to start a transaction in the core and returns its ID.
#[napi]
pub async fn start_transaction(&self, input: String, trace: String, request_id: String) -> napi::Result<String> {
Expand Down

0 comments on commit f81541a

Please sign in to comment.