Need help with xmysql?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

45 Stars 13 Forks MIT License 3 Commits 39 Opened issues


xmysql is now

Services available


Need anything else?

Contributors list

npm version GitHub license

Xmysql is now NocoDB
✨ The Open Source Airtable Alternative ✨

Xmysql : One command to generate REST APIs for any MySql database

Why this ?

xmysql gif

Generating REST APIs for a MySql database which does not follow conventions of frameworks such as rails, django, laravel etc is a small adventure that one like to avoid ..

Hence this.

Setup and Usage

xmysql requires node >= 7.6.0

npm install -g xmysql
xmysql -h localhost -u mysqlUsername -p mysqlPassword -d databaseName

That is it! Simple and minimalistic!

Happy hackery!

Example : Generate REST APIs for Magento

Powered by popular node packages : (express, mysql) => { xmysql }

xmysql gif

Boost Your Hacker Karma By Sharing :


  • Generates API for ANY MySql database :fire::fire:
  • Serves APIs irrespective of naming conventions of primary keys, foreign keys, tables etc :fire::fire:
  • Support for composite primary keys :fire::fire:
  • REST API Usual suspects : CRUD, List, FindOne, Count, Exists, Distinct
  • Bulk insert, Bulk delete, Bulk read :fire:
  • Relations
  • Pagination
  • Sorting
  • Column filtering - Fields :fire:
  • Row filtering - Where :fire:
  • Aggregate functions
  • Group By, Having (as query params) :fire::fire:
  • Group By, Having (as a separate API) :fire::fire:
  • Multiple group by in one API :fire::fire::fire::fire:
  • Chart API for numeric column :fire::fire::fire::fire::fire::fire:
  • Auto Chart API - (a gift for lazy while prototyping) :fire::fire::fire::fire::fire::fire:
  • XJOIN - (Supports any number of JOINS) :fire::fire::fire::fire::fire::fire::fire::fire::fire:
  • Supports views
  • Prototyping (features available when using local MySql server only)
    • Run dynamic queries :fire::fire::fire:
    • Upload single file
    • Upload multiple files
    • Download file
  • Health and version apis
  • Use more than one CPU Cores
  • Docker support and Nginx reverse proxy config :fire::fire::fire: - Thanks to @markuman
  • AWS Lambda Example - Thanks to @bertyhell :fire::fire::fire:

Use HTTP clients like Postman or similar tools to invoke REST API calls

Download node, mysql (setup mysql), sample database - if you haven't on your system.

API Overview

| HTTP Type | API URL | Comments | |-----------|----------------------------------|--------------------------------------------------------- | GET | / | Gets all REST APIs | | GET | /api/tableName | Lists rows of table | | POST | /api/tableName | Create a new row | | PUT | /api/tableName | Replaces existing row with new row | | POST :fire:| /api/tableName/bulk | Create multiple rows - send object array in request body| | GET :fire:| /api/tableName/bulk | Lists multiple rows - /api/tableName/bulk?ids=1,2,3 | | DELETE :fire:| /api/tableName/bulk | Deletes multiple rows - /api/tableName/bulk?ids=1,2,3 | | GET | /api/tableName/:id | Retrieves a row by primary key | | PATCH | /api/tableName/:id | Updates row element by primary key | | DELETE | /api/tableName/:id | Delete a row by primary key | | GET | /api/tableName/findOne | Works as list but gets single record matching criteria | | GET | /api/tableName/count | Count number of rows in a table | | GET | /api/tableName/distinct | Distinct row(s) in table - /api/tableName/distinct?_fields=col1| | GET | /api/tableName/:id/exists | True or false whether a row exists or not | | GET | /api/parentTable/:id/childTable | Get list of child table rows with parent table foreign key | | GET :fire:| /api/tableName/aggregate | Aggregate results of numeric column(s) | | GET :fire:| /api/tableName/groupby | Group by results of column(s) | | GET :fire:| /api/tableName/ugroupby | Multiple group by results using one call | | GET :fire:| /api/tableName/chart | Numeric column distribution based on (min,max,step) or(step array) or (automagic)| | GET :fire:| /api/tableName/autochart | Same as Chart but identifies which are numeric column automatically - gift for lazy while prototyping| | GET :fire:| /api/xjoin | handles join | | GET :fire:| /dynamic | execute dynamic mysql statements with params | | GET :fire:| /upload | upload single file | | GET :fire:| /uploads | upload multiple files | | GET :fire:| /download | download a file | | GET | /api/tableName/describe | describe each table for its columns | | GET | /api/tables | get all tables in database | | GET | /_health | gets health of process and mysql -- details query params for more details | | GET | /_version | gets version of Xmysql, mysql, node|

