Skip to content

Commit 8eb8332

Browse files
committed
Feat: Add advanced SurrealDB graph analysis functions to NAPI bindings
Exposes 6 advanced graph analysis functions from the Rust SDK to TypeScript/JavaScript via N-API bindings, enabling sophisticated code analysis in Node.js applications. ## New Files ### crates/codegraph-napi/src/graph.rs (NEW - 450+ lines) Complete NAPI wrapper module with: - 11 type converter functions (Rust SDK → NAPI types) - 6 exported async functions with proper error handling - Feature-gated behind `cloud-surrealdb` feature - Tokio async runtime integration ### crates/codegraph-napi/GRAPH_FUNCTIONS_GUIDE.md (NEW - 600+ lines) Comprehensive guide including: - Overview and use cases - SurrealDB setup instructions (local + cloud) - Complete function reference with examples - Real-world integration patterns (CI/CD, code review, dashboards) - Troubleshooting guide ### crates/codegraph-napi/example-graph.ts (NEW - 450+ lines) Runnable TypeScript example demonstrating: - All 6 graph functions with realistic scenarios - Environment variable setup - Error handling patterns - Practical workflows (architectural analysis, impact assessment) ## Modified Files ### crates/codegraph-napi/src/types.rs Added 11 #[napi(object)] types: - NodeLocation, NodeInfo, DependencyNode - CircularDependency, CallerInfo, CallChainNode - CouplingMetrics, CouplingMetricsResult - NodeReference, EdgeTypeCount, HubNode ### crates/codegraph-napi/src/state.rs - Added GraphFunctions initialization from SURREALDB_CONNECTION env var - Added `graph_functions: Option<Arc<GraphFunctions>>` field - Implemented `init_graph_functions()` helper for SurrealDB setup ### crates/codegraph-napi/src/lib.rs - Added graph module declaration with feature gate - Exported all 11 graph types - Exported all 6 graph functions ### crates/codegraph-napi/README.md - Added "Graph Analysis Functions" section (150+ lines) - Complete TypeScript usage examples for all 6 functions - Full TypeScript interface definitions ### crates/codegraph-napi/Cargo.toml - Updated cloud-surrealdb feature to enable codegraph-graph/surrealdb ### crates/codegraph-napi/package.json - Added `example:graph` script ## Exported Functions All 6 functions are async and return Promises: 1. **getTransitiveDependencies**(nodeId, edgeType, depth) - Find all downstream dependencies up to specified depth 2. **getReverseDependencies**(nodeId, edgeType, depth) - Find all upstream dependents (what depends on this node) 3. **detectCircularDependencies**(edgeType) - Identify circular dependency cycles in the graph 4. **traceCallChain**(fromNode, maxDepth) - Map function call hierarchies and execution paths 5. **calculateCouplingMetrics**(nodeId) - Measure afferent/efferent coupling, instability, stability 6. **getHubNodes**(minDegree) - Find highly connected nodes (architectural hubs) ## Environment Setup ```bash export SURREALDB_CONNECTION=ws://localhost:8000 # Or use SurrealDB Cloud (1GB free): wss://your-instance.surrealdb.cloud ``` ## Usage Example ```typescript import { getHubNodes, calculateCouplingMetrics } from 'codegraph-napi'; // Find architectural hubs const hubs = await getHubNodes(10); console.log(`Found ${hubs.length} hub nodes`); // Analyze coupling for a module const metrics = await calculateCouplingMetrics('node:module-uuid'); console.log(`Instability: ${metrics.metrics.instability}`); ``` ## Build Verification Code compiles successfully with cloud-surrealdb feature: ```bash cargo check -p codegraph-napi --no-default-features --features cloud-surrealdb ``` ## Documentation - Complete API reference in README.md - Comprehensive guide in GRAPH_FUNCTIONS_GUIDE.md - Working example in example-graph.ts - TypeScript type definitions auto-generated via napi-rs This implementation brings the full power of SurrealDB graph analysis to the Node.js ecosystem!
1 parent 1e9b556 commit 8eb8332

File tree

9 files changed

+2357
-5
lines changed

9 files changed

+2357
-5
lines changed

crates/codegraph-napi/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212
default = ["local"]
1313
local = ["codegraph-vector/faiss"]
1414
cloud-jina = ["codegraph-vector/jina"]
15-
cloud-surrealdb = ["surrealdb"]
15+
cloud-surrealdb = ["surrealdb", "codegraph-graph/surrealdb"]
1616
cloud = ["cloud-jina", "cloud-surrealdb"]
1717
full = ["local", "cloud"]
1818

@@ -33,7 +33,7 @@ tracing = { workspace = true }
3333
# Local crates
3434
codegraph-core = { path = "../codegraph-core" }
3535
codegraph-api = { path = "../codegraph-api" }
36-
codegraph-graph = { path = "../codegraph-graph" }
36+
codegraph-graph = { path = "../codegraph-graph", features = [] }
3737
codegraph-vector = { path = "../codegraph-vector" }
3838
codegraph-ai = { path = "../codegraph-ai" }
3939

0 commit comments

Comments
 (0)