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: put(value, id)
Description
Theput method inserts or updates a node in the graph with the specified value and id. If no id is provided, it generates a unique hash based on the value to serve as the node’s identifier. The method ensures that changes are persisted to storage and notifies listeners of the update.
This method is useful for adding new nodes or updating existing ones in the graph.
Parameters
-
value(required):- Type: Any serializable data (e.g., object, string, number, etc.)
- Description: The value to be stored in the node. This represents the data associated with the node.
-
id(optional):- Type: String
- Description: The unique identifier for the node. If not provided, the method will automatically generate a hash based on the
value.
Returns
id:- Type: String
- Description: The unique identifier (
id) of the inserted or updated node. If theidwas not provided, this will be the generated hash.
Behavior
-
Insertion or Update:
- If a node with the given
idalready exists, itsvalueis updated. - If no node exists with the given
id, a new node is created.
- If a node with the given
-
Automatic ID Generation:
- If no
idis provided, the method generates a unique hash using thegenerateHashfunction and the serializedvalue.
- If no
-
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 change using
sendDataandemit.
- The method emits an event to notify listeners of the change using
Examples
Example 1: Inserting a New Node
- A new node is created with the value
{ name: "Alice", age: 25 }. - Since no
idis provided, a unique hash is generated and returned.
Example 2: Updating an Existing Node
- A node with the ID
"user_123"is first created. - Later, the same node is updated with a new value (
age: 31).
Example 3: Automatic ID Generation
- The method automatically generates a unique hash for the node based on the serialized value
{ product: "Laptop", price: 999 }.
Example 4: Using a Custom ID
- A node is created with a custom ID
"custom_id_456".
Key Notes
- Persistence: All changes made by
putare persisted to storage usingsaveGraphToOPFS. This ensures durability of the data. - Notifications: The method emits events to notify listeners of the change. This is useful for real-time updates in applications.
- Hash Generation: If no
idis provided, the method usesgenerateHashto create a unique identifier based on the serializedvalue.
Error Handling
- If the
valuecannot be serialized (e.g., contains circular references), the method may throw an error during hash generation or storage. - Ensure that the
idprovided (if any) is unique to avoid overwriting existing nodes unintentionally.
Use Cases
- Adding New Data: Use
putto insert new nodes into the graph when creating records (e.g., users, products). - Updating Existing Data: Use
putto modify the value of an existing node by providing itsid. - Automatic ID Management: When you don’t need to manage IDs manually, rely on the automatic hash generation feature.