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.
db.get() Method Documentation
The db.get() method is a powerful utility for retrieving nodes from a graph-like data structure by their unique ID. It also supports real-time updates through an optional callback mechanism, allowing your application to react dynamically to changes in the data.
Method Signature
Parameters
-
id(string, required):- The unique identifier of the node you want to retrieve.
- Must be a valid string. If the ID is invalid or not found, the method will return
null.
-
callback(function, optional):- A function that will be executed when the node is retrieved and whenever its value changes in real-time.
- If no callback is provided, the method will simply return the node without setting up real-time updates.
Return Value
The method returns an object with the following properties:-
result:- The node object corresponding to the provided
id. - If the node is not found or the
idis invalid, this will benull.
- The node object corresponding to the provided
-
unsubscribe(optional):- A function that stops listening for real-time updates.
- This property is only included if a
callbackis provided.
Usage Examples
1. Retrieve a Node Without Real-Time Updates
Use this approach when you only need to fetch the node once and do not require updates.2. Retrieve a Node With Real-Time Updates
If you want to react to changes in the node’s value in real-time, provide acallback function.
- The
callbackwill be executed immediately with the initial node value. - It will also be called whenever the node’s value changes in the database.
3. Stop Listening for Real-Time Updates
To stop receiving updates, call theunsubscribe function returned by the method.
Error Handling
- If the
idis not a valid string, the method logs an error and returns{ result: null }. - If no node is found with the provided
id, the method logs an error and returns{ result: null }.
Key Notes
-
Real-Time Updates:
- Real-time updates are only enabled if a
callbackis provided. - Ensure you call
unsubscribewhen updates are no longer needed to avoid memory leaks.
- Real-time updates are only enabled if a
-
Asynchronous Nature:
- The method is asynchronous (
async), so always useawaitor handle it with.then().
- The method is asynchronous (
-
Data Structure Assumptions:
- Assumes the underlying data structure (
this.graph) is efficient for lookups byid(e.g., aMap).
- Assumes the underlying data structure (
Example Use Case
Imagine you are building a real-time dashboard where users can monitor specific data points (nodes). You can usedb.get() to fetch and subscribe to updates for each data point.
This documentation provides a clear understanding of how to use the
db.get() method effectively in your application.