Relational Tables

xmysql identifies foreign key relations automatically and provides GET api.

eg: blogs is parent table and comments is child table. API invocation will result in all comments for blog primary key 103. :arrowheadingup:

Support for composite primary keys

___ (three underscores)


___ : If there are multiple primary keys - separate them by three underscores as shown


_p & _size

_p indicates page and _size indicates size of response rows

By default 20 records and max of 100 are returned per GET request on a table.


When _size is greater than 100 - number of records defaults to 100 (i.e maximum)

When _size is less than or equal to 0 - number of records defaults to 20 (i.e minimum)

Order by / Sorting



eg: sorts ascending by column1



eg: sorts descending by column1

Multiple fields in sort


eg: sorts ascending by column1 and descending by column2

Column filtering / Fields


eg: gets only customerNumber and checkNumber in response of each record

eg: gets all fields in table row but not checkNumber

Row filtering / Where

Comparison operators

eq      -   '='         -  (colName,eq,colValue)
ne      -   '!='        -  (colName,ne,colValue)
gt      -   '>'         -  (colName,gt,colValue)
gte     -   '>='        -  (colName,gte,colValue)
lt      -   '

Use of comparison operators


Logical operators

~or     -   'or'
~and    -   'and'
~xor    -   'xor'

Use of logical operators

eg: simple logical expression


eg: complex logical expression


eg: logical expression with sorting(sort), pagination(p), column filtering (fields) ``` /api/payments?where=(amount,gte,1000)&sort=-amount&p=2&fields=customerNumber ```

eg: filter of rows using where is available for relational route URLs too. ``` /api/offices/1/employees?where=(jobTitle,eq,Sales%20Rep) ```



Works similar to list but only returns top/one result. Used in conjunction with _where :arrowheadingup:



Returns number of rows in table :arrowheadingup:



Returns true or false depending on whether record exists :arrowheadingup:

Group By Having as query params



eg: SELECT country,count(*) FROM offices GROUP BY country


eg: SELECT country,count(1) as _count FROM offices GROUP BY country having _count > 1

Group By Having as API



eg: SELECT country,count(*) FROM offices GROUP BY country


eg: SELECT country,city,count(*) FROM offices GROUP BY country,city


eg: SELECT country,city,count(*) as _count FROM offices GROUP BY country,city having _count > 1

Group By, Order By



eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC


eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC, country ASC


eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC, country DESC

Aggregate functions



response body [ { "min_of_amount": 615.45, "max_of_amount": 120166.58, "avg_of_amount": 32431.645531, "sum_of_amount": 8853839.23, "stddev_of_amount": 20958.625377426568, "variance_of_amount": 439263977.71130896 } ]

eg: retrieves all numeric aggregate of a column in a table


response body [ { "min_of_priceEach": 26.55, "max_of_priceEach": 214.3, "avg_of_priceEach": 90.769499, "sum_of_priceEach": 271945.42, "stddev_of_priceEach": 36.576811252187795, "variance_of_priceEach": 1337.8631213781719, "min_of_quantityOrdered": 6, "max_of_quantityOrdered": 97, "avg_of_quantityOrdered": 35.219, "sum_of_quantityOrdered": 105516, "stddev_of_quantityOrdered": 9.832243813502942, "variance_of_quantityOrdered": 96.67301840816688 } ]

