1.2.6 • Published 7 years ago

mysql-restapi v1.2.6

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
7 years ago

mysql-restapi

Module for gateway between MySql DB & RestAPI.

Express framework & mysql package required for database connection.

API

Before Installation

Make sure you have setup express project Go here to find out more

Make sure mysql module is instsalled before installing mysql-restapi, or install it

$ npm install mysql --save

Installation

$ npm install mysql-restapi

First load express and mysql

var express = require('express');
var mysql = require('mysql');
var mysqlrestapi  = require('mysql-restapi');
var dbconfig = require('./connections');
var app = express();
var api = mysqlrestapi(app, dbconfig);

Create file named connections.js on root

/* Create database connetion */
var mysql = require('mysql');
var connection=mysql.createPool({
    host:'localhost',
    user:'DBUSER',
    password:'DBPASSWORD',
    database:'DATABASE'
});

/* Setting parameters for API url customization */
var settingOptions = {
    apiURL:'api', // Custom parameter to create API urls
    paramPrefix:'_' // Parameter for field seperation in API url
};

/* Setting options to handle cross origin resource sharing issue */
var corsOptions = {
  origin: "*", // Website you wish to allow to connect
  methods: "GET, POST, PUT, DELETE", // Request methods you wish to allow
  preflightContinue: false,
  optionsSuccessStatus: 200,
  allowedHeaders: "Content-Type", // Request headers you wish to allow
  credentials: true // Set to true if you need the website to include cookies in the requests sent
};

module.exports={connection, settingOptions, corsOptions};

How to use

After completing setup given above follow these options to run APIs:

CRUD (GET, POST/CREATE, PUT/UPDATE, DELETE) request APIs

Request MethodRequest URLPurposeParameters
GETYOUR_DOMAIN/api/crud/:tablenameFetch full table-
GETYOUR_DOMAIN/api/crud/:tablename/:rowIDFetch record via Row ID-
POST        YOUR_DOMAIN/api/crud/:tablenameCreate/Enter record in table{"fieldname":"fieldvalue",---}
PUTYOUR_DOMAIN/api/crud/:tablename/:rowIDUpdate record via ID{"fieldname":"fieldvalue",---}
DELETEYOUR_DOMAIN/api/crud/:tablename/:rowIDDelete record via Row ID-

APIs to fetch data

Request MethodRequest URLPurpose
GETYOUR_DOMAIN/api/crud/:tablename?_limit=0,100Passing limit in result
GETYOUR_DOMAIN/api/crud/:tablename?_orderfieldname=DESCSort order in result
GETYOUR_DOMAIN/api/crud/:tablename?_fields=fieldname1,fieldname2Fetch selected fields
GETYOUR_DOMAIN/api/crud/:tablename?fieldnameLIKE=%fieldvalue%Like query, varry % position
GETYOUR_DOMAIN/api/crud/:tablename?_limit=0,100&_orderfieldname =DESC&_fields=fieldname1,fieldname2fieldnameLIKE=%fieldvalue%All together

Custom query request APIs

Request MethodRequest URLPurposeParameters
GETYOUR_DOMAIN/api/custom/:queryRun custom query & get result-
POSTYOUR_DOMAIN/api/customRun custom query & get result{"query":"SQL_QUERY"}

Login/check fields Post API

Request MethodRequest URLPurposeParameters
POSTYOUR_DOMAIN/api/check/:tablenameCheck records availablity status{"fieldname":"fieldvalue",---}