1.1.0 • Published 9 years ago

hapi-mysql-routes-plugin v1.1.0

Weekly downloads
9
License
Proprietary
Repository
github
Last release
9 years ago

hapi-mysql-routes-plugin Build Status

Hapi Mysql Routes Plugin registers basic http routes and turns them into RESTful API endpoints that interact with a Mysql resource. The routes are:

Install

$ npm install --save hapi-mysql-routes-plugin

Example

import hapiMysqlRoutes from 'hapi-mysql-routes-plugin';
import {Server} from 'hapi';

const port = 9000;
const server = new Server();

server.connection({ port: port, labels: ['api'] });

const api = server.select('api');

api.register(
  [
    {
      register: hapiMysqlRoutes,
      options: {
        mysqlConfig: mysql,
        primaryKey: 'id',
        tableName: 'users',
      }
    }
  ],
  function(err) {
    if (err) {
      server.log('error', err);
    }
  }
);

server.start(function() {
  server.log('info', 'Server running at: ' + server.info.uri);
});

Routes

GET /

This is the list route. You can pass in a number of query parameters that applies to your Mysql resource. The results are paginated. You can send limit and cursor along with the rest of the query parameters. If limit and cursor are not set, then a default of limit = 500 and cursor = 1 is set.

Returns HTTP 200 OK with JSON

{
  limit: 500,
  cursor: 1,
  records: [
    {
      id: 100,
      name: 'Foo Bar',
      email: 'foobar@example.com'
    }
  ]
}

In the above example if there are no users in in the users table, it HTTP 200 OK with JSON:

{
  limit: null,
  cursor: null,
  records: []
}
GET /{id}

Returns a response of HTTP 200 OK with the row that matches the id. Responds with HTTP 404 Not Found if a matching row cannot be found.

POST / with payload

Returns a response of HTTP 200 OK with the id of the newly created row.

DELETE /{id}

Returns a response of HTTP 204 No Content and removes the row matching the id from the table.

Options

mysqlConfig required

mysqlConfig Object:

host: {
 database: 'db host'
 user: 'db username'
 password: 'db password'
 port: 'db port'
}

tableName string required

The name of the mysql table on which crud operations are to be performed.

primaryKey string required

The primary key of the mysql table. This is constrained to be an auto-increment key.

mysqlDebug string optional

You can set this option to 'enabled' if you would like to debug and view the MySql queries.

requestTransformation function optional

If this option is set, the function is applied to all the requests sent to the Route Handlers.

import mapKeys from 'lodash/object/mapKeys';
import rearg from 'lodash/function/rearg';
import snakeCase from 'lodash/string/snakeCase';

function formatRequest(result) {
  const transformKeys = rearg(snakeCase, [1, 0]);
  return mapKeys(result, transformKeys);
}

responseTransformation function optional

If this option is set, the function is applied to all the api responses sent back by the Route Handlers.

import mapKeys from 'lodash/object/mapKeys';
import rearg from 'lodash/function/rearg';
import snakeCase from 'lodash/string/snakeCase';

function formatRequest(result) {
  const transformKeys = rearg(snakeCase, [1, 0]);
  return mapKeys(result, transformKeys);
}

show optional - This option corresponds to the GET /{id} route.

The Hapi config object can be passed in as follows:

show: {
  config: {
    validate: {
      params: {
        id: Joi.number().integer()
      }
    }
  }
}

list optional - This option corresponds to the GET / route.

The Hapi config object can be passed in as follows:

list: {
  config: {
    validate: {
      query: {
        name: Joi.any(),
        email: Joi.any(),
        limit: Joi
          .number()
          .integer()
          .min(1)
          .default(2)
          .optional(),
        cursor: Joi
          .number()
          .min(1)
          .default(1)
          .optional()
      }
    }
  }
}

create optional - This option corresponds to the POST / route with a payload.

The Hapi config object can be passed in as follows:

create: {
  config: {
    validate: {
      params: {
        name: Joi.string(),
        email: Joi.string().email()
      }
    }
  }
}

The payload is a JSON object.

destroy optional - This option corresponds to the DELETE /{id}.

The Hapi config object can be passed in as follows:

destroy: {
  config: {
    validate: {
      params: {
        id: Joi.number().integer()
      }
    }
  }
}

##Customizing Requests

Custom requests can be sent by using the route prerequiste feature of Hapi Routes. The custom request object has to be set to request.pre.customRequest. When set, the request object is replaced by the request.pre object.