import{Graph}from"@monstermann/graph";// Define your node and edge typestypeNodes=|{type: "User";id: string;name: string}|{type: "Post";id: string;title: string}|{type: "Comment";id: string;text: string};typeEdges={User: {Post: {role: "author"|"editor"};Comment: void};Post: {Comment: void};};// Create a graphconstgraph=Graph.create<Nodes,Edges>();// Add nodesletg=Graph.setNode(graph,{type: "User",id: "1",name: "Alice"});g=Graph.setNode(g,{type: "Post",id: "1",title: "Hello World"});g=Graph.setNode(g,{type: "Comment",id: "1",text: "Great post!"});// Add edges with datag=Graph.setEdge(g,["User","1"],["Post","1"],{role: "author"});g=Graph.setEdge(g,["User","1"],["Comment","1"]);g=Graph.setEdge(g,["Post","1"],["Comment","1"]);// Query the graphconstuser=Graph.getNode(g,["User","1"]);// user: { type: "User", id: "1", name: "Alice" }constuserPosts=Graph.getNeighbors(g,["User","1"],"Post");// userPosts: [{ type: "Post", id: "1", title: "Hello World" }]constpostEdge=Graph.getEdge(g,["User","1"],["Post","1"]);// postEdge: { role: "author" }npm install @monstermann/graphpnpm add @monstermann/graphyarn add @monstermann/graphbun add @monstermann/graphnpm install -D @monstermann/unplugin-graphpnpm -D add @monstermann/unplugin-graphyarn -D add @monstermann/unplugin-graphbun -D add @monstermann/unplugin-graph// vite.config.tsimportgraphfrom"@monstermann/unplugin-graph/vite";exportdefaultdefineConfig({plugins: [graph()],});// rollup.config.jsimportgraphfrom"@monstermann/unplugin-graph/rollup";exportdefault{plugins: [graph()],};// rolldown.config.jsimportgraphfrom"@monstermann/unplugin-graph/rolldown";exportdefault{plugins: [graph()],};// webpack.config.jsconstgraph=require("@monstermann/unplugin-graph/webpack");module.exports={plugins: [graph()],};// rspack.config.jsconstgraph=require("@monstermann/unplugin-graph/rspack");module.exports={plugins: [graph()],};// esbuild.config.jsimport{build}from"esbuild";importgraphfrom"@monstermann/unplugin-graph/esbuild";build({plugins: [graph()],});functionGraph.batch(graph: Graph,transform: (graph: Graph)=>Graph|void,): GraphPerforms multiple graph mutations efficiently by batching them together. Returns the original graph if no changes were made, optimizing for structural sharing.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string;name: string}|{type: "Project";id: string;name: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();// Add multiple nodes and edges in a single batchconstupdated=Graph.batch(graph,(g)=>{g=Graph.setNode(g,{type: "Project",id: "1",name: "My Project"});g=Graph.setNode(g,{type: "Task",id: "1",title: "Task 1"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Task 2"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);g=Graph.setEdge(g,["Project","1"],["Task","2"]);returng;});functionGraph.create<Nodes,Edges>(): Graph<Nodes,Edges>Creates a new empty graph with the provided nodes & edges configuration.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={// Project <-> TaskProject: {Task: void};// Section <-> TaskSection: {Task: void};};constgraph=Graph.create<Nodes,Edges>();functionGraph.findEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,find: (edge: EdgeData)=>boolean,): EdgeData|undefinedFinds the first edge of a specific type from a source node that matches the predicate. Returns undefined if no matching edge is found.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number;assignedAt: Date}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setNode(g,{type: "Task",id: "2"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1,assignedAt: newDate(),});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 2,assignedAt: newDate(),});consthighPriorityEdge=Graph.findEdge(g,["Project","1"],"Task",(edge)=>edge.priority>1,);// highPriorityEdge: { priority: 2, assignedAt: Date }functionGraph.findEdges(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,find: (edge: EdgeData)=>boolean,): EdgeData[]Finds all edges of a specific type from a source node that match the predicate.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number;assignedAt: Date}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setNode(g,{type: "Task",id: "2"});g=Graph.setNode(g,{type: "Task",id: "3"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1,assignedAt: newDate(),});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 2,assignedAt: newDate(),});g=Graph.setEdge(g,["Project","1"],["Task","3"],{priority: 3,assignedAt: newDate(),});consthighPriorityEdges=Graph.findEdges(g,["Project","1"],"Task",(edge)=>edge.priority>=2,);// highPriorityEdges: [{ priority: 2, ... }, { priority: 3, ... }]functionGraph.findNeighbor(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,find: (target: Node,edge: EdgeData,source: Node)=>boolean,): Node|undefinedFinds the first neighbor node of a specific type that matches the predicate. The predicate receives the target node, edge data, and source node. Returns undefined if no matching neighbor is found.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1",title: "Low Priority Task"});g=Graph.setNode(g,{type: "Task",id: "2",title: "High Priority Task"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 3});consthighPriorityTask=Graph.findNeighbor(g,["Project","1"],"Task",(task,edge,project)=>edge.priority>2,);// highPriorityTask: { type: "Task", id: "2", title: "High Priority Task" }functionGraph.findNeighbors(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,find: (target: Node,edge: EdgeData,source: Node)=>boolean,): Node[]Finds all neighbor nodes of a specific type that match the predicate. The predicate receives the target node, edge data, and source node.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1",title: "Low Priority Task"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Medium Priority Task"});g=Graph.setNode(g,{type: "Task",id: "3",title: "High Priority Task"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 2});g=Graph.setEdge(g,["Project","1"],["Task","3"],{priority: 3});constmediumAndHighPriority=Graph.findNeighbors(g,["Project","1"],"Task",(task,edge,project)=>edge.priority>=2,);// mediumAndHighPriority: [// { type: "Task", id: "2", title: "Medium Priority Task" },// { type: "Task", id: "3", title: "High Priority Task" }// ]functionGraph.findNode(graph: Graph,type: NodeType,find: (node: Node)=>boolean,): Node|undefinedFinds the first node of a specific type that matches the predicate. Returns undefined if no matching node is found.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string;completed: boolean}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Task",id: "1",title: "First Task",completed: false,});g=Graph.setNode(g,{type: "Task",id: "2",title: "Second Task",completed: true,});g=Graph.setNode(g,{type: "Task",id: "3",title: "Third Task",completed: false,});constcompletedTask=Graph.findNode(g,"Task",(task)=>task.completed);// completedTask: { type: "Task", id: "2", title: "Second Task", completed: true }consttaskWithTitle=Graph.findNode(g,"Task",(task)=>task.title==="First Task",);// taskWithTitle: { type: "Task", id: "1", title: "First Task", completed: false }functionGraph.findNodes(graph: Graph,type: NodeType,find: (node: Node)=>boolean,): Node[]Finds all nodes of a specific type that match the predicate.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string;completed: boolean}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Task",id: "1",title: "First Task",completed: false,});g=Graph.setNode(g,{type: "Task",id: "2",title: "Second Task",completed: true,});g=Graph.setNode(g,{type: "Task",id: "3",title: "Third Task",completed: false,});constincompleteTasks=Graph.findNodes(g,"Task",(task)=>!task.completed);// incompleteTasks: [// { type: "Task", id: "1", title: "First Task", completed: false },// { type: "Task", id: "3", title: "Third Task", completed: false }// ]functionGraph.forEachEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,fn: (edge: EdgeData)=>void,): GraphIterates over all edges of a specific type from a source node, executing a function for each edge. Returns the graph unchanged (for chaining).
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setNode(g,{type: "Task",id: "2"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 2});Graph.forEachEdge(g,["Project","1"],"Task",(edge)=>{console.log(edge.priority);});// Logs:// 1// 2functionGraph.forEachNeighbor(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,fn: (target: Node,edge: EdgeData,source: Node)=>void,): GraphIterates over all neighbor nodes of a specific type, executing a function for each neighbor. The function receives the target node, edge data, and source node. Returns the graph unchanged (for chaining).
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string;name: string};typeEdges={Project: {Task: {priority: number}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1",name: "My Project"});g=Graph.setNode(g,{type: "Task",id: "1",title: "First Task"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Second Task"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 2});Graph.forEachNeighbor(g,["Project","1"],"Task",(task,edge,project)=>{console.log(`${project.name}: ${task.title} (priority: ${edge.priority})`);});// Logs:// "My Project: First Task (priority: 1)"// "My Project: Second Task (priority: 2)"functionGraph.forEachNode(graph: Graph,type: NodeType,fn: (node: Node)=>void,): GraphIterates over all nodes of a specific type, executing a function for each node. Returns the graph unchanged (for chaining).
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Task",id: "1",title: "First Task"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Second Task"});g=Graph.setNode(g,{type: "Task",id: "3",title: "Third Task"});Graph.forEachNode(g,"Task",(task)=>{console.log(task.title);});// Logs:// "First Task"// "Second Task"// "Third Task"functionGraph.fromJS(data: {nodes: Node[];edges: [NodeType,NodeId,NodeType,NodeId,EdgeData][];}): GraphCreates a graph from a plain JavaScript object representation. Useful for deserializing graphs from JSON or other storage formats.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constdata={nodes: [{type: "Project",id: "1"},{type: "Task",id: "1",title: "My Task"},{type: "Task",id: "2",title: "Another Task"},],edges: [["Project","1","Task","1",undefined],["Project","1","Task","2",undefined],],};constgraph=Graph.fromJS<Nodes,Edges>(data);Graph.hasNode(graph,["Project","1"]);// trueGraph.hasEdge(graph,["Project","1"],["Task","1"]);// truefunctionGraph.getEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},target: [NodeType,NodeId]|{type: NodeType,id: NodeId},): EdgeData|undefinedRetrieves the edge data between two nodes. Returns undefined if the edge doesn't exist.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {assignedAt: Date}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{assignedAt: newDate(),});constedge=Graph.getEdge(g,["Project","1"],["Task","1"]);// edge: { assignedAt: Date }constmissing=Graph.getEdge(g,["Project","1"],["Task","2"]);// missing: undefinedfunctionGraph.getEdges(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,): EdgeData[]Retrieves all edges of a specific type from a source node.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setNode(g,{type: "Task",id: "2"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1});g=Graph.setEdge(g,["Project","1"],["Task","2"],{priority: 2});constedges=Graph.getEdges(g,["Project","1"],"Task");// edges: [{ priority: 1 }, { priority: 2 }]functionGraph.getNeighbor(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,): Node|undefinedRetrieves the first neighbor node of a specific type connected to the source node. Returns undefined if no neighbor of the specified type exists.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1",title: "Task 1"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Task 2"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);g=Graph.setEdge(g,["Project","1"],["Task","2"]);constfirstTask=Graph.getNeighbor(g,["Project","1"],"Task");// firstTask: { type: "Task", id: "1", title: "Task 1" }constnoSection=Graph.getNeighbor(g,["Project","1"],"Section");// noSection: undefinedfunctionGraph.getNeighbors(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},type: NodeType,): Node[]Retrieves all neighbor nodes of a specific type connected to the source node.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1",title: "Task 1"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Task 2"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);g=Graph.setEdge(g,["Project","1"],["Task","2"]);consttasks=Graph.getNeighbors(g,["Project","1"],"Task");// tasks: [{ type: "Task", id: "1", title: "Task 1" }, { type: "Task", id: "2", title: "Task 2" }]functionGraph.getNode(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},): Node|undefinedRetrieves a node from the graph. Returns undefined if the node doesn't exist.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();constwithNode=Graph.setNode(graph,{type: "Task",id: "1",title: "My Task",});consttask=Graph.getNode(withNode,["Task","1"]);// task: { type: "Task", id: "1", title: "My Task" }constmissing=Graph.getNode(withNode,["Task","2"]);// missing: undefinedfunctionGraph.getNodes(graph: Graph,type: NodeType,): Node[]Retrieves all nodes of a specific type from the graph.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Task",id: "1",title: "Task 1"});g=Graph.setNode(g,{type: "Task",id: "2",title: "Task 2"});g=Graph.setNode(g,{type: "Project",id: "1"});consttasks=Graph.getNodes(g,"Task");// tasks: [{ type: "Task", id: "1", title: "Task 1" }, { type: "Task", id: "2", title: "Task 2" }]constprojects=Graph.getNodes(g,"Project");// projects: [{ type: "Project", id: "1" }]functionGraph.hasEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},target: [NodeType,NodeId]|{type: NodeType,id: NodeId},): booleanChecks if an edge exists between two nodes in the graph.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);Graph.hasEdge(g,["Project","1"],["Task","1"]);// trueGraph.hasEdge(g,["Task","1"],["Project","1"]);// true (bidirectional)Graph.hasEdge(g,["Project","1"],["Task","2"]);// falsefunctionGraph.hasNode(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},): booleanChecks if a node exists in the graph.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();constwithNode=Graph.setNode(graph,{type: "Task",id: "1"});Graph.hasNode(withNode,["Task","1"]);// trueGraph.hasNode(withNode,{type: "Task",id: "1"});// trueGraph.hasNode(withNode,["Task","2"]);// falsefunctionGraph.mapEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},target: [NodeType,NodeId]|{type: NodeType,id: NodeId},update: (edge: EdgeData)=>EdgeData,): GraphUpdates an edge by applying a transformation function. Returns a new graph instance with the updated edge.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number;assignedAt: Date}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1,assignedAt: newDate(),});// Update edge datag=Graph.mapEdge(g,["Project","1"],["Task","1"],(edge)=>({
...edge,priority: 2,}));functionGraph.mapNode(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},update: (node: Node)=>Node,): GraphUpdates a node by applying a transformation function. If the node's type or id changes, all edges are preserved. Returns a new graph instance with the updated node.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string;completed: boolean}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Task",id: "1",title: "My Task",completed: false,});// Update a propertyg=Graph.mapNode(g,["Task","1"],(task)=>({
...task,completed: true,}));// Change id (edges are preserved)letg2=Graph.setNode(graph,{type: "Task",id: "1",title: "Task",completed: false,});g2=Graph.setNode(g2,{type: "Project",id: "1"});g2=Graph.setEdge(g2,["Project","1"],["Task","1"]);g2=Graph.mapNode(g2,["Task","1"],(task)=>({
...task,id: "2",}));// Edge now connects ["Project", "1"] -> ["Task", "2"]functionGraph.mergeEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},target: [NodeType,NodeId]|{type: NodeType,id: NodeId},update: Partial<EdgeData>,): GraphPartially updates an edge by merging the provided properties. Returns a new graph instance with the updated edge.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {priority: number;assignedAt: Date}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setEdge(g,["Project","1"],["Task","1"],{priority: 1,assignedAt: newDate(),});// Merge partial updateg=Graph.mergeEdge(g,["Project","1"],["Task","1"],{priority: 2});constedge=Graph.getEdge(g,["Project","1"],["Task","1"]);// edge: { priority: 2, assignedAt: <original date> }functionGraph.mergeNode(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},update: Partial<Node>,): GraphPartially updates a node by merging the provided properties. Returns a new graph instance with the updated node.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string;completed: boolean}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Task",id: "1",title: "My Task",completed: false,});// Merge partial updateg=Graph.mergeNode(g,["Task","1"],{completed: true});consttask=Graph.getNode(g,["Task","1"]);// task: { type: "Task", id: "1", title: "My Task", completed: true }functionGraph.removeEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},target: [NodeType,NodeId]|{type: NodeType,id: NodeId},): GraphRemoves an edge between two nodes. The nodes remain in the graph. Returns a new graph instance without the edge.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);// Remove the edgeg=Graph.removeEdge(g,["Project","1"],["Task","1"]);Graph.hasNode(g,["Project","1"]);// trueGraph.hasNode(g,["Task","1"]);// trueGraph.hasEdge(g,["Project","1"],["Task","1"]);// falsefunctionGraph.removeNode(graph: Graph,node: [NodeType,NodeId]|{type: NodeType,id: NodeId},): GraphRemoves a node and all its associated edges from the graph. Returns a new graph instance without the node.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);// Remove the task (also removes the edge)g=Graph.removeNode(g,["Task","1"]);Graph.hasNode(g,["Task","1"]);// falseGraph.hasEdge(g,["Project","1"],["Task","1"]);// falsefunctionGraph.setEdge(graph: Graph,source: [NodeType,NodeId]|{type: NodeType,id: NodeId},target: [NodeType,NodeId]|{type: NodeType,id: NodeId},data?: EdgeData,): GraphAdds or updates an edge between two nodes. Both nodes must exist in the graph. Returns a new graph instance with the edge set. Edges are bidirectional.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: {assignedAt: Date}};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1"});// Add edge with datag=Graph.setEdge(g,["Project","1"],["Task","1"],{assignedAt: newDate(),});// Add edge without data (void)letg2=Graph.setNode(graph,{type: "Section",id: "1"});g2=Graph.setNode(g2,{type: "Task",id: "1"});g2=Graph.setEdge(g2,["Section","1"],["Task","1"]);functionGraph.setNode(graph: Graph,node: Node,): GraphAdds or updates a node in the graph. Returns a new graph instance with the node set.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();// Add a new nodeconstg1=Graph.setNode(graph,{type: "Task",id: "1",title: "My Task"});// Update existing nodeconstg2=Graph.setNode(g1,{type: "Task",id: "1",title: "Updated Task"});functionGraph.toJS(graph: Graph,): {nodes: Node[];edges: [NodeType,NodeId,NodeType,NodeId,EdgeData][];}Converts a graph to a plain JavaScript object representation. Useful for serializing graphs to JSON or other storage formats.
import{Graph}from"@monstermann/graph";typeNodes=|{type: "Task";id: string;title: string}|{type: "Section";id: string}|{type: "Project";id: string};typeEdges={Project: {Task: void};Section: {Task: void};};constgraph=Graph.create<Nodes,Edges>();letg=Graph.setNode(graph,{type: "Project",id: "1"});g=Graph.setNode(g,{type: "Task",id: "1",title: "My Task"});g=Graph.setEdge(g,["Project","1"],["Task","1"]);constdata=Graph.toJS(g);// data: {// nodes: [// { type: "Project", id: "1" },// { type: "Task", id: "1", title: "My Task" }// ],// edges: [// ["Project", "1", "Task", "1", undefined]// ]// }// Can be serialized to JSONconstjson=JSON.stringify(data);