# 3. Set Up Database

Set up functions for different environments (Node.js, web browsers, and React Native) to manage LevelDB databases:

* **Node.js Setup**: Utilizes `leveldown` for creating databases. Use `createNodeDatabase(path)` to instantiate a LevelDown database at the given file path.

  ```typescript
  const db = createNodeDatabase('./path/to/database');
  ```
* **Web Browser Setup**: Leverages `level-js` for browser-based databases. Use `createWebDatabase(path)` to initialize a LevelDB database, offering basic functionality through the `LevelDB` instance.

  ```typescript
  const db = createWebDatabase('./data/mydb');
  ```
* **React Native Setup**: Employs `leveldown-nodejs-mobile` for creating databases on React Native platforms. Use the asynchronous `createNativeDatabase(path)` function, which returns a promise that resolves to a `LeveldownNodejsMobile` database instance.

  ```typescript
  const db = await createNativeDatabase('/path/to/database');

  ```

````typescript
// Setup for Node builds.
import LevelDownDB from "leveldown";
/**
 * Creates a LevelDown database instance at the specified path.
 *
 * @param dbLocationPath - The file system path where the database will be created
 * @returns A LevelDown database instance
 *
 * @example
 * ```typescript
 * const db = createNodeDatabase('./path/to/database');
 * ```
 */
export const createNodeDatabase = (dbLocationPath: string) => {
  console.log("Creating local database at path: ", dbLocationPath);
  const db = LevelDownDB(dbLocationPath);
  return db;
};

// Setup for Web builds.
import LevelDB from "level-js";
/**
 * Creates a new web database instance at the specified location path
 * @param dbLocationPath - The file system path where the database will be created
 * @returns A new LevelDB database instance
 * @remarks This function initializes a new LevelDB database at the specified path
 * and provides basic database functionality through the returned instance.
 * @example
 * const db = createWebDatabase('./data/mydb');
 */
export const createWebDatabase = (dbLocationPath: string) => {
  indexedDB.databases;
  console.log("Creating local database at path: ", dbLocationPath);
  const db = new LevelDB(dbLocationPath);
  return db;
};

// Setup for React Native builds.

// @ts-ignore
import LeveldownNodejsMobile from "leveldown-nodejs-mobile";
/**
 * Creates a native database instance using LeveldownNodejsMobile.
 *
 * @param dbLocationPath - The file system path where the database will be stored
 * @returns A Promise that resolves to a new LeveldownNodejsMobile database instance
 *
 * @example
 * ```typescript
 * const db = await createNativeDatabase('/path/to/database');
 * ```
 */
export const createNativeDatabase = async (dbLocationPath: string) => {
  return new LeveldownNodejsMobile(dbLocationPath);
};
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.railgun.org/developer-guide/wallet/getting-started/3.-set-up-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
