diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index 4107921f2f..d866c69f71 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -255,16 +255,7 @@ impl Library2022 { "fastn-apps" => processor::apps::process(value, kind, doc, self), "is-reader" => processor::user_group::is_reader(value, kind, doc, self).await, "sql" => processor::sql::process(value, kind, doc, self).await, - "package-query" => { - processor::sqlite::process( - value, - kind, - doc, - self, - &fastn_core::library2022::processor::sql::get_db_config()?, - ) - .await - } + "package-query" => processor::package_query::process(value, kind, doc, self).await, "pg" => processor::pg::process(value, kind, doc).await, "package-tree" => { processor::package_tree::process(value, kind, doc, &self.config).await diff --git a/fastn-core/src/library2022/processor/mod.rs b/fastn-core/src/library2022/processor/mod.rs index d9e21caeed..2f4fdd08bc 100644 --- a/fastn-core/src/library2022/processor/mod.rs +++ b/fastn-core/src/library2022/processor/mod.rs @@ -5,12 +5,13 @@ pub(crate) mod figma_tokens; pub(crate) mod figma_typography_tokens; pub(crate) mod get_data; pub(crate) mod http; +pub(crate) mod package_query; pub(crate) mod package_tree; pub(crate) mod pg; pub(crate) mod query; pub(crate) mod request_data; pub(crate) mod sitemap; -pub mod sql; +pub(crate) mod sql; pub(crate) mod sqlite; pub(crate) mod toc; pub(crate) mod user_details; diff --git a/fastn-core/src/library2022/processor/package_query.rs b/fastn-core/src/library2022/processor/package_query.rs new file mode 100644 index 0000000000..537b499fd8 --- /dev/null +++ b/fastn-core/src/library2022/processor/package_query.rs @@ -0,0 +1,61 @@ +pub async fn process( + value: ftd::ast::VariableValue, + kind: ftd::interpreter::Kind, + doc: &ftd::interpreter::TDoc<'_>, + req_config: &fastn_core::RequestConfig, +) -> ftd::interpreter::Result { + let (headers, query) = + fastn_core::library2022::processor::sqlite::get_p1_data("package-data", &value, doc.name)?; + + fastn_core::library2022::utils::log_deprecation_warning( + "`package-query` has been deprecated, use `sql` processor instead.", + ); + + let sqlite_database = + match headers.get_optional_string_by_key("db", doc.name, value.line_number())? { + Some(k) => k, + None => { + return ftd::interpreter::utils::e2( + "`db` is not specified".to_string(), + doc.name, + value.line_number(), + ) + } + }; + + let sqlite_database_path = req_config.config.root.join(sqlite_database.as_str()); + + if !sqlite_database_path.exists() { + return ftd::interpreter::utils::e2( + "`db` does not exists for package-query processor".to_string(), + doc.name, + value.line_number(), + ); + } + + let query_response = fastn_core::library2022::processor::sqlite::execute_query( + &sqlite_database_path, + query.as_str(), + doc, + headers, + value.line_number(), + ) + .await; + + match query_response { + Ok(result) => fastn_core::library2022::processor::sqlite::result_to_value( + Ok(result), + kind, + doc, + &value, + super::sql::STATUS_OK, + ), + Err(e) => fastn_core::library2022::processor::sqlite::result_to_value( + Err(e.to_string()), + kind, + doc, + &value, + super::sql::STATUS_ERROR, + ), + } +} diff --git a/fastn-core/src/library2022/processor/sqlite.rs b/fastn-core/src/library2022/processor/sqlite.rs index 91a7e6e8fe..a7723aa96e 100644 --- a/fastn-core/src/library2022/processor/sqlite.rs +++ b/fastn-core/src/library2022/processor/sqlite.rs @@ -28,10 +28,11 @@ pub async fn process( value: ftd::ast::VariableValue, kind: ftd::interpreter::Kind, doc: &ftd::interpreter::TDoc<'_>, - config: &fastn_core::RequestConfig, + req_config: &fastn_core::RequestConfig, db_config: &fastn_core::library2022::processor::sql::DatabaseConfig, ) -> ftd::interpreter::Result { let (headers, query) = get_p1_data("package-data", &value, doc.name)?; + let sqlite_database_path = req_config.config.root.join(&db_config.db_url); // need the query params // question is they can be multiple @@ -41,9 +42,14 @@ pub async fn process( // for now they wil be ordered // select * from users where - let db_path = config.config.root.join(&db_config.db_url); - let query_response = - execute_query(&db_path, query.as_str(), doc, headers, value.line_number()).await; + let query_response = execute_query( + &sqlite_database_path, + query.as_str(), + doc, + headers, + value.line_number(), + ) + .await; match query_response { Ok(result) => result_to_value(Ok(result), kind, doc, &value, super::sql::STATUS_OK), @@ -286,7 +292,7 @@ fn extract_named_parameters( Ok(params) } -async fn execute_query( +pub(crate) async fn execute_query( database_path: &camino::Utf8PathBuf, query: &str, doc: &ftd::interpreter::TDoc<'_>, diff --git a/fastn-core/src/library2022/utils.rs b/fastn-core/src/library2022/utils.rs index d91b49e0fe..21f9872ff1 100644 --- a/fastn-core/src/library2022/utils.rs +++ b/fastn-core/src/library2022/utils.rs @@ -14,3 +14,9 @@ pub fn document_full_id( Ok(format!("/{}/", full_document_id.trim_matches('/'))) } + +pub fn log_deprecation_warning(message: &str) { + use colored::Colorize; + + println!("{}", format!("Warning: {}", message).bright_yellow()); +}