eg: retrieves numeric aggregate can be done for multiple columns too

Union of multiple group by statements


:fire::fire:[ HOTNESS ALERT ]

Group by multiple columns in one API call using _fields query params - comes really handy


response body {
"Sales Rep":17 }, {
"President":1 }, {
"Sale Manager (EMEA)":1 }, {
"Sales Manager (APAC)":1 }, {
"Sales Manager (NA)":1 }, {
"VP Marketing":1 }, {
"VP Sales":1 } ], "reportsTo":[
"1002":2 }, {
"1056":4 }, {
"1088":3 }, {
"1102":6 }, {
"1143":6 }, {
"1621":1 } {
"":1 }, ] }



:fire::fire::fire::fire::fire::fire: [ HOTNESS ALERT ]

Chart API returns distribution of a numeric column in a table

It comes in SEVEN powerful flavours

  1. Chart : With min, max, step in query params :fire::fire: :arrowheadingup:

This API returns the number of rows where amount is between (0,25000), (25001,50000) ...



[ { "amount": "0 to 25000", "_count": 107 }, { "amount": "25001 to 50000", "_count": 124 }, { "amount": "50001 to 75000", "_count": 30 }, { "amount": "75001 to 100000", "_count": 7 }, { "amount": "100001 to 125000", "_count": 5 }, { "amount": "125001 to 150000", "_count": 0 } ]

  1. Chart : With step array in params :fire::fire: :arrowheadingup:

This API returns distribution between the step array specified



[ { "amount": "0 to 10000", "_count": 42 }, { "amount": "10001 to 20000", "_count": 36 }, { "amount": "20001 to 70000", "_count": 183 }, { "amount": "70001 to 140000", "_count": 12 } ]

  1. Chart : With step pairs in params :fire::fire: :arrowheadingup:

This API returns distribution between each step pair



[ {"amount":"0 to 50000","_count":231}, {"amount":"40000 to 100000","_count":80} ]

  1. Chart : with no params :fire::fire: :arrowheadingup:

This API figures out even distribution of a numeric column in table and returns the data


Response [ { "amount": "-9860 to 11100", "_count": 45 }, { "amount": "11101 to 32060", "_count": 91 }, { "amount": "32061 to 53020", "_count": 109 }, { "amount": "53021 to 73980", "_count": 16 }, { "amount": "73981 to 94940", "_count": 7 }, { "amount": "94941 to 115900", "_count": 3 }, { "amount": "115901 to 130650", "_count": 2 } ]

  1. Chart : range, min, max, step in query params :fire::fire: :arrowheadingup:

This API returns the number of rows where amount is between (0,25000), (0,50000) ... (0,maxValue)

Number of records for amount is counted from min value to extended Range instead of incremental steps



[ { "amount": "0 to 25000", "_count": 107 }, { "amount": "0 to 50000", "_count": 231 }, { "amount": "0 to 75000", "_count": 261 }, { "amount": "0 to 100000", "_count": 268 }, { "amount": "0 to 125000", "_count": 273 } ]

  1. Range can be specified with step array like below

[ { "amount": "0 to 10000", "_count": 42 }, { "amount": "0 to 20000", "_count": 78 }, { "amount": "0 to 70000", "_count": 261 }, { "amount": "0 to 140000", "_count": 273 } ]

  1. Range can be specified without any step params like below

