Hierarchy enables components to build tree structures with parent-child relationships. It provides circular reference prevention, archive cascade to descendants, and position-based sibling ordering. Use for container hierarchies, folder structures, or any nested organizational model.

1
Ports
1
Schemas
0
Hooks
4
Events
01

Ports

Required
  • Clock
    Timestamps for archive/move events
Optional
  • EventBus optional
    Publish hierarchy change events
Adapters Provided
  • InMemoryHierarchyTree
    implements HierarchyWriterPort
    In-memory tree for testing and simple deployments
02

Schemas

Defines
  • NodeInfo
    Lightweight node reference for tree traversal
Uses

No external schemas used

03

Hooks

No hooks declared

04

Events

  • NodeArchived v1
    When a node is archived (including cascade)
    payload: {node_id, archived_at, archived_by_cascade}
  • NodeUnarchived v1
    When a node is unarchived
    payload: {node_id, unarchived_at}
  • NodeMoved v1
    When a node is moved to a new parent
    payload: {node_id, old_parent_id, new_parent_id}
  • SiblingsReordered v1
    When sibling order is changed
    payload: {parent_id, ordered_ids, reordered_at}
05

Stories

06

Examples

Container hierarchy in Todos
from psp.platform.hierarchy import InMemoryHierarchyTree, HierarchyReaderPort

class ContainerRepository:
    def __init__(self, hierarchy: HierarchyReaderPort):
        self._hierarchy = hierarchy

    def get_breadcrumb(self, container_id: UUID) -> list[str]:
        ancestors = self._hierarchy.get_ancestors(
            container_id, include_self=True
        )
        return [self._get_name(a.id) for a in reversed(ancestors)]

Use hierarchy for task container organization.

Folder structure for documents
tree = InMemoryHierarchyTree(max_depth=10)

# Create folder structure
root = uuid4()
tree.register_node(root)

documents = uuid4()
tree.register_node(documents, parent_id=root)

# Get depth
depth = tree.get_depth(documents)
print(f"Depth: {depth}")  # 1

Organize documents in nested folders.

Archive with event publishing
from psp.platform.hierarchy import InMemoryHierarchyTree

tree = InMemoryHierarchyTree()

# Archive subtree
archived_ids = tree.archive_subtree(container_id)

# Get and publish events
for event in tree.pop_events():
    print(f"Event: {type(event).__name__} for {event.node_id}")

Publish events after archiving a subtree.

API Reference

This component mounts routes under /v1/hierarchy. View OpenAPI specification