npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-mysql-routes-plugin

v1.1.0

Published

Hapi plugin to create routes for crud operations on a MySql resource

Downloads

9

Readme

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:

  • GET /{id}
  • GET / [query params]
  • POST / [payload]
  • DELETE /{id}

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: '[email protected]'
    }
  ]
}

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.