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
- Node.js (12.x or later)
- MySQL database with an existing schema
- Clone the repository:
git clone https://github.com/vkeenan/mysql-to-openapi.git
cd mysql-to-openapi
- Install the required dependencies:
npm install
- Create a
.env
file in the project root folder with the following variables:
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.
To generate the OpenAPI YAML file, run the following command:
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.
The script performs the following steps:
- Connects to the MySQL database using the provided credentials.
- Queries the
INFORMATION_SCHEMA.COLUMNS
table to retrieve table and column information for the specified schema. - 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.
- Generates model definitions in the
- 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.