Documentation Index
Fetch the complete documentation index at: https://mintlify.com/estebanrfp/gdb/llms.txt
Use this file to discover all available pages before exploring further.
Method: remove(id)
Description
Theremove method deletes a node from the graph by its unique identifier (id). It also ensures that any references to the removed node (e.g., edges) are cleaned up, maintaining the integrity of the graph. Once the node is removed, the changes are persisted to storage, and listeners are notified of the deletion.
This method is useful for removing nodes from the graph while ensuring data consistency and real-time updates.
Parameters
id(required):- Type: String
- Description: The unique identifier of the node to be removed. If the node with the specified
iddoes not exist, an error message will be logged, and the method will exit without making changes.
Behavior
-
Node Lookup:
- The method first checks if a node with the given
idexists in the graph. If no such node is found, an error message is logged, and the method exits.
- The method first checks if a node with the given
-
Node Removal:
- The node with the specified
idis deleted from the graph.
- The node with the specified
-
Edge Cleanup:
- Any references to the removed node (e.g., edges pointing to it) are cleaned up from other nodes in the graph. This ensures that the graph remains consistent and free of dangling references.
-
Persistence:
- Changes are saved to persistent storage (e.g., OPFS) using
saveGraphToOPFS.
- Changes are saved to persistent storage (e.g., OPFS) using
-
Notification:
- The method emits an event to notify listeners of the deletion using
sendDataandemit. This is useful for real-time synchronization in distributed systems.
- The method emits an event to notify listeners of the deletion using
Returns
- Nothing:
- The method does not return any value. However, it logs an error message if the specified node does not exist.
Examples
Example 1: Removing a Node
- A node is added to the graph with the value
{ name: "Alice", age: 25 }. - The node is then removed using its
id.
Example 2: Removing a Non-Existent Node
- The method logs an error message because no node with the specified
idexists in the graph.
Example 3: Cleaning Up Edges
- A relationship (edge) is created between two nodes.
- When the second node is removed, the edge referencing it is automatically cleaned up.
Key Notes
-
Error Handling:
- If the specified
iddoes not exist in the graph, the method logs an error message (Nodo con ID '<id>' no encontrado.) and exits without making changes.
- If the specified
-
Edge Cleanup:
- The method ensures that any edges pointing to the removed node are removed from other nodes. This prevents dangling references and maintains graph integrity.
-
Persistence:
- All changes made by
removeare persisted to storage usingsaveGraphToOPFS. This ensures durability of the graph.
- All changes made by
-
Notifications:
- The method emits events to notify listeners of the deletion. This is useful for real-time updates in distributed systems or collaborative applications.
Use Cases
- Deleting Records: Use
removeto delete nodes from the graph when records are no longer needed (e.g., users, products). - Maintaining Graph Integrity: The automatic cleanup of edges ensures that the graph remains consistent after a node is removed.
- Real-Time Updates: The notification mechanism allows other parts of the application (or other peers) to react to the deletion in real time.
Best Practices
- Validate IDs: Always ensure that the
idyou pass toremoveexists in the graph to avoid unnecessary error messages. - Handle Errors Gracefully: If your application relies on the removal of nodes, consider wrapping the
removecall in a try-catch block to handle errors programmatically. - Test Edge Cases: Test scenarios where nodes have multiple edges or where the graph is heavily interconnected to ensure proper cleanup.
This documentation provides a clear and concise explanation of the
remove method, including its behavior, parameters, error handling, and practical examples. Let me know if youβd like further clarification or additional examples! π