MCPcopy Create free account
hub / github.com/codemix/graph / addEdge

Method addEdge

packages/graph/src/Graph.ts:591–643  ·  view source on GitHub ↗
(...args: any[])

Source from the content-addressed store, hash-verified

589 properties: EdgeProperties<TSchema, TEdgeLabel>,
590 ): Edge<TSchema, TEdgeLabel>;
591 public addEdge(...args: any[]) {
592 let rawOutV: Vertex<TSchema, any> | ElementId;
593 let label: EdgeLabel<TSchema>;
594 let rawInV: Vertex<TSchema, any> | ElementId;
595 let properties: EdgeProperties<TSchema, any>;
596 if (args.length === 1 && typeof args[0] === "object" && args[0] !== null) {
597 ({ outV: rawOutV, label, inV: rawInV, properties } = args[0]);
598 } else {
599 [rawOutV, label, rawInV, properties] = args;
600 }
601
602 // Validate and transform properties when validation is enabled
603 const validateProperties = this.#config.validateProperties ?? true;
604 if (validateProperties) {
605 const schemaProperties = this.#config.schema.edges[label]?.properties;
606 properties = parseProperties(label, properties, schemaProperties);
607 }
608
609 const id = this.generateElementId(label);
610
611 const outV = typeof rawOutV === "object" ? rawOutV : this.getVertexById(rawOutV);
612 const inV = typeof rawInV === "object" ? rawInV : this.getVertexById(rawInV);
613
614 if (outV == null) {
615 throw new VertexNotFoundError(rawOutV as ElementId);
616 }
617 if (inV == null) {
618 throw new VertexNotFoundError(rawInV as ElementId);
619 }
620 const data: StoredEdge = {
621 "@type": "Edge",
622 id,
623 properties,
624 inV: inV.id,
625 outV: outV.id,
626 };
627
628 // Ensure unique indexes are built before checking constraints
629 this.#indexManager.ensureUniqueIndexesBuilt(label, this.#config.storage.getEdges([label]));
630
631 // Check unique constraints before adding (throws if violated)
632 this.#indexManager.checkAllUniqueConstraints(label, properties);
633
634 // Add to storage
635 this.#config.storage.addEdge(data);
636
637 // Add to indexes
638 this.#indexManager.onElementAdd(data);
639
640 const edge = this.instantiateEdge(data);
641
642 return edge;
643 }
644
645 /**
646 * Delete an edge from the graph.

Callers 3

setupQueryExecutionGraphFunction · 0.95
setupMultiLabelGraphFunction · 0.95
createWeightedEdgeGraphFunction · 0.95

Calls 9

generateElementIdMethod · 0.95
getVertexByIdMethod · 0.95
instantiateEdgeMethod · 0.95
parsePropertiesFunction · 0.85
onElementAddMethod · 0.80
getEdgesMethod · 0.65
addEdgeMethod · 0.65

Tested by 3

setupQueryExecutionGraphFunction · 0.76
setupMultiLabelGraphFunction · 0.76
createWeightedEdgeGraphFunction · 0.76