> For the complete documentation index, see [llms.txt](https://discord-base.gitbook.io/discord-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://discord-base.gitbook.io/discord-base/utilities/remote-mysql.md).

# Remote MySQL

The Database module provides a simple interface for connecting to a MySQL database using the mysql2/promise library. It manages connection states and offers methods for executing safe queries

## States

The `DBStates` enum defines the various states of the database connection:

<table><thead><tr><th width="165">state</th><th>description</th></tr></thead><tbody><tr><td>connecting</td><td>The connection is in the process of being established.</td></tr><tr><td>connected</td><td>The connection has been successfully established.</td></tr><tr><td>error</td><td>An error occurred during the connection or query execution.</td></tr><tr><td>closed</td><td>The connection has been closed.</td></tr></tbody></table>

## Options

The `DBOptions` interface allows you to specify optional parameters for the database connection:

```typescript
interface DBOptions {
	host?: string; // Database host (default: process.env.DB_HOST)
	user?: string; // Database user (default: process.env.DB_USER)
	password?: string; // Database password (default: process.env.DB_PASSWORD)
	database?: string; // Database name (default: process.env.DB_NAME)
	port?: number; // Database port (default: process.env.DB_PORT)
	alias?: string; // Optional alias for the connection
}
```

## Methods

### `connect`

Establishes a connection to the database using provided options or environment variables.

```typescript
private async connect(): Promise<void>;
```

### `end`

Forcefully closes the database connection.

```typescript
public async end(): Promise<void>;
```

### `query`

Executes a SQL query on the database and returns the result.

```typescript
public async query<T>(sql: string, values?: any[]): Promise<T[]>;
```

<table><thead><tr><th width="165">parameter</th><th>description</th></tr></thead><tbody><tr><td>sql</td><td>The SQL query string.</td></tr><tr><td>connected</td><td>Optional array of values to be replaced in the query.</td></tr></tbody></table>

### `connection`

Returns the database connection handle.

```typescript
get connection(): Connection | null;
```

### `quit`

Safely closes the database connection.

```typescript
private async quit(): Promise<void>;
```

## Example

Here’s how you can use the `DBClass` to connect to a database and execute a query:

```typescript
import DB, { DBOptions } from "@utils/db";

const options: DBOptions = {
	host: "localhost",
	user: "root",
	password: "password",
	database: "database",
	port: 3306,
};

const db = new DB(options);

try {
	const results = await db.query<any>("SELECT * FROM users");
	console.log(results);
} catch (error) {
	console.error("Database error:", error);
}

```

This example demonstrates how to create a new database connection, execute a query to retrieve all users from the `users` table.
