implement more cases of nested disconnect
@@ -4,8 +4,9 @@ use crate::{
query_graph::{Node, NodeRef, QueryGraph, QueryGraphDependency},
ParsedInputValue,
};
-use prisma_models::{ModelRef, RelationFieldRef};
-use std::sync::Arc;
+use connector::Filter;
+use prisma_models::{ModelRef, PrismaValue, RelationFieldRef};
+use std::{convert::TryInto, sync::Arc};
/// Handles nested connect cases.
/// The resulting graph can take multiple forms, based on the relation type to the parent model.
@@ -23,9 +24,9 @@ pub fn connect_nested_disconnect(
if relation.is_many_to_many() {
handle_many_to_many(graph, parent_node, parent_relation_field, value, child_model)?;
} else if relation.is_one_to_many() {
- // handle_one_to_many(graph, parent_node, parent_relation_field, value, child_model)?;
+ handle_one_to_many(graph, parent_node, parent_relation_field, value, child_model)?;
} else {
- // handle_one_to_one(graph, parent_node, parent_relation_field, value, child_model)?;
+ handle_one_to_one(graph, parent_node, parent_relation_field, value, child_model)?;
}
}
@@ -48,3 +49,57 @@ fn handle_many_to_many(
Ok(())
}
+
+fn handle_one_to_many(
+ graph: &mut QueryGraph,
+ parent_node: NodeRef,
+ parent_relation_field: &RelationFieldRef,
+ value: ParsedInputValue,
+ child_model: &ModelRef,
+) -> QueryGraphBuilderResult<()> {
+ let filter: Filter = if parent_relation_field.is_list {
+ let record_finder = extract_record_finder(value, &child_model)?;
+ record_finder.into()
+ } else {
+ Filter::empty()
+ };
+ let find_child_records_node =
+ utils::insert_find_children_by_parent_node(graph, &parent_node, parent_relation_field, filter)?;
+
+ disconnect::disconnect_records_node(
+ graph,
+ &parent_node,
+ &find_child_records_node,
+ &parent_relation_field,
+ None,
+ None,
+ )?;
+ Ok(())
+}
+
+fn handle_one_to_one(
+ graph: &mut QueryGraph,
+ parent_node: NodeRef,
+ parent_relation_field: &RelationFieldRef,
+ value: ParsedInputValue,
+ child_model: &ModelRef,
+) -> QueryGraphBuilderResult<()> {
+ let val: PrismaValue = value.try_into()?;
+ let should_delete = if let PrismaValue::Boolean(b) = val { true } else { false };
+
+ if should_delete {
+ let find_child_records_node =
+ utils::insert_find_children_by_parent_node(graph, &parent_node, parent_relation_field, Filter::empty())?;
+
+ disconnect::disconnect_records_node(
+ graph,
+ &parent_node,
+ &find_child_records_node,
+ &parent_relation_field,
+ None,
+ None,
+ )?;
+ }
+
+ Ok(())
+}
