Classes
NanGraph
Directed multi-edge in-memory graph.
| Method (TS) | Method (Python) | Description |
|---|---|---|
new NanGraph() |
NanGraph() |
Constructor |
g.addNode(node) |
g.add_node(dict) |
Add node (replaces if exists) |
g.addEdge(edge) |
g.add_edge(from_, to, rel_type) |
Add directed typed edge (auto-creates nodes) |
g.getNode(id) |
g.get_node(id) |
Get node (or undefined/None) |
g.hasNode(id) |
g.has_node(id) |
Does node exist? |
g.getRelated(id, relType?, direction?) |
g.get_related(id, rel_type?, direction?) |
Outgoing/incoming/both neighbors with filter |
g.nodeCount |
g.node_count |
Node count |
g.edgeCount |
g.edge_count |
Edge count |
g.edges |
g.edges |
Edge list |
g.nodes |
g.nodes |
Node map |
g.addNode({ id: 'api', type: 'aws.apigateway', label: 'API GW', attrs: { monthly_cost: 20 } });
g.addEdge({ from: 'api', to: 'lambda', relType: 'routes_to' });
g.add_node({"id": "api", "type": "aws.apigateway", "label": "API GW", "attrs": {"monthly_cost": 20}})
g.add_edge("api", "lambda", "routes_to")
All construction methods return the graph itself → chainable.
Analysis functions
| TS | Python | Formula |
|---|---|---|
traverseBFS(g, start, opts?) |
traverse_bfs(g, start, rel_types?, max_depth?, direction?) |
Reachable nodes (BFS), excl. start |
traverseDFS(g, start, opts?) |
traverse_dfs(g, start, rel_types?, max_depth?, direction?) |
Reachable nodes (DFS), excl. start |
blastRadius(g, start, opts?) |
blast_radius(g, start, rel_types?, max_depth?) |
downstream affected nodes |
costChain(g, start, opts?) |
cost_chain(g, start, rel_types?, max_depth?) |
Σ monthly_cost (start + downstream) |
teamCost(g, owner) |
team_cost(g, owner) |
Σ monthly_cost by attrs.owner |
fanIn(g, id) |
fan_in(g, id) |
incoming edge count |
singlePointsOfFailure(g, threshold?) |
single_points_of_failure(g, threshold?) |
nodes with fan-in >= threshold |
TS options:
interface TraversalOptions {
relTypes?: string[]; // only these relations
maxDepth?: number; // depth limit
direction?: 'outgoing' | 'incoming' | 'both';
}
Importers
Build a NanGraph from data.
| TS | Python | Input |
|---|---|---|
fromObject(obj) |
from_object(dict) |
JS object / dict (BYaML or dependency-graph) |
fromYaml(str) |
from_yaml(str) |
YAML string |
fromJson(str) |
from_json(str) |
JSON string |
Supported formats:
- BYaML v0.3:
{ components: [...], relationships: [{from,to,type}] } - dependency-graph.yaml:
{ nodes: {id: props}, edges: [{from,to,relType}] }
Types
interface GraphNode {
id: string;
label?: string;
type?: string;
attrs?: Record<string, unknown>;
}
interface GraphEdge {
from: string;
to: string;
relType: string;
label?: string;
}
Direction semantics
| Value | Behavior |
|---|---|
outgoing |
this → others |
incoming |
others → this |
both |
both |
Notes
- Zero deps in TS core (YAML import uses lightweight
yaml). Python usesPyYAML. - Works in Node.js, Cloudflare Workers, Lambda (TS) and Python 3.10+ (Py).
- Same API across TS and Python — column by column parity.