# @storyshelf/db-mysql

> MySQL and MariaDB database adapter for StoryShelf via mysql2 and Drizzle ORM, with PlanetScale and TiDB presets.

Source: https://storyshelf.js.org/packages/db-mysql/

`@storyshelf/db-mysql` connects StoryShelf to MySQL or MariaDB through `mysql2` and Drizzle ORM. It implements the same `DatabaseAdapter` contract as [SQLite](../db-sqlite/) and [Postgres](../db-postgres/), so the rest of the application does not change when you switch databases.

## Install

```sh
npm i @storyshelf/db-mysql
```

- [npm package](https://www.npmjs.com/package/@storyshelf/db-mysql) — install tarballs and version history.
- [Source on GitHub](https://github.com/GuptaSiddhant/StoryShelf/tree/main/packages/db-mysql) — package directory on `main`.

## Configure

```ts
import { createMysqlDatabase } from "@storyshelf/db-mysql";

const database = createMysqlDatabase({
  url: process.env.DATABASE_URL!, // mysql://user:pass@host:3306/shelf
});
```

Provide one of:

- `url` / `connectionString` — a MySQL connection URL.
- `host` (with `port`, `user`, `password`, `database`, `ssl`, `connectionLimit`) — discrete options; the pool defaults to 10 connections.
- `client` — a preconfigured `mysql2` pool or connection. When you inject a client you own its lifecycle; the adapter only closes pools it created.

Migrations run inside the adapter's `migrate` hook — await them via `app.lifecycle.setup()` after creating the app. The adapter implements `DatabaseAdapter` (`insert`, `update`, `get`, `remove`, `list`, `count`, `all`) plus a `ping` used by health checks.

## Presets

Two preset entry points exist so generated servers and the CLI can import a provider-named factory:

```ts
import { createPlanetscaleDatabase } from "@storyshelf/db-mysql/planetscale";
import { createTidbDatabase } from "@storyshelf/db-mysql/tidb";
```

Today both are thin aliases over the same `mysql2` pool: PlanetScale and TiDB Serverless speak the MySQL wire protocol, so the standard options above work unchanged (use `ssl` as your provider requires). They take the same options as `createMysqlDatabase`.

## Other exports

| Subpath                       | Contents                                                                                    |
| ----------------------------- | ------------------------------------------------------------------------------------------- |
| `@storyshelf/db-mysql/schema` | The Drizzle table definitions.                                                              |
| `@storyshelf/db-mysql/ddl`    | The canonical `DDL` string for every table and index, for provisioning outside the adapter. |

## When to use it

Choose MySQL or MariaDB when that is what your team already runs or your platform provides. Pair it with [local storage](../storage-local/) for single-node deployments, or [S3-compatible storage](../storage-s3/) when instances do not share a filesystem.
