async def update_with_nodes_and_edges(
        self,
        db: AsyncSession,
        *,
        db_obj: SyncDag,
        obj_in: SyncDagUpdate,
        current_user: schemas.User,
    ) -> SyncDag:
        """Update a DAG with its nodes and edges."""
        # Create a copy of the input without nodes and edges for basic fields update
        parent_update_data = obj_in.model_dump(exclude={"nodes", "edges"}, exclude_unset=True)

        # Update only the parent SyncDag fields
        db_obj = await self.update(
            db, db_obj=db_obj, obj_in=parent_update_data, current_user=current_user
        )

        # If nodes provided, replace all nodes
        if obj_in.nodes is not None:
            # Delete existing nodes (cascade will handle edges)
            await db.execute(delete(DagNode).where(DagNode.dag_id == db_obj.id))
            # Create new nodes
            for node_in in obj_in.nodes:
                node_data = node_in.model_dump()
                db_node = DagNode(
                    **node_data,
                    dag_id=db_obj.id,
                    organization_id=current_user.organization_id,
                    created_by_email=current_user.email,
                    modified_by_email=current_user.email,
                )
                db.add(db_node)
            await db.flush()

        # If edges provided, replace all edges
        if obj_in.edges is not None:
            # Delete existing edges
            await db.execute(delete(DagEdge).where(DagEdge.dag_id == db_obj.id))
            # Create new edges
            for edge_in in obj_in.edges:
                db_edge = DagEdge(
                    from_node_id=edge_in.from_node_id,
                    to_node_id=edge_in.to_node_id,
                    dag_id=db_obj.id,
                    organization_id=current_user.organization_id,
                    created_by_email=current_user.email,
                    modified_by_email=current_user.email,
                )
                db.add(db_edge)

        await db.commit()
        await db.refresh(db_obj)

        # Reload with relationships
        result = await db.execute(
            select(SyncDag)
            .where(SyncDag.id == db_obj.id)
            .options(
                selectinload(SyncDag.nodes),
                selectinload(SyncDag.edges),
            )
        )
        return result.scalar_one()
Update a DAG with its nodes and edges.
backend/airweave/crud/crud_dag.py