fixed root array queries
This commit is contained in:
@ -63,33 +63,33 @@ impl<'a> Compiler<'a> {
|
||||
}
|
||||
|
||||
fn compile_array(&mut self, node: Node<'a>) -> Result<(String, String), String> {
|
||||
// 1. Array of DB Entities (`$ref` or `$family` pointing to a table limit)
|
||||
if let Some(items) = &node.schema.obj.items {
|
||||
let next_path = node.ast_path.clone();
|
||||
|
||||
if let Some(ref_id) = &items.obj.r#ref {
|
||||
if let Some(type_def) = self.db.types.get(ref_id) {
|
||||
let mut entity_node = node.clone();
|
||||
entity_node.ast_path = next_path;
|
||||
entity_node.schema = std::sync::Arc::clone(items);
|
||||
return self.compile_entity(type_def, entity_node, true);
|
||||
}
|
||||
let mut resolved_type = None;
|
||||
if let Some(family_target) = items.obj.family.as_ref() {
|
||||
let base_type_name = family_target.split('.').next_back().unwrap_or(family_target);
|
||||
resolved_type = self.db.types.get(base_type_name);
|
||||
} else if let Some(base_type_name) = items.obj.identifier() {
|
||||
resolved_type = self.db.types.get(&base_type_name);
|
||||
}
|
||||
|
||||
let mut next_node = node.clone();
|
||||
next_node.depth += 1;
|
||||
next_node.ast_path = next_path;
|
||||
next_node.schema = std::sync::Arc::clone(items);
|
||||
let (item_sql, _) = self.compile_node(next_node)?;
|
||||
if let Some(type_def) = resolved_type {
|
||||
let mut entity_node = node.clone();
|
||||
entity_node.schema = std::sync::Arc::clone(items);
|
||||
return self.compile_entity(type_def, entity_node, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Arrays of mapped Native Postgres Columns (e.g. `jsonb`, `text[]`)
|
||||
if let Some(prop) = &node.property_name {
|
||||
return Ok((
|
||||
format!("(SELECT jsonb_agg({}) FROM TODO)", item_sql),
|
||||
format!("{}.{}", node.parent_alias, prop),
|
||||
"array".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok((
|
||||
"SELECT jsonb_agg(TODO) FROM TODO".to_string(),
|
||||
"array".to_string(),
|
||||
))
|
||||
// 3. Fallback for root execution of standalone non-entity arrays
|
||||
Err("Cannot compile a root array without a valid entity reference or table mapped via `items`.".to_string())
|
||||
}
|
||||
|
||||
fn compile_reference(&mut self, node: Node<'a>) -> Result<(String, String), String> {
|
||||
|
||||
Reference in New Issue
Block a user