From 653fbd40602f72fa5beb86d4f8e73d8427ed683b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Houl=C3=A9?= <13155277+tomhoule@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:14:36 +0100 Subject: [PATCH] Revert "query-engine-node-api: delete getConfig() (#3608)" (#3619) We deleted getConfig from the node api builds of the query-engine for a 30kb gain with the assumption that it wasn't used at all anymore in the CLI. It is not the case. This reverts commit 7e6991a472f700dcea04ffab5800ea589abcf097. --- .../query-engine-node-api/src/functions.rs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/query-engine/query-engine-node-api/src/functions.rs b/query-engine/query-engine-node-api/src/functions.rs index 77772b51a947..0c7c38ae971f 100644 --- a/query-engine/query-engine-node-api/src/functions.rs +++ b/query-engine/query-engine-node-api/src/functions.rs @@ -1,8 +1,12 @@ use crate::error::ApiError; +use napi::{bindgen_prelude::*, JsUnknown}; use napi_derive::napi; use query_core::{schema::QuerySchemaRef, schema_builder}; use request_handlers::dmmf; -use std::sync::Arc; +use std::{ + collections::{BTreeMap, HashMap}, + sync::Arc, +}; #[derive(serde::Serialize, Clone, Copy)] #[napi(object)] @@ -35,6 +39,43 @@ pub fn dmmf(datamodel_string: String) -> napi::Result { Ok(serde_json::to_string(&dmmf)?) } +#[napi] +pub fn get_config(js_env: Env, options: JsUnknown) -> napi::Result { + #[derive(serde::Deserialize)] + #[serde(rename_all = "camelCase")] + struct GetConfigOptions { + datamodel: String, + #[serde(default)] + ignore_env_var_errors: bool, + #[serde(default)] + datasource_overrides: BTreeMap, + #[serde(default)] + env: HashMap, + } + + let options: GetConfigOptions = js_env.from_js_value(options)?; + + let GetConfigOptions { + datamodel, + ignore_env_var_errors, + datasource_overrides, + env, + } = options; + + let overrides: Vec<(_, _)> = datasource_overrides.into_iter().collect(); + let mut config = psl::parse_configuration(&datamodel).map_err(|errors| ApiError::conversion(errors, &datamodel))?; + + if !ignore_env_var_errors { + config + .resolve_datasource_urls_from_env(&overrides, |key| env.get(key).map(ToString::to_string)) + .map_err(|errors| ApiError::conversion(errors, &datamodel))?; + } + + let serialized = psl::get_config::config_to_mcf_json_value(&config); + + js_env.to_js_value(&serialized) +} + #[napi] pub fn debug_panic(panic_message: Option) -> napi::Result<()> { let user_facing = user_facing_errors::Error::from_panic_payload(Box::new(