多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.5 KiB

![npm version](https://img.shields.io/node/v/xmysql.svg)
7 years ago
[![Build Status](https://travis-ci.org/o1lab/xmysql.svg?branch=master)](https://travis-ci.org/o1lab/xmysql)
[![GitHub stars](https://img.shields.io/github/stars/o1lab/xmysql.svg?style=plastic)](https://github.com/o1lab/xmysql/stargazers)
7 years ago
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/o1lab/xmysql/master/LICENSE)
7 years ago
# xmysql: one command to generate REST APIs for **any** MySql database
## Why this ?
Generating REST APIs quickly for a MySql database which does not follow conventions of
frameworks such as rails, django etc is a small adventure that one rather like to avoid ..
7 years ago
<p align="center">
<img src="https://media.giphy.com/media/8gWrk3QZrjF1C/giphy.gif" alt="Rick & Morty"/>
</p>
Hence this.
Powered by node packages : ([express](https://github.com/expressjs/express), [mysql](https://github.com/mysqljs/mysql)) => { [xmysql](https://github.com/o1lab/xmysql) }
## Setup and Usage
```
npm install -g xmysql
```
```
xmysql -h localhost -u mysqlUsername -p mysqlPassword -d databaseName
```
```
http://localhost:3000
```
That's it!
7 years ago
## Features
7 years ago
* Generates API for **ANY** MySql database
* Serves APIs irrespective of naming conventions of primary keys, foreign keys, tables etc
* CRUD : Usual suspects
* Support for composite primary keys
* Pagination
* Sorting
* Fields
* Group By
* Group By, Order By
7 years ago
* Relations
* Run dynamic queries
Use HTTP clients like [Postman](https://www.getpostman.com/) or [similar tools](https://chrome.google.com/webstore/search/http%20client?_category=apps) to invoke REST API calls
7 years ago
____
7 years ago
Download [node](https://nodejs.org/en/download/current/),
[mysql](https://dev.mysql.com/downloads/mysql/)
[(setup mysql)](https://dev.mysql.com/doc/mysql-getting-started/en/#mysql-getting-started-installing),
[sample database](https://dev.mysql.com/doc/employee/en/employees-installation.html) -
if you haven't on your system.
## Root URL
Root URL (localhost:3000/) returns all REST API urls for each table in schema.
7 years ago
## CRUD APIs Usual Suspects
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName
* POST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName/:id
* PUT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName/:id
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName/count
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName/exists
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/parentTable/:id/childTable
* DELETE&nbsp; /api/tableName/:id
7 years ago
* POST&nbsp;&nbsp;&nbsp;&nbsp; /dynamic
## Other APIS
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tableName/describe
7 years ago
* GET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /api/tables
## Support for composite primary keys
#### ___ (three underscores)
```
/api/payments/103___JM555205
```
*___* : If there are multiple primary keys - seperate them by three underscores as shown
## Pagination
#### _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.
```
/api/payments?_size=50
```
```
/api/payments?_p=2
```
```
/api/payments?_p=2&_size=50
```
## Order by / Sorting
7 years ago
#### ASC
7 years ago
```
/api/payments?_sort=column1
```
eg: sorts ascending by column1
#### DESC
7 years ago
```
/api/payments?_sort=-column1
```
eg: sorts descending by column1
#### Multiple fields in sort
7 years ago
```
/api/payments?_sort=column1,-column2
```
eg: sorts ascending by column1 and descending by column2
## Fields
```
/api/payments?_fields=customerNumber,checkNumber
```
eg: gets only customerNumber and checkNumber in response of each record
```
/api/payments?_fields=-checkNumber
```
eg: gets all fields in table row but not checkNumber
## Group By
```
/api/offices/groupby?_fields=country
```
eg: SELECT country,count(*) FROM offices GROUP BY country
```
/api/offices/groupby?_fields=country,city
```
eg: SELECT country,city,count(*) FROM offices GROUP BY country,city
## Group By, Order By
```
/api/offices/groupby?_fields=country,city&sort=city
```
eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC
```
/api/offices/groupby?_fields=country,city&sort=city,country
```
eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC, country ASC
```
/api/offices/groupby?_fields=country,city&sort=city,-country
```
eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC, country DESC
7 years ago
## 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 127.0.0.1 option.
7 years ago
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"]
}
```
## Relational Tables
xmysql identifies foreign key relations automatically and provides GET api.
```
/api/customers/103/payments
```
eg: Customers is parent table and payments is child table. API invocation will result in all payments with customer 103.
7 years ago
## When to use ?
* You need just REST APIs without much hassle for (ANY) MySql database.
7 years ago
* 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, Authorisation etc - Not this.
7 years ago
* Other times not mentioned in when to use section