[ { "amount": "-9860 to 11100", "_count": 45 }, { "amount": "-9860 to 32060", "_count": 136 }, ...


Please Note: _fields in Chart API can only take numeric column as its argument.


Identifies numeric columns in a table which are not any sort of key and applies chart API as before - feels like magic when there are multiple numeric columns in table while hacking/prototyping and you invoke this API.


[ { "column": "amount", "chart": [ { "amount": "-9860 to 11100", "_count": 45 }, { "amount": "11101 to 32060", "_count": 91 }, { "amount": "32061 to 53020", "_count": 109 }, { "amount": "53021 to 73980", "_count": 16 }, { "amount": "73981 to 94940", "_count": 7 }, { "amount": "94941 to 115900", "_count": 3 }, { "amount": "115901 to 130650", "_count": 2 } ] } ]


Xjoin query params and values:

_join           :   List of tableNames alternated by type of join to be made (_j, _ij,_ lj, _rj)
alias.tableName :   TableName as alias
_j              :   Join [ _j => join, _ij => ij, _lj => left join , _rj => right join)
_onNumber       :   Number 'n' indicates condition to be applied for 'n'th join between (n-1) and 'n'th table in list  

Simple example of two table join:

Sql join query:

SELECT pl.field1, pr.field2
FROM productlines as pl
    JOIN products as pr
        ON pl.productline = pr.productline

Equivalent xjoin query API:


Multiple tables join

Sql join query:

SELECT pl.field1, pr.field2, ord.field3
FROM productlines as pl
    JOIN products as pr
        ON pl.productline = pr.productline
    JOIN orderdetails as ord
        ON pr.productcode = ord.productcode

Equivalent xjoin query API:



pl.productlines => productlines as pl

_j => join

pr.products => products as pl

_on1 => join condition between productlines and products => (pl.productline,eq,pr.productline)

_on2 => join condition between products and orderdetails => (pr.productcode,eq,ord.productcode)

Example to use : _fields, _where, _p, _size in query params



[{"pl_productline":"Classic Cars","pr_productName":"1972 Alfa Romeo GTA"}]

Please note : Xjoin response has aliases for fields like below aliasTableName + '' + columnName.
eg: pl.productline in _fields query params - returns as pl
productline in response.

Run dynamic queries


Dynamic queries on a database can be run by POST method to URL localhost:3000/dynamic

This is enabled ONLY when using local mysql server i.e -h localhost or -h option.

Post body takes two fields : query and params.

query: SQL query or SQL prepared query (ones with ?? and ?)

params : parameters for SQL prepared query ``` POST /dynamic

    "query": "select * from ?? limit 1,20",
    "params": ["customers"]
POST /dynamic URL can have any suffix to it - which can be helpful in prototyping


POST /dynamic/weeklyReport ```

POST /dynamic/user/update

Upload single file


POST /upload

Do POST operation on /upload url with multiform 'field' assigned to local file to be uploaded

eg: curl --form [email protected]/Users/me/Desktop/a.png http://localhost:3000/upload

returns uploaded file name else 'upload failed'

(Note: POSTMAN has issues with file uploading hence examples with curl)

Upload multiple files


POST /uploads

Do POST operation on /uploads url with multiform 'fields' assigned to local files to be uploaded

Notice 's' near /api/uploads and files in below example

eg: curl --form [email protected]/Users/me/Desktop/a.png --form [email protected]/Users/me/Desktop/b.png http://localhost:3000/uploads

returns uploaded file names as string

Download file



For upload and download of files -> you can specify storage folder using -s option Upload and download apis are available only with local mysql server





Shows up time of Xmysql process and mysql server



Provides more details on process.

Infact passing any query param gives detailed health output: example below

http://localhost:3000/health?voila ``` {"processuptime":107.793,"mysqluptime":"2905","ostotalmemory":17179869184,"osfreememory":2573848576,"osloadaverage":[2.052734375,2.12890625,2.11767578125],"v8heapstatistics":{"totalheapsize":24735744,"totalheapsizeexecutable":5242880,"totalphysicalsize":23735016,"totalavailablesize":1475411128,"usedheapsize":18454968,"heapsizelimit":1501560832,"mallocedmemory":8192,"peakmallocedmemory":11065664,"doeszap_garbage":0}} ```





When to use ?


  • You need just REST APIs for (ANY) MySql database at blink of an eye (literally).
  • You are learning new frontend frameworks and need REST APIs for your MySql database.
  • You are working on a demo, hacks etc

When NOT to use ?


  • If you are in need of a full blown MVC framework, ACL, Validations, Authorisation etc - its early days please watch/star this repo to keep a tab on progress.

Command line options



-V, --version            Output the version number
-h, --host <n>           Hostname of database -&gt; localhost by default
-u, --user <n>           Username of database -&gt; root by default
-p, --password <n>       Password of database -&gt; empty by default
-d, --database <n>       database schema name
-r, --ipAddress <n>      IP interface of your server / localhost by default    
-n, --portNumber <n>     Port number for app -&gt; 3000 by default
-o, --port <n>           Port number of mysql -&gt; 3306 by default
-a, --apiPrefix <n>      Api url prefix -&gt; /api/ by default
-s, --storageFolder <n>  Storage folder -&gt; current working dir by default (available only with local)
-i, --ignoreTables <n>   Comma separated table names to ignore
-c, --useCpuCores <n>    Specify number of cpu cores to use / 1 by default / 0 to use max
-y, --readOnly           readonly apis -&gt; false by default    
-h, --help               Output usage information


$ xmysql -u username -p password -d databaseSchema

Boost Your Hacker Karma By Sharing :



Simply run with

docker run -p 3000:80 -d markuman/xmysql:0.4.2

The best way for testing is to run mysql in a docker container too and create a docker network, so that

can access the
container with a name from docker network.
  1. Create network
    • docker network create mynet
  2. Start mysql with docker name
    and bind to docker network
    • docker run --name some-mysql -p 3306:3306 --net mynet -e MYSQL_ROOT_PASSWORD=password -d markuman/mysql
  3. run xmysql and set env variable for
    from step 2
    • docker run -p 3000:80 -d -e DATABASE_HOST=some-mysql --net mynet markuman/xmysql

You can also pass the environment variables to a file and use them as an option with docker like

docker run --env-file ./env.list -p 3000:80 --net mynet -d markuman/xmysql

environment variables which can be used:


Furthermore, the docker container of xmysql is listen on port 80. You can than request it just with

in other services running in the same docker network.

Debugging xmysql in docker.

Given you've deployed your xmysql docker container like

docker run -d \
--network local_dev \
--name xmysql \
-p 3000:80 \
-e DATABASE_HOST=mysql_host \

but the response is just


then obviously the connection to your mysql database failed.

  1. attache to the xmysql image
    • docker exec -ti xmysql
  2. install mysql cli client
    • apk --update --no-cache add mysql-client
  3. try to access your mysql database
    • mysql-client -h mysql_host
  4. profit from the
    error output and improve the environment variables for mysql

Nginx Reverse Proxy Config with Docker


This is a config example when you use nginx as reverse proxy

events {
   worker_connections 1024;

} http { server { server_name; listen 80 ; location / { rewrite ^/(.*) /$1 break; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass; } } }


  1. create a docker network
    docker network create local_dev
  2. start a mysql server
    docker run -d --name mysql -p 3306:3306 --network local_dev -e MYSQL_ROOT_PASSWORD=password mysql
  3. start xmysql
    docker run -d --network local_dev --name xmyxql -e DATABASE_NAME=sys -e DATABASE_HOST=mysql -p 3000:80 markuman/xmysql:0.4.2
  4. start nginx on host system with the config above
    sudo nginx -g 'daemon off;' -c /tmp/nginx.conf
  5. profit

When you start your nginx proxy in a docker container too, use as

value of xmysql. E.g.
proxy_pass http://xmysql
(remember, xmysql runs in it's docker container already on port 80).

Tests : setup on local machine


docker-compose run test
* Requires
to be installed on your machine.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.