{"id":42838,"library":"hapi-plugin-mysql","title":"hapi-plugin-mysql","description":"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.","status":"active","version":"7.2.7","language":"javascript","source_language":"en","source_url":"https://github.com/Salesflare/hapi-plugin-mysql","tags":["javascript","hapi","mysql","plugin"],"install":[{"cmd":"npm install hapi-plugin-mysql","lang":"bash","label":"npm"},{"cmd":"yarn add hapi-plugin-mysql","lang":"bash","label":"yarn"},{"cmd":"pnpm add hapi-plugin-mysql","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency for hapi plugin","package":"@hapi/hapi","optional":false},{"reason":"runtime dependency for MySQL driver","package":"mysql","optional":false}],"imports":[{"note":"Plugin is registered as a CommonJS module with require(). There is no ES module export.","wrong":"import { register } from 'hapi-plugin-mysql'","symbol":"Plugin","correct":"await server.register({ plugin: require('hapi-plugin-mysql'), options: {...} })"},{"note":"init() returns a Promise and must be awaited.","wrong":"const init = require('hapi-plugin-mysql').init()","symbol":"init","correct":"const { init } = require('hapi-plugin-mysql'); await init(options)"},{"note":"getConnection() returns a Promise; call with await.","wrong":"const conn = require('hapi-plugin-mysql').getConnection()","symbol":"getConnection","correct":"const { getConnection } = require('hapi-plugin-mysql'); const conn = await getConnection()"}],"quickstart":{"code":"const Hapi = require('@hapi/hapi');\nconst HapiPluginMysql = require('hapi-plugin-mysql');\n\nconst start = async () => {\n  const server = Hapi.server({ port: 3000 });\n\n  await server.register({\n    plugin: HapiPluginMysql,\n    options: {\n      host: process.env.DB_HOST ?? 'localhost',\n      user: process.env.DB_USER ?? 'root',\n      password: process.env.DB_PASS ?? ''\n    }\n  });\n\n  server.route({\n    method: 'GET',\n    path: '/',\n    handler: async (request, h) => {\n      const result = await new Promise((resolve, reject) => {\n        request.app.db.query('SELECT 1 + 1 AS solution', (err, rows) => {\n          if (err) reject(err);\n          else resolve(rows[0].solution);\n        });\n      });\n      return { solution: result };\n    }\n  });\n\n  await server.start();\n  console.log('Server running on %s', server.info.uri);\n};\n\nstart().catch(err => console.error(err));","lang":"javascript","description":"Registers the MySQL plugin and uses request.app.db to query. Assumes MySQL is running locally."},"warnings":[{"fix":"Handle transactions manually in your route handlers using connection.beginTransaction(), commit(), rollback().","message":"Transactions are no longer part of this plugin as of v7.0.0.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Use request.app.db to access the MySQL connection.","message":"The property is request.app.db, not request.app.connection (which was used in hapi <=16).","severity":"gotcha","affected_versions":">=5.0.0"},{"fix":"Consider using mysql2 for modern features.","message":"The mysql package is in maintenance mode and does not support prepared statements or async/await natively.","severity":"deprecated","affected_versions":">=0.0.0"},{"fix":"Call await HapiPluginMysql.stop() on server stop or process exit.","message":"The pool must be stopped manually or the process may hang.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use object destructuring to access init, getConnection, stop, etc.","message":"require('hapi-plugin-mysql') returns an object, not a function.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'19.0.0':45 '7.2.7':39 'attach':8 'connect':11,62 'current':35 'depend':42 'everi':14 'handl':49 'hapi':2,5,77,79 'hapi-plugin-mysql':1 'hapi/hapi':44 'hood':34 'init/stop':26 'initi':70 'javascript':78 'manag':25,55 'manual':23,57 'must':54 'mysql':4,10,29,80 'npm':30 'outsid':75 'packag':31 'pattern':64 'plugin':3,6,20,52,59,81 'pool':12,24,63,67 'provid':18 'registr':21,72 'request':15 'request.app.db':17 'stabl':36 'transact':46 'use':27,60,74 'user':53 'version':37 'via':16","created_at":"2026-06-05T16:56:55.145894+00:00","updated_at":"2026-06-05T16:56:55.145894+00:00","problems":[{"fix":"npm install @hapi/hapi --save","cause":"Missing peer dependency","error":"Error: Cannot find module '@hapi/hapi'"},{"fix":"Ensure request.app.db.query error is properly handled with try/catch or reject.","cause":"Multiple calls to response methods due to unhandled async errors in route handler","error":"Error: Cannot set headers after they are sent to the client"},{"fix":"Check that MySQL is running and host is correct.","cause":"MySQL host not reachable","error":"Error: getaddrinfo ENOTFOUND localhost"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/Salesflare/hapi-plugin-mysql","github":"https://github.com/Salesflare/hapi-plugin-mysql","docs":null,"changelog":null,"pypi":null,"npm":"hapi-plugin-mysql","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}