Skip to content

Commit

Permalink
backwards compatibility for package-query (#1460)
Browse files Browse the repository at this point in the history
* backwards compatibility for package-query

* refactored code and added deprecation message

* removed comments

* minor changes

* applied recommended changes
  • Loading branch information
harshdoesdev authored Nov 7, 2023
1 parent cde85e8 commit 3eb6f6a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
11 changes: 1 addition & 10 deletions fastn-core/src/library2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion fastn-core/src/library2022/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions fastn-core/src/library2022/processor/package_query.rs
Original file line number Diff line number Diff line change
@@ -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<ftd::interpreter::Value> {
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,
),
}
}
16 changes: 11 additions & 5 deletions fastn-core/src/library2022/processor/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ftd::interpreter::Value> {
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
Expand All @@ -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),
Expand Down Expand Up @@ -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<'_>,
Expand Down
6 changes: 6 additions & 0 deletions fastn-core/src/library2022/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit 3eb6f6a

Please sign in to comment.