Registry / database / hapi-plugin-mysql

hapi-plugin-mysql

JSON →
library7.2.7jsnpmunverified

Hapi plugin that attaches a MySQL connection pool to every request via request.app.db. Provides both plugin registration and manual pool management (init/stop). Uses the `mysql` npm package under the hood. Current stable version is 7.2.7, with a dependency on @hapi/hapi >=19.0.0. Transactions are not handled by the plugin; users must manage them manually. The plugin uses the connection pool pattern, and the pool can be initialized before registration for use outside of hapi.

npm install hapi-plugin-mysql
INSTALL
IMPORT
SIG · HAPI-PLUGIN-MYSQL
H
hapi-plugin-mysql
databasejavascriptv7.2.7
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.

Plugin
await server.register({ plugin: require('hapi-plugin-mysql'), options: {...} })
import { register } from 'hapi-plugin-mysql'
Plugin is registered as a CommonJS module with require(). There is no ES module export.
init
const { init } = require('hapi-plugin-mysql'); await init(options)
const init = require('hapi-plugin-mysql').init()
init() returns a Promise and must be awaited.
getConnection
const { getConnection } = require('hapi-plugin-mysql'); const conn = await getConnection()
const conn = require('hapi-plugin-mysql').getConnection()
getConnection() returns a Promise; call with await.

Registers the MySQL plugin and uses request.app.db to query. Assumes MySQL is running locally.

const Hapi = require('@hapi/hapi'); const HapiPluginMysql = require('hapi-plugin-mysql'); const start = async () => { const server = Hapi.server({ port: 3000 }); await server.register({ plugin: HapiPluginMysql, options: { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '' } }); server.route({ method: 'GET', path: '/', handler: async (request, h) => { const result = await new Promise((resolve, reject) => { request.app.db.query('SELECT 1 + 1 AS solution', (err, rows) => { if (err) reject(err); else resolve(rows[0].solution); }); }); return { solution: result }; } }); await server.start(); console.log('Server running on %s', server.info.uri); }; start().catch(err => console.error(err));
Debug
Known issues
breakingTransactions are no longer part of this plugin as of v7.0.0.
fix
Handle transactions manually in your route handlers using connection.beginTransaction(), commit(), rollback().
affects: >=7.0.0
gotchaThe property is request.app.db, not request.app.connection (which was used in hapi <=16).
fix
Use request.app.db to access the MySQL connection.
affects: >=5.0.0
deprecatedThe mysql package is in maintenance mode and does not support prepared statements or async/await natively.
fix
Consider using mysql2 for modern features.
affects: >=0.0.0
gotchaThe pool must be stopped manually or the process may hang.
fix
Call await HapiPluginMysql.stop() on server stop or process exit.
affects: >=0.0.0
gotcharequire('hapi-plugin-mysql') returns an object, not a function.
fix
Use object destructuring to access init, getConnection, stop, etc.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module '@hapi/hapi'
Missing peer dependency
fix
npm install @hapi/hapi --save
Error: Cannot set headers after they are sent to the client
Multiple calls to response methods due to unhandled async errors in route handler
fix
Ensure request.app.db.query error is properly handled with try/catch or reject.
Error: getaddrinfo ENOTFOUND localhost
MySQL host not reachable
fix
Check that MySQL is running and host is correct.
Upgrade
Version history
7.2.7latest on npm
Audit
Dependencies
@hapi/hapirequiredpeer dependency for hapi plugin
mysqlrequiredruntime dependency for MySQL driver
Agent activity
8 hits · last 30 days
node
8
Resources
hapi-plugin-mysql — npm install hapi-plugin-mysql · libregistry