Fastify MongoDB connection plugin, with this you can share the same MongoDb connection pool in every part of your server.
Under the hood the official mongodb driver is used,
the options that you pass to
register will be passed to the Mongo client.
mongodb driver version is 3.
If you don't provide the client by yourself (see below), the url option is required.
npm i fastify-mongodb --save
Add it to your project with
register and you are done!
const fastify =fastifyfastifyfastify
You may also supply a pre-configured instance of
const mongodb =mongodbMongoClient
- the passed
clientconnection will not be closed when the Fastify server shuts down.
- in order to terminate the mongodb connection you have to manually call the fastify.close method (for example for testing purposes, otherwise the test will hang).
This plugin decorates the
fastify instance with a
mongo object. That object has the
db property is added only if:
databasestring option is given during the plugin registration.
- the connection string contains the database name. See the Connection String URI Format
name option can be used in order to connect to multiple mongodb clusters.
const fastify =fastifyfastify
This project is kindly sponsored by:
Licensed under MIT.