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

Method updateProperty

packages/graph/src/Graph.ts:670–718  ·  view source on GitHub ↗

* Update a property of a vertex or edge. * @param id The id of the vertex or edge, or the vertex or edge itself. * @param key The key of the property. * @param value The value of the property. * @throws PropertyValidationError if validateProperties is enabled and key is not in schema.

(
    id: ElementId | Vertex<TSchema, any> | Edge<TSchema, any>,
    key: string,
    value: any,
  )

Source from the content-addressed store, hash-verified

668 * @throws UniqueConstraintViolationError if a unique constraint would be violated.
669 */
670 public updateProperty(
671 id: ElementId | Vertex<TSchema, any> | Edge<TSchema, any>,
672 key: string,
673 value: any,
674 ): void {
675 if (typeof id === "object") {
676 id = id.id;
677 }
678 const label = getLabelFromElementId(id);
679 const isVertex = label in this.#config.schema.vertices;
680 const isEdge = label in this.#config.schema.edges;
681
682 // Validate and transform property value when validation is enabled (default: false)
683 let parsedValue = value;
684 const validateProperties = this.#config.validateProperties ?? true;
685 if (validateProperties) {
686 const schemaProperties = isVertex
687 ? this.#config.schema.vertices[label]?.properties
688 : isEdge
689 ? this.#config.schema.edges[label]?.properties
690 : undefined;
691 // parsePropertyValue validates key exists and transforms value
692 parsedValue = parsePropertyValue(key, label, value, schemaProperties);
693 }
694
695 // Ensure unique indexes are built before checking constraints
696 if (this.#indexManager.isUnique(label, key)) {
697 const elements = isVertex
698 ? this.#config.storage.getVertices([label])
699 : this.#config.storage.getEdges([label]);
700 this.#indexManager.ensureUniqueIndexesBuilt(label, elements);
701
702 // Check unique constraint (exclude current element since it's an update)
703 // Use parsedValue for unique constraint check
704 this.#indexManager.checkUniqueConstraint(label, key, parsedValue, id);
705 }
706
707 const element = isVertex
708 ? this.#config.storage.getVertexById(id)
709 : isEdge
710 ? this.#config.storage.getEdgeById(id)
711 : undefined;
712 const oldValue = element ? (element.properties as Record<string, unknown>)[key] : undefined;
713 // Store the parsed/transformed value, not the raw input
714 this.#config.storage.updateProperty(id, key, parsedValue);
715 if (element) {
716 this.#indexManager.onPropertyUpdate(id, key, oldValue, parsedValue);
717 }
718 }
719
720 public toJSON() {
721 return {

Callers

nothing calls this directly

Calls 11

getLabelFromElementIdFunction · 0.85
parsePropertyValueFunction · 0.85
isUniqueMethod · 0.80
checkUniqueConstraintMethod · 0.80
onPropertyUpdateMethod · 0.80
getVerticesMethod · 0.65
getEdgesMethod · 0.65
getVertexByIdMethod · 0.65
getEdgeByIdMethod · 0.65
updatePropertyMethod · 0.65

Tested by

no test coverage detected