> ## 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…

# **`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**

```javascript theme={null}
async db.get(id, callback = null)
```

***

## **Parameters**

1. **`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`.

2. **`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 `id` is invalid, this will be `null`.

* **`unsubscribe`** (optional):
  * A function that stops listening for real-time updates.
  * This property is only included if a `callback` is 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.

```javascript theme={null}
const { result } = await db.get('node-id');
console.log(result); // Logs the node object or null if not found
```

***

### **2. Retrieve a Node With Real-Time Updates**

If you want to react to changes in the node's value in real-time, provide a `callback` function.

```javascript theme={null}
const { result } = await db.get('node-id', (node) => {
  console.log('Node updated:', node);
});
```

* The `callback` will 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 the `unsubscribe` function returned by the method.

```javascript theme={null}
const { unsubscribe } = await db.get('node-id', (node) => {
  console.log('Node updated:', node);
});

// Stop listening after some time
setTimeout(() => {
  unsubscribe();
  console.log('Stopped listening for updates.');
}, 5000);
```

***

## **Error Handling**

* If the `id` is 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**

1. **Real-Time Updates**:
   * Real-time updates are only enabled if a `callback` is provided.
   * Ensure you call `unsubscribe` when updates are no longer needed to avoid memory leaks.

2. **Asynchronous Nature**:
   * The method is asynchronous (`async`), so always use `await` or handle it with `.then()`.

3. **Data Structure Assumptions**:
   * Assumes the underlying data structure (`this.graph`) is efficient for lookups by `id` (e.g., a `Map`).

***

## **Example Use Case**

Imagine you are building a real-time dashboard where users can monitor specific data points (nodes). You can use `db.get()` to fetch and subscribe to updates for each data point.

```javascript theme={null}
// Fetch and subscribe to updates for a specific node
const { unsubscribe } = await db.get('sensor-123', (node) => {
  console.log(`Sensor value updated: ${node.value}`);
});

// Simulate stopping updates after 10 seconds
setTimeout(() => {
  unsubscribe();
  console.log('Stopped monitoring sensor.');
}, 10000);
```

***

This documentation provides a clear understanding of how to use the `db.get()` method effectively in your application.
