newOS for Developers

Back to Graph Traversal

Edge Query

Query relationships from the __EDGES cache table

intermediateIndexedDB

Overview

Edge queries use the local __EDGES table in IndexedDB to quickly look up relationships without making network requests. This is the foundation for all graph traversal in the client.

The backend uses Neo4j, but edges are cached locally for performance.

Edge Key Format

// Format: "{entity}+{entityId}+{relationship}+{targetEntity}"

// Examples:
folder+abc123+attachment+post     // Posts in folder
user+xyz789+author+folder         // Folders by user
user+xyz789+rates+user            // Users that user follows
folder+abc123+access+user         // Users with access

Usage Example

import { getEdges, setEdges } from "@/lib/cache";

// Query edges from the cache
const postIds = await getEdges("folder", "folder-123", "attachment", "post");
// Returns: ["post-1", "post-2", "post-3"]

// Build the key manually
const edgeKey = `folder+${folderId}+attachment+post`;
const cached = await db.get("__EDGES", edgeKey);

// Update edges (after API response)
await setEdges("folder", "folder-123", "attachment", "post", [
  "post-1", "post-2", "post-3", "post-4"
]);

Cache Table Schema

// __EDGES table in IndexedDB
{
  key: string;           // Edge key format
  value: string[];       // Array of target IDs
  updated: number;       // Timestamp for cache invalidation
}

// Example entry:
{
  key: "folder+abc123+attachment+post",
  value: ["post-1", "post-2", "post-3"],
  updated: 1706886400000
}

Performance Tips

  • Edge lookups are O(1) - use them freely
  • Cache invalidation happens on write operations
  • For two-hop queries, chain getEdges calls
  • TTL is configurable per edge type