fastify-mongojs
Simple wrapper around mongojs to share common connection pool across Fastify server. This project is experimental if you need something stable, take a look at fastify-mongodb
Install
$ npm i --save fastify-mongojs
Example
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
})
Reference
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.
Caveats
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.
License
Licensed under MIT.