simillustra / mysql-to-openapi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MySQL to OpenAPI License: 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

Prerequisites

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

Setup

  1. Clone the repository:
git clone https://github.com/vkeenan/mysql-to-openapi.git
cd mysql-to-openapi
  1. Install the required dependencies:
npm install
  1. 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.

Usage

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.

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.

About

License:Other


Languages

Language:JavaScript 100.0%