Skip to content

Commit

Permalink
Adds name support
Browse files Browse the repository at this point in the history
  • Loading branch information
lependu committed Aug 14, 2018
1 parent 7e749fb commit 088ec2c
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 18 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# fastify-mongojs

Simple wrapper around (mongojs)[https://github.com/mafintosh/mongojs] to share common connection pool across (Fastify)[https://github.com/fastify/fastify] server.

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
[![Build Status](https://travis-ci.org/lependu/fastify-mongojs.svg?branch=master)](https://travis-ci.org/lependu/fastify-mongojs)
[![Greenkeeper badge](https://badges.greenkeeper.io/lependu/fastify-mongojs.svg)](https://greenkeeper.io/)
[![Known Vulnerabilities](https://snyk.io/test/github/lependu/fastify-mongojs/badge.svg)](https://snyk.io/test/github/lependu/fastify-mongojs)
[![Coverage Status](https://coveralls.io/repos/github/lependu/fastify-mongojs/badge.svg?branch=master)](https://coveralls.io/github/lependu/fastify-mongojs?branch=master)

## Install
```
Expand All @@ -12,7 +14,38 @@ $ npm i --save fastify-mongojs

## Example
```js
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` `{String}` `optional` `default: 'mongo'`
`url` `{String}` `required`
`collections` `{Array}` `optional` `default: []`

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)[https://github.com/mafintosh/mongojs]

## Caveats
Due the recent changes in (mongodb)[https://github.com/mongodb/node-mongodb-native] if you pass mongodb connection to mongojs it will fail.
There is a (PR)[https://github.com/mafintosh/mongojs/pull/353] in place which needs to be published first, to imlement this feature.

## License
Licensed under [MIT](./LICENSE).
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@ const mongojs = require('mongojs')
const fastifyMongojs = function (instance, opts, next) {
const options = Object.assign({}, opts)

const client = opts.client || ''
delete opts.client
const name = options.name || 'mongo'
delete options.name

const collections = opts.collections || []
delete opts.collections
if (typeof instance[name] !== 'undefined') {
return next(new Error(`Decorator ${name} has registered`))
}

const db = mongojs(client, collections, opts)
if (typeof options.url === 'undefined') {
return next(new Error('url option is mandatory'))
}

db.on('error', function (err) {
const url = options.url
delete options.url

const collections = options.collections || []
delete options.collections

const db = mongojs(url, collections, options)

db.on('error', err => {
return next(err)
})

instance.decorate('mongo', db)
instance.addHook('onClose', instance => instance.mongo.close())
instance.decorate(name, db)
instance.addHook('onClose', instance => instance[name].close())
next()
}

Expand Down
96 changes: 87 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,117 @@ const plugin = require('.')

const { test } = tap

test('Decorates fastify instance with db | default options', t => {
test('Decorates fastify instance with db | without name', t => {
t.plan(4)

register(t, {}, function (err, fastify) {
register(t, { url: 'db', collections: ['a']}, function (err, fastify) {
t.error(err)
t.equal(typeof fastify.mongo, 'object')

fastify.mongo.stats((err, res) => {
t.error(err)
t.equal(res.db, 'admin')
t.equal(res.db, 'db')
})
})
})

test('Decorates fastify instance with db | custom options', t => {
test('Decorates fastify instance with db | with name', t => {
t.plan(4)

register(t, { client: 'db', collections: ['a']}, function (err, fastify) {
register(t, {
name: 'db1',
url: 'db1',
collections: ['a']
}, function (err, fastify) {
t.error(err)
t.equal(typeof fastify.mongo, 'object')
t.equal(typeof fastify.db1, 'object')

fastify.mongo.stats((err, res) => {
fastify['db1'].stats((err, res) => {
t.error(err)
t.equal(res.db, 'db')
t.equal(res.db, 'db1')
})
})
})

test('Paseses empty array if opts.collections missing', t => {
t.plan(4)

register(t, {
name: 'db1',
url: 'db1'
}, function (err, fastify) {
t.error(err)
t.equal(typeof fastify.db1, 'object')

fastify['db1'].stats((err, res) => {
t.error(err)
t.equal(res.collections, 0)
})
})
})

test('Throws if options.url not provided', t => {
t.plan(2)

register(t, {}, function (err, fastify) {
t.ok(err instanceof Error)
t.match(err.message, /url option is mandatory/)
})
})

test('Throws if options.name has registered', t => {
t.plan(2)

const fastify = Fastify()
t.teardown(() => fastify.close())

fastify
.decorate('alpha', {})
.register(plugin, {
name: 'alpha',
url: 'db'
})
.ready(err => {
t.ok(err instanceof Error)
t.match(err.message, /Decorator alpha has registered/)
})
})

test('Handles multiple connections', t => {
t.plan(7)

const fastify = Fastify()
t.teardown(() => fastify.close())

fastify
.register(plugin, {
name: 'alpha',
url: 'db1'
})
.register(plugin, {
name: 'beta',
url: 'db2'
})
.ready(err => {
t.error(err)
t.ok(fastify.alpha)
t.ok(fastify.beta)
fastify.alpha.stats((err, res) => {
t.error(err)
t.equal(res.db, 'db1')
})
fastify.beta.stats((err, res) => {
t.error(err)
t.equal(res.db, 'db2')
})
})
})

test('Handles mongojs error', t => {
t.plan(3)

register(t, {
client: 'mongodb://1.0.0.1:27017/db?connectTimeoutMS=100',
url: 'mongodb://1.0.0.1:27017/db?connectTimeoutMS=100',
collections: ['a']
}, function (err, fastify) {
t.error(err)
Expand Down

0 comments on commit 088ec2c

Please sign in to comment.