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: link(sourceId, targetId)
Description
Thelink method creates a directed edge from one existing node to another, turning flat records into a true graph. The edge is stored on the source node, persisted to storage, and synchronized with peers in real time, just like any other change. Linked nodes can then be explored with recursive graph traversal queries using the $edge operator in db.map().
This method is useful for modeling relationships between records (e.g., users and groups, orders and products, parents and children).
Parameters
-
sourceId(required):- Type: String
- Description: The unique identifier of the node the edge starts from (the parent).
-
targetId(required):- Type: String
- Description: The unique identifier of the node the edge points to (the child).
Behavior
-
Validation:
- Both nodes must already exist. If either
idis not found, a warning is logged (⚠️ One or both nodes (<sourceId>, <targetId>) do not exist.) and the method exits without making changes.
- Both nodes must already exist. If either
-
Directed Edge:
link(a, b)createsa → bonly. The relationship is one-way; for a bidirectional relationship, calllinkin both directions.
-
Timestamping:
- The operation is stamped with the Hybrid Logical Clock (HLC), so concurrent changes resolve deterministically across peers.
-
Persistence:
- Changes are saved to persistent storage (e.g., OPFS).
-
Notification:
- The method emits an event to notify listeners and peers of the change. This is useful for real-time synchronization in distributed systems.
Returns
- Nothing:
- The method does not return any value. However, it logs a warning if either of the specified nodes does not exist.
Examples
Example 1: Linking Two Nodes
- Two nodes are created with
db.put(). - A directed edge is created from the group to the user.
Example 2: Bidirectional Relationship
- Each
linkcall creates a single one-way edge. - Calling it in both directions models a mutual relationship.
Example 3: Querying Linked Nodes with $edge
- The main part of the query selects the starting node(s) for the traversal.
- The
$edgesub-query filters their descendants (children, grandchildren, etc.), which are returned as the result.
Example 4: Linking a Non-Existent Node
- The method logs a warning because the target node does not exist, and no changes are made.
Key Notes
-
Direction Matters:
- Edges are followed outward from the starting nodes during
$edgetraversals. The parent belongs in the main query and the children’s filter inside$edge. Inverting them (e.g.,{ $edge: { id: parentId } }) asks for descendants equal to the parent, which is almost always empty.
- Edges are followed outward from the starting nodes during
-
Edges Live on the Source Node:
- Each node carries an
edgesarray of target IDs.db.map()callbacks expose it alongsideid,value, andtimestamp.
- Each node carries an
-
Edge Cleanup:
- When a node is deleted with
db.remove(), references to it in other nodes’ edges are cleaned up automatically — no dangling edges.
- When a node is deleted with
-
Persistence & Notifications:
- All changes made by
linkare persisted to storage and emitted to listeners and peers, keeping the graph consistent across the network.
- All changes made by
Use Cases
- Modeling Relationships: Use
linkto connect records (e.g., users to groups, orders to products, documents to revisions). - Graph Traversal: Combine
linkwith the$edgeoperator to resolve deep, multi-hop relationships in a single declarative query. - Hierarchies: Build trees (categories, organizations, threads) by consistently linking from parent to child.
Best Practices
- Create Nodes First: Ensure both nodes exist (
db.put()) before linking them; the method does not create missing nodes. - Choose Meaningful Directions: Prefer natural directions (
order → items,group → members,parent → children) so$edgetraversals read intuitively. - Await the Call: The method is asynchronous — always
awaitit so ordering with subsequent queries is guaranteed.
This documentation provides a clear and concise explanation of the
link method, including its behavior, parameters, error handling, and practical examples. Let me know if you’d like further clarification or additional examples! 😊