├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hapi-mysql 2 | 3 | ## Usage 4 | 5 | ### Register plugin: 6 | 7 | ```javascript 8 | var server = Hapi.createServer('0.0.0.0', 8000); 9 | server.pack.require('hapi-mysql', Config.db, function(err) { 10 | if (err) { 11 | console.error(err); 12 | throw err; 13 | } 14 | }); 15 | ``` 16 | 17 | ### Use plugin: 18 | 19 | ```javascript 20 | request.server.plugins['hapi-mysql'].pool.getConnection(function(err, connection) { 21 | 22 | // Use the connection 23 | connection.query( 24 | 'SELECT 1 FROM mytable', 25 | function(err, rows) { 26 | 27 | if(err) { 28 | throw new Error(err) 29 | } 30 | } 31 | ) 32 | 33 | // And done with the connection. 34 | connection.release(); 35 | }) 36 | ``` 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var mysql = require('mysql'); 2 | 3 | var register = function (server, options, next) { 4 | console.log('MySQL plugin registered'); 5 | 6 | if (!options.host) { 7 | throw new Error('DB host not defined in config'); 8 | } 9 | 10 | var pool = mysql.createPool(options); 11 | 12 | server.expose('pool', pool); 13 | 14 | return next(); 15 | }; 16 | 17 | register.attributes = { 18 | pkg: require('./package.json') 19 | }; 20 | 21 | module.exports = register; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapi-mysql", 3 | "version": "1.0.13", 4 | "description": "A simple hapi plugin that provides a connection to a MySQL database", 5 | "author": "Laurie Voss ", 6 | "bugs": { 7 | "url": "https://github.com/seldo/hapi-mysql/issues" 8 | }, 9 | "dependencies": { 10 | "mysql": "^2.5.4" 11 | }, 12 | "devDependencies": {}, 13 | "homepage": "https://github.com/seldo/hapi-mysql", 14 | "keywords": [ 15 | "ecosystem:hapi", 16 | "mysql", 17 | "plugin" 18 | ], 19 | "license": "ISC", 20 | "main": "index.js", 21 | "repository": { 22 | "type": "git", 23 | "url": "https://github.com/seldo/hapi-mysql.git" 24 | }, 25 | "scripts": { 26 | "test": "echo \"Error: no test specified\" && exit 1" 27 | } 28 | } 29 | --------------------------------------------------------------------------------