Registry / database / hapi-mysql

hapi-mysql

JSON →
library1.0.16jsnpmunverified

A simple Hapi.js plugin that provides a MySQL database connection pool using the mysql npm package. Current stable version is 1.0.16, with low release cadence (last update 2016). It exposes a connection pool via server.plugins['hapi-mysql'].pool, allowing query execution through the mysql driver's pool API. Key differentiator: minimalistic plugin that does not abstract MySQL queries; it simply integrates mysql connection pooling into Hapi's plugin system. Alternative modern approach: use @hapi/hapi with @hapi/glue and a dedicated mysql or sequelize plugin.

npm install hapi-mysql
INSTALL
IMPORT
SIG · HAPI-MYSQL
H
hapi-mysql
databasejavascriptv1.0.16
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Default export
Require the plugin as 'hapi-mysql' and register via server.pack.require('hapi-mysql', config, callback)
Using ES import syntax (import hapiMysql from 'hapi-mysql')
This is a CommonJS plugin using require(). Hapi v17+ uses server.register() instead of server.pack.require().
pool
const pool = request.server.plugins['hapi-mysql'].pool;
const pool = server.plugins['hapi-mysql'].pool; (outside of route handler without proper context)
server is available inside plugin registration callback or route handlers. request.server.plugins is the canonical path.
require('hapi-mysql')
const hapiMysql = require('hapi-mysql');
import hapiMysql from 'hapi-mysql';
Plugin is CommonJS only, no ES module available.

Registers hapi-mysql plugin on a Hapi.js server and uses the pool to run a SELECT query in a route handler.

const Hapi = require('hapi'); const server = new Hapi.Server(); server.connection({ port: 3000, host: 'localhost' }); const dbConfig = { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASSWORD ?? '', database: process.env.DB_NAME ?? 'test' }; server.register({ register: require('hapi-mysql'), options: dbConfig }, (err) => { if (err) { console.error('Failed to load plugin:', err); throw err; } server.route({ method: 'GET', path: '/', handler: (request, reply) => { const pool = request.server.plugins['hapi-mysql'].pool; pool.getConnection((err, connection) => { if (err) { return reply(err); } connection.query('SELECT 1 AS result', (err, rows) => { connection.release(); if (err) { return reply(err); } reply(rows); }); }); } }); server.start((err) => { if (err) { throw err; } console.log('Server running at:', server.info.uri); }); });
Debug
Known issues
breakingThis plugin uses the deprecated Hapi v16 API (server.pack.require). In Hapi v17+, registration is done via server.register().
fix
Use server.register({ register: require('hapi-mysql'), options: config }, callback) for Hapi v17+.
affects: >=17.0.0
deprecatedThe mysql npm package version used is outdated. Consider using mysql2 which supports promises and prepared statements.
fix
Migrate to mysql2 and update connection handling to use promises.
affects: >=1.0.0
gotchaThe pool object is accessible via request.server.plugins['hapi-mysql'].pool. Using server.plugins directly without a request context may lead to undefined errors.
fix
Always access the pool inside a route handler or plugin callback where 'request' or 'server' is properly scoped.
affects: >=1.0.0
gotchaConnection release is mandatory. Forgetting to call connection.release() will exhaust the pool.
fix
Always release the connection in a finally block or use pool.query for single queries.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'pool' of undefined
Accessing server.plugins['hapi-mysql'].pool before plugin is registered or using wrong server reference.
fix
Ensure plugin registration is completed before accessing plugins. Access via request.server.plugins inside route handlers.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES)
Invalid MySQL credentials in plugin options.
fix
Verify DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME environment variables or configuration object.
Error: Cannot find module 'hapi-mysql'
Missing npm installation or incorrect require path.
fix
Run 'npm install hapi-mysql' and ensure the module is required correctly.
Upgrade
Version history
1.0.16latest on npm
Audit
Dependencies
mysqlrequiredPeer dependency for MySQL database driver
Agent activity
6 hits · last 30 days
node
6
Resources
hapi-mysql — npm install hapi-mysql · libregistry