Registry / database / hapi-mongodb

hapi-mongodb

JSON →
library11.0.0jsnpmunverified

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.

npm install hapi-mongodb
INSTALL
IMPORT
SIG · HAPI-MONGODB
H
hapi-mongodb
databasejavascriptv11.0.0
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 (default)
✓ await server.register({ plugin: require('hapi-mongodb'), options: { url: '...', decorate: true } })
✗ import hapiMongodb from 'hapi-mongodb'
Plugin is registered as a Hapi plugin; default export is the plugin object.
server.mongo
✓ server.mongo.db
✗ server.plugins['hapi-mongodb'].db
When decorate: true, mongo is available on server and request. Use decorate as a string for custom name.
request.mongo
✓ request.mongo.ObjectID
✗ request.mongo.db.ObjectID
request.mongo provides db, client, ObjectID, lib properties.

Demonstrates registering the hapi-mongodb plugin, using decorate, and accessing the database via request.mongo.db.

const Hapi = require('@hapi/hapi'); const start = async () => { const server = Hapi.server({ port: 3000 }); await server.register({ plugin: require('hapi-mongodb'), options: { url: process.env.MONGO_URL ?? 'mongodb://localhost:27017/test', decorate: true } }); server.route({ method: 'GET', path: '/items', handler: async (request, h) => { const db = request.mongo.db; const items = await db.collection('items').find().toArray(); return items; } }); await server.start(); console.log('Server running on %s', server.info.uri); }; start().catch(err => { console.error(err); process.exit(1); });
Debug
Known issues
breakingVersion 11 drops support for Node.js < 18 and Hapi < 20. mongodb driver upgraded to 6.x which has breaking API changes.
fix
Upgrade to Node.js >=18, @hapi/hapi >=20. Update mongodb usage to match driver 6.x (e.g., MongoClient.connect signature changes).
affects: >=11.0.0
deprecatedThe 'url' option defaults to 'mongodb://localhost:27017' which is insecure in production.
fix
Always provide an explicit url with credentials and proper host.
affects: >=1.0.0
gotchaWhen 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.
fix
If using multiple connections, set decorate to a unique string per connection.
affects: >=1.0.0
gotchaThe plugin does not support async/await for the register function directly; use await server.register().
fix
Correct pattern: await server.register({ plugin: require('hapi-mongodb'), options: {...} });
affects: >=10.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'db')
Decorations not enabled or wrong decorate name.
fix
Ensure options.decorate is set to true or a string, and access the correct property: request.mongo.db.
Error: Missing required plugin: @hapi/hapi
@hapi/hapi is not installed or not in dependencies.
fix
Run: npm install @hapi/hapi
MongoParseError: Invalid connection string
Incorrect MongoDB URL format.
fix
Use valid mongodb:// URL, e.g., mongodb://user:pass@localhost:27017/dbname
TypeError: server.register is not a function
Using old Hapi v16 style or not awaiting initialization.
fix
Use async/await: const server = Hapi.server(); await server.register(...);
Upgrade
Version history
11.0.0latest on npm
Audit
Dependencies
@hapi/hapirequiredPeer dependency; plugin requires Hapi >= 20
Agent activity
15 hits · last 30 days
node
14
Resources
hapi-mongodb — npm install hapi-mongodb · libregistry