Skip to content

Performance: Use SurrealDB recursive FETCH to return nested tree on initial load #602

Closed

Description

@malibio

Problem

Currently, the frontend receives a flat array of nodes and edges from the backend, then reconstructs the tree structure in visibleNodesFromStores using flattenNodes(). This happens on every reactive change, causing unnecessary CPU work.

Proposed Solution

Use SurrealDB's recursive FETCH capability to return a pre-built nested tree structure from the backend:

SELECT *, ->has_child->(node ORDER BY ->has_child.order).* AS children
FROM node:parent
FETCH children

Backend Changes Required

  1. packages/core/src/operations/mod.rs or services/node_service.rs:

    • Add get_children_tree(parent_id) function that returns nested structure
    • Use SurrealDB recursive FETCH to build tree in database
  2. packages/desktop-app/src-tauri/src/commands/nodes.rs:

    • Add new Tauri command get_children_tree

Frontend Changes Required

  1. packages/desktop-app/src/lib/stores/reactive-structure-tree.svelte.ts:

    • Store nested structure directly instead of flat parent→children Map
  2. packages/desktop-app/src/lib/design/components/base-node-viewer.svelte:

    • Consume nested tree without flattenNodes() reconstruction

Benefits

  • Eliminate O(n) tree reconstruction on every reactive update
  • Reduce frontend CPU usage
  • Database handles tree traversal more efficiently
  • Simpler frontend code

Reference

See docs/architecture/development/hierarchy-reactivity-architecture-review.md section 13.7 for component usage patterns.

Activity

  1. self-assigned this
    on Nov 21, 2025
  2. added 3 commits that reference this issue on Nov 21, 2025
    952d67a
    3fa3b3a
    004941f
  3. malibio commented on Nov 22, 2025

    @malibio
    CollaboratorAuthor

    Note: HierarchyRelationship Type

    When implementing this issue, consider whether the HierarchyRelationship type (from event-types.ts) needs adjustment.

    Current assessment: Likely minimal impact.

    The nested tree response from recursive FETCH may embed children directly in nodes, which could reduce usage of HierarchyRelationship for the initial load path. However, the type will still be needed for:

    • LIVE SELECT events (edge:created, edge:deleted, edge:updated)
    • Incremental updates after initial load

    The underlying edge fields (id, in, out, order) won't change - only the response shape (nested vs flat).

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

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

Metadata

Metadata

Assignees

Labels

backendenhancementperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions