Giter Site home page Giter Site logo

swaggerql / swaggerql Goto Github PK

View Code? Open in Web Editor NEW
53.0 4.0 11.0 805 KB

Easily and simply convert SQL database into a REST API with Swagger documentation

License: MIT License

JavaScript 96.43% Dockerfile 1.37% Shell 2.20%
swagger-api swagger-ui swagger mariadb mysql database postgresql sqlite sql oracle

swaggerql's Introduction

SwaggerQL

Build Status CodeQL Docker swaggerql-mysql Docker swaggerql-mariadb Docker swaggerql-postgres Docker swaggerql-oracle Docker swaggerql-sqlite

Swagger + SQL = Simple reference microservice with transparent documentation and no coding.
All you need to do is create a Swagger file and specify SQL queries in the description.

When you should use it:

  • you need an internal microservice with simple queries to an existing database
  • you quickly need a prototype of a reference service
  • you need to test data in the database
  • you need a stub application with database access to test the deployment scripts

Screenshot

Getting Started

Install the appropriate database module before using the SwaggerQL:

  • pg for PostgreSQL and Amazon Redshift
  • mysql2 for MySQL
  • mysql for MariaDB or MySQL
  • sqlite3 for SQLite3
  • mssql for MSSQL
  • oracledb and Oracle instant-client for Oracle
Run SwaggerQL with PostgreSQL
npm install swaggerql
npm install pg

Create config/local.yaml

client: pg
connection:
  host: 127.0.0.1
  user: your_database_user
  password: your_database_password
  database: myapp_test

Run SwaggerQL

$(npm bin)/swaggerql

And try http://0.0.0.0:8000

Run SwaggerQL with Oracle
npm install swaggerql
npm install oracledb

Install Oracle instant-client

Create config/local.yaml

client: oracledb
connection:
  user: your_database_user
  password: your_database_password
  connectString: (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SID=MY_SID)))
pool:
  min: 0
  max: 3

Run SwaggerQL

$(npm bin)/swaggerql

And try http://0.0.0.0:8000

More examples in the repository.

Configuration

Connect to DB

The knex library is used to connect to various databases. The most common configuration file format is:

client: <driver>
connection:
  host: 127.0.0.1
  user: your_database_user
  password: your_database_password
  database: myapp_test

More examples of connection configurations in Knex documentation

See node-config documentation for information about naming, load order and format of configuration files.

REST API

Build APIs by describing them in OpenAPI document specification and importing them via YAML or JSON to construct your own REST API.

The only difference is that a SQL query is inserted into the description field.

paths:
  /two:
    get:
      summary: One plus one equals two
      description: |
        SELECT 1 + 1
      responses:
        200:
          description: OK

The description can use Markdown with a specified SQL query:

description: |
  Add one to one

  ```sql
  SELECT 1 + 1
  ```

Query Parameter Binding

You can pass parameters to SQL query from path, query, form parameters described in Swagger file. Use named bindings, such as :name are interpreted as values and :name: interpreted as identifiers.

paths:
  /increment:
    get:
      summary: Increment of N per one
      description: |
        SELECT 1 + :n
      parameters:
      - name: n
        in: query
        description: Number
        required: true
        style: form
        explode: true
        schema:
          type: integer
      responses:
        200:
          description: OK

Execution options

Query expression can be handled as a transaction if the client sends X-Transaction header. Sometimes there's need to release cursors in the database.

paths:
  /compute:
    get:
      summary: Adding the results of a calculation
      description: |
        insert into table
        select function()
      parameters:
      - name: X-Transaction
        in: header
        description: Run Query in transaction wrapper
        schema:
          type: boolean
          default: true

CLI

Configuration options can be overridden via command-line arguments or environment variables. Priorities are the following:

  • A command-line option has the highest priority. It overrides the environment variable and config file value.
  • An environment variable has second priority. It overrides the config file value.
  • A config file value has the lowest priority.
  • If there isn't a command-line option, environment variable or config file option specified, the default is used.

Run $(npm bin)/swaggerql --help for more information.

Usage: swaggerql [options]

Options:
  -V, --version                output the version number
  -i, --input-spec <path>      path to specification file (default: "openapi.yaml")
  -p, --port <number>          http port to start server (default: 8000)
  -d, --client <name>          name of client SQL driver
  -c, --connection <dsn|json>  connection options to the appropriate database client
  -l, --log-level <level>      logging level: debug, info, warn, error (default: "info")
  -h, --help                   output usage information

Environment variables:

  • SWAGGERQL_INPUT_SPEC — path to specification file
  • SWAGGERQL_PORT — http port to start server
  • SWAGGERQL_CLIENT — name of client SQL driver
  • SWAGGERQL_CONNECTION — connection options to the appropriate database client
  • SWAGGERQL_LOG_LEVEL — logging level

Docker

docker run -it --rm -p 8000:8000 \
        -v $(pwd)/config/local.yaml:/app/config/production.yaml \
        -v $(pwd)/openapi.yaml:/app/openapi.yaml \
        swaggerql/swaggerql-mysql

Available Docker containers:

swaggerql's People

Contributors

dependabot[bot] avatar levonet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swaggerql's Issues

Trouble trying to run it

Hi & thanks for this promising and precious tool!

I'm having issues trying to run it.
The installation created a directory called openapi.yaml (vs the file) I fixed that and tried to run the container I've got

$ docker run -it --rm -p 8000:8000 \
  -v $(pwd)/config/local.yaml:/app/config/production.yaml \
  -v $(pwd)/openapi.yaml:/app/openapi.yaml \
  swaggerql/swaggerql-mysql
yarn run v1.22.4
$ bin/swaggerql
Knex: run
$ npm install mysql --save
Cannot find module 'mysql'
Error: Cannot find module 'mysql'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Client_MySQL._driver (/app/node_modules/knex/lib/dialects/mysql/index.js:31:12)
    at Client_MySQL.initializeDriver (/app/node_modules/knex/lib/client.js:230:26)
    at Client_MySQL.Client (/app/node_modules/knex/lib/client.js:68:10)
    at new Client_MySQL (/app/node_modules/knex/lib/dialects/mysql/index.js:20:10)
    at Knex (/app/node_modules/knex/lib/knex.js:53:28)
    at Object.connect (/app/lib/query-builder/adapter/mysql2.js:4:24)
(node:28) UnhandledPromiseRejectionWarning: Error: Knex: run

Any help appreciated
TX

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.