{"id":42835,"library":"hapi-mongodb","title":"hapi-mongodb","description":"A Hapi.js plugin that shares a MongoDB connection pool across the entire server. Current stable version 11.0.0, requires Node.js >= 18 and @hapi/hapi >= 20.0.0. Ships with mongodb driver 6.x. Provides server/request decorations for easy access to MongoClient, Db, ObjectID, and the mongodb library. Supports multiple connections via an array of options. Simplifies MongoDB integration in Hapi applications by managing connections and decorations automatically.","status":"active","version":"11.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/Marsup/hapi-mongodb","tags":["javascript","hapi","mongodb"],"install":[{"cmd":"npm install hapi-mongodb","lang":"bash","label":"npm"},{"cmd":"yarn add hapi-mongodb","lang":"bash","label":"yarn"},{"cmd":"pnpm add hapi-mongodb","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; plugin requires Hapi >= 20","package":"@hapi/hapi","optional":false}],"imports":[{"note":"Plugin is registered as a Hapi plugin; default export is the plugin object.","wrong":"import hapiMongodb from 'hapi-mongodb'","symbol":"plugin (default)","correct":"await server.register({ plugin: require('hapi-mongodb'), options: { url: '...', decorate: true } })"},{"note":"When decorate: true, mongo is available on server and request. Use decorate as a string for custom name.","wrong":"server.plugins['hapi-mongodb'].db","symbol":"server.mongo","correct":"server.mongo.db"},{"note":"request.mongo provides db, client, ObjectID, lib properties.","wrong":"request.mongo.db.ObjectID","symbol":"request.mongo","correct":"request.mongo.ObjectID"}],"quickstart":{"code":"const Hapi = require('@hapi/hapi');\n\nconst start = async () => {\n  const server = Hapi.server({ port: 3000 });\n\n  await server.register({\n    plugin: require('hapi-mongodb'),\n    options: {\n      url: process.env.MONGO_URL ?? 'mongodb://localhost:27017/test',\n      decorate: true\n    }\n  });\n\n  server.route({\n    method: 'GET',\n    path: '/items',\n    handler: async (request, h) => {\n      const db = request.mongo.db;\n      const items = await db.collection('items').find().toArray();\n      return items;\n    }\n  });\n\n  await server.start();\n  console.log('Server running on %s', server.info.uri);\n};\n\nstart().catch(err => {\n  console.error(err);\n  process.exit(1);\n});","lang":"javascript","description":"Demonstrates registering the hapi-mongodb plugin, using decorate, and accessing the database via request.mongo.db."},"warnings":[{"fix":"Upgrade to Node.js >=18, @hapi/hapi >=20. Update mongodb usage to match driver 6.x (e.g., MongoClient.connect signature changes).","message":"Version 11 drops support for Node.js < 18 and Hapi < 20. mongodb driver upgraded to 6.x which has breaking API changes.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Always provide an explicit url with credentials and proper host.","message":"The 'url' option defaults to 'mongodb://localhost:27017' which is insecure in production.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"If using multiple connections, set decorate to a unique string per connection.","message":"When using multiple connections (array of options), the decorate option must be a string to avoid overwriting. Using decorate: true for multiple connections will cause the last connection to overwrite previous.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Correct pattern: await server.register({ plugin: require('hapi-mongodb'), options: {...} });","message":"The plugin does not support async/await for the register function directly; use await server.register().","severity":"gotcha","affected_versions":">=10.0.0"}],"env_vars":null,"search_vec":"'11.0.0':20 '18':23 '20.0.0':26 '6':31 'access':38 'across':13 'applic':60 'array':52 'automat':66 'connect':11,49,63 'current':17 'db':41 'decor':35,65 'driver':30 'easi':37 'entir':15 'hapi':2,59,68 'hapi-mongodb':1 'hapi.js':5 'hapi/hapi':25 'integr':57 'javascript':67 'librari':46 'manag':62 'mongocli':40 'mongodb':3,10,29,45,56,69 'multipl':48 'node.js':22 'objectid':42 'option':54 'plugin':6 'pool':12 'provid':33 'requir':21 'server':16 'server/request':34 'share':8 'ship':27 'simplifi':55 'stabl':18 'support':47 'version':19 'via':50 'x':32","created_at":"2026-06-05T16:56:53.716078+00:00","updated_at":"2026-06-05T16:56:53.716078+00:00","problems":[{"fix":"Ensure options.decorate is set to true or a string, and access the correct property: request.mongo.db.","cause":"Decorations not enabled or wrong decorate name.","error":"TypeError: Cannot read properties of undefined (reading 'db')"},{"fix":"Run: npm install @hapi/hapi","cause":"@hapi/hapi is not installed or not in dependencies.","error":"Error: Missing required plugin: @hapi/hapi"},{"fix":"Use valid mongodb:// URL, e.g., mongodb://user:pass@localhost:27017/dbname","cause":"Incorrect MongoDB URL format.","error":"MongoParseError: Invalid connection string"},{"fix":"Use async/await: const server = Hapi.server(); await server.register(...);","cause":"Using old Hapi v16 style or not awaiting initialization.","error":"TypeError: server.register is not a function"}],"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/Marsup/hapi-mongodb#readme","github":"git://github.com/Marsup/hapi-mongodb","docs":null,"changelog":null,"pypi":null,"npm":"hapi-mongodb","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}}