Simple wrapper around mongojs to share common connection pool across Fastify server.
$ npm i --save fastify-mongojs
const Fastify = require('fastify')
const fastifyMongojs = require('fastify-mongojs')
const fastify = Fastify()
fastify.register(fastifyMongojs, {
name: 'myMongo',
url: 'myDb',
collections: ['awesome_collection']
})
fastify.get('/', function(req, reply) => {
// You can reach the db connection with fastify.myMongo
})
fastify.listen(3000, err => {
if (err) throw err
})
name | type | required | default |
---|---|---|---|
name |
{String} |
❌ | 'mongo' |
url |
{String} |
✔️ | |
collections |
{Array} |
❌ | [] |
The only plugin specific option is name
which makes possible to share multiple connection pools across the server instance.
The url
and collections
options will be passed to mongojs separately, all other options will be passed to the mongojs instance as well.
For more information about the avaiable options please see mongojs.
Due the recent changes in mongodb if you pass mongodb connection to mongojs it will fail. There is a PR in place which needs to be published first, to imlement this feature.
Licensed under MIT.