diff --git a/src/ts_generator/sql_parser/translate_insert.rs b/src/ts_generator/sql_parser/translate_insert.rs index 7651bb3b..50f6f1d6 100644 --- a/src/ts_generator/sql_parser/translate_insert.rs +++ b/src/ts_generator/sql_parser/translate_insert.rs @@ -70,7 +70,7 @@ pub async fn translate_insert( if value.to_string() == "?" { // If the placeholder is `'?'`, we can process it using insert_value_params and generate nested params type - ts_query.insert_value_params(&field.field_type, &(row, column), &placeholder); + ts_query.insert_value_params(&field.field_type, &(row, column), field.is_nullable, &placeholder); } else { ts_query.insert_param(&field.field_type, &field.is_nullable, &placeholder)?; } diff --git a/src/ts_generator/types/ts_query.rs b/src/ts_generator/types/ts_query.rs index 9fb93bef..b31797be 100644 --- a/src/ts_generator/types/ts_query.rs +++ b/src/ts_generator/types/ts_query.rs @@ -208,9 +208,9 @@ pub struct TsQuery { pub annotated_params: BTreeMap, // We use BTreeMap here as it's a collection that's already sorted - pub insert_params: BTreeMap>, + pub insert_params: BTreeMap>>, // Holds any annotated @param and perform replacement when generated TS types - pub annotated_insert_params: BTreeMap>, + pub annotated_insert_params: BTreeMap>>, pub result: HashMap>, // Holds any annotated @result and perform replacement when generating TS types @@ -319,7 +319,13 @@ impl TsQuery { /// /// e.g. /// [ [number, string], [number, string] ] - pub fn insert_value_params(&mut self, value: &TsFieldType, point: &(usize, usize), _placeholder: &Option) { + pub fn insert_value_params( + &mut self, + value: &TsFieldType, + point: &(usize, usize), + is_nullable: bool, + _placeholder: &Option, + ) { let (row, column) = point; let annotated_insert_param = self.annotated_insert_params.get(row); @@ -334,7 +340,11 @@ impl TsQuery { row_params = self.insert_params.get_mut(row); } - row_params.unwrap().insert(*column, value.to_owned()); + let mut types = vec![value.to_owned()]; + if is_nullable { + types.push(TsFieldType::Null); + } + row_params.unwrap().insert(*column, types); } } @@ -397,7 +407,10 @@ impl TsQuery { // Process each row and produce Number, String, Boolean row .values() - .map(|col| col.to_string()) + .map(|col| { + let type_strings = col.iter().map(|t| t.to_string()).collect::>(); + type_strings.join(" | ").to_string() + }) .collect::>() .join(", ") }) diff --git a/tests/demo/typescript/demo.queries.ts b/tests/demo/typescript/demo.queries.ts index 81dfebb4..55e8cb24 100644 --- a/tests/demo/typescript/demo.queries.ts +++ b/tests/demo/typescript/demo.queries.ts @@ -73,7 +73,7 @@ export interface IGetItemsWithRowsQuery { result: IGetItemsWithRowsResult; } -export type TestInsertParams = [[number, string, string]]; +export type TestInsertParams = [[number, string, string | null]]; export interface ITestInsertResult { diff --git a/tests/demo/typescript/demo.snapshot.ts b/tests/demo/typescript/demo.snapshot.ts index 11dd5a49..f77e5bc5 100644 --- a/tests/demo/typescript/demo.snapshot.ts +++ b/tests/demo/typescript/demo.snapshot.ts @@ -73,7 +73,7 @@ export interface IGetItemsWithRowsQuery { result: IGetItemsWithRowsResult; } -export type TestInsertParams = [[number, string, string]]; +export type TestInsertParams = [[number, string, string | null]]; export interface ITestInsertResult { diff --git a/tests/mysql_insert_query_parameters.rs b/tests/mysql_insert_query_parameters.rs index 769205ae..c9655fa4 100644 --- a/tests/mysql_insert_query_parameters.rs +++ b/tests/mysql_insert_query_parameters.rs @@ -54,7 +54,7 @@ VALUES //// Generated TS interfaces //// r#" -export type SomeInputQueryParams = [[number, string], [string, string]]; +export type SomeInputQueryParams = [[number, string], [string | null, string | null]]; export interface ISomeInputQueryResult {