Giter Club home page Giter Club logo

xmysql's Introduction

npm version Build Status GitHub stars GitHub license

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 ..

Rick & Morty

Hence this.

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

Setup and Usage

npm install -g xmysql
xmysql -h localhost -u mysqlUsername -p mysqlPassword -d databaseName
http://localhost:3000

That's it!

Features

  • 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
  • Relations
  • Run dynamic queries

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.

Root URL

Root URL (localhost:3000/) returns all REST API urls for each table in schema.

CRUD APIs Usual Suspects

  • GET       /api/:tableName
  • POST      /api/:tableName
  • GET       /api/:tableName/:id
  • PUT       /api/:tableName/:id
  • GET       /api/:tableName/count
  • GET       /api/:tableName/exists
  • GET       /api/:parentTable/:id/:childTable
  • DELETE  /api/:tableName/:id
  • POST     /dynamic

Other APIS

  • GET      /api/:tableName/describe
  • GET      /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

Sorting

_sort

/api/payments?_sort=column1

eg: sorts ascending by column1

/api/payments?_sort=-column1

eg: sorts descending by column1

/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

Run dynamic queries

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

This is enabled only in local i.e -h localhost or -h 127.0.0.1 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"]
    }

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.

When to use ?

  • You need just REST APIs without much hassle for (ANY) MySql database.
  • 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.
  • Other times not mentioned in when to use section

xmysql's People

Contributors

o1lab avatar

Watchers

 avatar  avatar  avatar

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.