11.0.0 • Published 1 year ago
hapi-mongodb v11.0.0
Hapi-MongoDB
This is a plugin to share a common MongoDB connection pool across the whole Hapi server.
Options can be a single object with the following keys or an array of the same kind if you need multiple connections :
- url: Optional. MongoDB connection string (eg.
mongodb://user:pass@localhost:27017
).- defaults to
mongodb://localhost:27017
- defaults to
- settings: Optional. Provide extra settings to the connection, see documentation.
- decorate: Optional. Rather have exposed objects accessible through server and request decorations. You cannot mix different types of decorations.
- If
true
,server.mongo
orrequest.mongo
- If it's a string,
server.<string>
orrequest.<string>
- If
Several objects are exposed by this plugin :
client
:MongoClient
for that connection. If an array was provided for the configuration, it will be an array ofMongoClient
s in the same orderdb
:Db
for that connection. If an array was provided for the configuration, it will be an array ofDb
s in the same orderlib
: mongodb library in case you need to use itObjectID
: mongodb ObjectID constructor in case you need to use it
Usage example :
const Hapi = require('hapi');
const Boom = require('boom');
const launchServer = async function() {
const dbOpts = {
url: 'mongodb://localhost:27017/test',
settings: {
poolSize: 10
},
decorate: true
};
const server = Hapi.server();
await server.register({
plugin: require('hapi-mongodb'),
options: dbOpts
});
server.route( {
method: 'GET',
path: '/users/{id}',
async handler(request) {
const db = request.mongo.db;
const ObjectID = request.mongo.ObjectID;
try {
const result = await db.collection('users').findOne({ _id: new ObjectID(request.params.id) });
return result;
}
catch (err) {
throw Boom.internal('Internal MongoDB error', err);
}
}
});
await server.start();
console.log(`Server started at ${server.info.uri}`);
};
launchServer().catch((err) => {
console.error(err);
process.exit(1);
});
Compatibility level
- Hapi >= 20
- Node.js >= 18
Ships with mongodb
6.x.
11.0.0
1 year ago
10.0.2
3 years ago
10.0.3
3 years ago
10.0.1
4 years ago
10.0.0
4 years ago
9.0.3
4 years ago
9.0.2
5 years ago
9.0.1
5 years ago
9.0.0
6 years ago
8.0.0
7 years ago
7.1.0
8 years ago
7.0.0
8 years ago
6.2.1
8 years ago
6.2.0
9 years ago
6.1.0
9 years ago
6.0.0
9 years ago
5.0.0
9 years ago
4.1.0
10 years ago
4.0.1
10 years ago
4.0.0
10 years ago
3.2.0
10 years ago
3.1.0
10 years ago
3.0.0
10 years ago
2.3.0
11 years ago
2.2.0
11 years ago
2.1.0
11 years ago
2.0.1
11 years ago
2.0.0
11 years ago
1.3.2
12 years ago
1.3.1
12 years ago
1.3.0
12 years ago