kmongo
v0.1.2
Published
koa-mongo: use node-mongodb-native in koajs togather with connection pool.
Downloads
10
Readme
kmongo
koa-mongo: use node-mongodb-native in koajs togather with connection pool.
Installation
$ npm install kmongo --save
API
kmongo([name, option, context])
name
: the field name in your koactx
variableoption
: the mongo options, see this doc for more details.context
: if you wanna useready
event, then pass a trigger here, best value is yourapp
.
Usage
register at startup of your app:
var kmongo = require('kmongo');
var app = koa();
var opt = {
host: 'localhost',
port: 27017,
server: {
poolSize: 10,
auto_reconnect: true
}
};
app.use(kmongo('mongo', opt, app));
app.once('ready', function() {
// connection created
app.listen(80);
});
use it in your router generator function
this.getUser = function * () {
var db = this.mongo.db('users');
// no need to close this collection because of mongodb usage
}
If you don't like the ready
trigger, then you just handle an error in the common event error
like following:
app.use(kmongo('mongo', opt));
app.on('error', function(err) {
console.log(err);
// here we could handle mongodb error
});
app.listen(80);
License
MIT