Registry / database / hapi-multi-mongo

hapi-multi-mongo

JSON →
library5.1.1jsnpmunverified

A Hapi.js plugin for managing multiple MongoDB connections, designed for Hapi >= 17. Current version 5.1.1. It decorates the server or request object with MongoDB client instances, supporting complex configurations with multiple servers, custom names, and connection options. Differs from hapi-mongodb by focusing on multiple connections and offering flexible configuration via strings, objects, or arrays.

npm install hapi-multi-mongo
INSTALL
IMPORT
SIG · HAPI-MULTI-MONGO
H
hapi-multi-mongo
databasejavascriptv5.1.1
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.

register
✓ await server.register({ plugin: require('hapi-multi-mongo'), options })
✗ server.register(require('hapi-multi-mongo'))
Must pass options as second argument. Plugin is CommonJS only.
server.mongo
✓ server.mongo.db_name.collection('test')
✗ server.plugins['hapi-multi-mongo'].mongo.db_name.collection
Access via decoration if decorate: true (default false, so request.mongo).
request.mongo
✓ request.mongo.db_name.collection('test')
✗ request.server.mongo
Decorated on request object if decorate: false (default).

Registers hapi-multi-mongo with a single MongoDB connection and decorates server.mongo. Defines a route that queries a collection.

const Hapi = require('@hapi/hapi'); const init = async () => { const server = Hapi.server({ port: 3000 }); await server.register({ plugin: require('hapi-multi-mongo'), options: { connection: 'mongodb://localhost:27017/mydb', decorate: true } }); server.route({ method: 'GET', path: '/', handler: async (request, h) => { const db = server.mongo.mydb; const result = await db.collection('items').findOne({}); return result; } }); await server.start(); console.log('Server running on %s', server.info.uri); }; process.on('unhandledRejection', (err) => { console.log(err); process.exit(1); }); init();
Debug
Known issues
breakingVersion 4.0.0 replaced db object with MongoClient; use db.collection() instead of db.collection() on client.
fix
Access database via server.mongo.dbName or request.mongo.dbName, then call .collection() on that db object.
affects: >=4.0.0
breakingHapi 17+ required; for Hapi <=16 use version 3.1.1.
fix
Install hapi-multi-mongo@3.1.1 for older Hapi versions.
affects: >=5.0.0
gotchaIf decorate is false (default), mongo is only available on request.mongo, not server.mongo.
fix
Set decorate: true in options to access via server.mongo.
affects: >=1.0.0
gotchaConnection names: if URI includes database, the db name is used as key; if no database, you must provide a name in config object.
fix
Always include a database in the URI or provide an explicit name per connection.
affects: >=1.0.0
Errors
Common errors & fixes
Error: hapi-multi-mongo requires a MongoClient instance
Older version or wrong usage; plugin expects MongoDB driver 3.x+.
fix
Update to hapi-multi-mongo@5.x and ensure mongodb driver is installed (npm install mongodb).
TypeError: Cannot read property 'collection' of undefined
Accessing server.mongo without decorate:true, or incorrect connection name.
fix
Enable decorate:true or use request.mongo. Verify connection name matches database name.
Error: Connection name not found in configuration
Missing name when URI has no database or array of URIs without names.
fix
Provide a name property for each connection object, e.g., { uri: '...', name: 'myConn' }.
Upgrade
Version history
5.1.1latest on npm
Audit
Dependencies
@hapi/hapirequiredPeer dependency required for plugin registration and decoration
Agent activity
9 hits · last 30 days
node
8
Resources
hapi-multi-mongo — npm install hapi-multi-mongo · libregistry