Skip to content

Backend should return relationship data from createNode API #603

Closed

Description

@malibio

Problem

Currently, when the frontend creates a node with a parent, it must manufacture the edge/relationship data locally using addInMemoryRelationship(). This is a workaround because the backend's create_node API only returns the node ID, not the relationship data.

From packages/desktop-app/src/lib/utils/node-hierarchy.ts:

// TODO: This is a workaround. The proper fix (per hierarchy-reactivity-architecture-review.md)
// is for the backend to return complete relationship data from node creation, allowing the
// frontend to apply a proper optimistic update instead of manufacturing relationship records.

Current Workaround

// Frontend guesses the order value
const order = existingChildren.length > 0 ? existingChildren.length + 1.0 : 1.0;
reactiveStructureTree.addInMemoryRelationship(parentId, childId, order);

This is problematic because:

  1. Frontend manufactures database records (violates single source of truth)
  2. Order calculation may not match backend's fractional ordering
  3. Edge ID is manufactured (${parentId}-${childId}) instead of using database ID

Proposed Solution

Backend Changes

  1. packages/core/src/operations/mod.rs:

    • Create CreateNodeResult struct:
      pub struct CreateNodeResult {
          pub node_id: String,
          pub edge: Option<HierarchyRelationship>,  // Present if parent_id was provided
      }
    • Modify create_node() to return Result<CreateNodeResult, NodeOperationError>
  2. packages/desktop-app/src-tauri/src/commands/nodes.rs:

    • Update create_node command to return CreateNodeResult
    • Add serialization for new type

Frontend Changes

  1. packages/desktop-app/src/lib/services/tauri-commands.ts:

    • Update createNode() return type
  2. packages/desktop-app/src/lib/services/shared-node-store.ts:

    • Use returned edge data instead of manufacturing it
  3. packages/desktop-app/src/lib/utils/node-hierarchy.ts:

    • Remove registerChildWithParent() workaround (or simplify to use real data)

Benefits

  • Single source of truth for relationship data
  • Correct edge IDs from database
  • Correct fractional order from backend
  • Eliminates frontend responsibility for database record generation

Reference

See docs/architecture/development/hierarchy-reactivity-architecture-review.md for architecture context.

Activity

  1. malibio commented on Nov 22, 2025

    @malibio
    CollaboratorAuthor

    Note from PR #604 Review

    During the code review of PR #604, the reviewer noted that the Rust verification query was removed in surreal_store.rs with this comment:

    // Note: Verification query removed - CREATE over HTTP client may have
    // read-after-write timing issues, but data IS persisted. We trust the
    // CREATE query succeeded if it didn't return an error.

    Suggestion: Once this issue (#603) is implemented and the backend returns complete relationship data from createNode, consider adding:

    • A warning log when running over HTTP to aid debugging
    • Optional metrics/counters for tracking CREATE operations for observability

    This would help detect any edge cases where CREATE might fail silently. The verification query removal was necessary to avoid timing issues, but observability could be improved as part of this work.

  2. malibio commented on Nov 22, 2025

    @malibio
    CollaboratorAuthor

    Note: HierarchyRelationship Type

    When implementing this issue, the backend should return relationship data using the HierarchyRelationship type structure (from event-types.ts):

    interface HierarchyRelationship {
      id: string;      // Database edge ID
      in: string;      // Parent node ID
      out: string;     // Child node ID  
      order: number;   // Fractional sort order
    }

    Current assessment: No changes needed to the type itself.

    This issue actually validates the abstraction - the returned relationship data will match the existing type, and frontend code can use getParentId() / getChildId() helpers if needed.

    Reference: HierarchyRelationship was introduced in PR #604 to abstract SurrealDB's edge format from frontend code.

  3. malibio commented on Nov 22, 2025

    @malibio
    CollaboratorAuthor

    Closing as resolved. LIVE SELECT now handles relationship updates automatically via edge:created events. The registerChildWithParent() workaround in node-hierarchy.ts is never called (dead code). The manual relationship manufacturing is no longer needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    architecturebackendenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions