An open API service indexing awesome lists of open source software.

https://github.com/vkeenan/mysql-to-openapi


https://github.com/vkeenan/mysql-to-openapi

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

          

# MySQL to OpenAPI [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

MySQL to OpenAPI is a simple script that generates an OpenAPI 2.0 (formerly known as Swagger) YAML file from your MySQL database schema. The generated YAML file will include definitions for each table in your schema, as well as basic CRUD operations for each table.

Read about how this open-source contribution was made: [Using ChatGPT to generate Swagger from MySQL with mysql-to-openapi.js](https://salesforcedevops.net/index.php/2023/03/22/using-chatgpt-to-generate-swagger-from-mysql-with-mysql-to-openapi-js/)

## Prerequisites

- Node.js (12.x or later)
- MySQL database with an existing schema

## Setup

1. Clone the repository:

```sh
git clone https://github.com/vkeenan/mysql-to-openapi.git
cd mysql-to-openapi
```

2. Install the required dependencies:

```sh
npm install
```

3. Create a `.env` file in the project root folder with the following variables:

```ini
DB_HOST=your_database_host
DB_USER=your_database_user
DB_PASSWORD=your_database_password
DB_NAME=your_database_name
```

Replace `your_database_host`, `your_database_user`, `your_database_password`, and `your_database_name` with the appropriate values for your MySQL database.

## Usage

To generate the OpenAPI YAML file, run the following command:

```sh
node index.js
```

The script will generate a YAML file named `${DB_NAME}.yaml` in the project root folder. This file contains the OpenAPI definitions and paths for each table in your schema.

## How It Works

The script performs the following steps:

1. Connects to the MySQL database using the provided credentials.
2. Queries the `INFORMATION_SCHEMA.COLUMNS` table to retrieve table and column information for the specified schema.
3. Iterates over the query results and processes each table and column:
- Generates model definitions in the `definitions` section of the OpenAPI YAML file.
- Creates request and response objects for each table.
- Constructs basic CRUD operations for each table in the `paths` section of the OpenAPI YAML file.
4. Writes the generated OpenAPI YAML file to the project root folder.

The generated OpenAPI YAML file can be used as a starting point for further customization and development of an API.

```