Skip to content

midnightcodr/hapi-mysql2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inspired by https://github.com/Marsup/hapi-mongodb, here's another simple mysql plugin for hapijs that supports multiple connections.

Usage example:

const Hapi = require('hapi');
const Boom = require('boom');

const clientOpts = {
    settings: 'mysql://user:secret@localhost/test?insecureAuth=true',
    decorate: true
};

const server = new Hapi.Server();
server.connection({ port: 8000 });

server.register({
    register: require('./lib'),
    options: clientOpts
}, function (err) {
    if (err) {
        console.error(err);
        throw err;
    }

    server.route({
        method: 'GET',
        path: '/mysql',
        handler(request, reply) {
            const pool = request.mysql.pool;

            pool.query('select 1 as counter', (err, result) => {
                if(err) {
                    console.log(err);
                    return reply(Boom.internal('Internal Mysql Error'));
                }
                reply({result});
            })
        }
    });

    server.start(function() {
        console.log(`Server started at ${server.info.uri}`);
    });
});

Test instruction:

url=mysql://someuser:somepass@somehost/test npm test

For more usage examples, see lib/index.test.js.

About

Another simple mysql plugin for hapijs that supports multiple connections, inspired by https://github.com/Marsup/hapi-mongodb

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •