0.1.2 • Published 10 years ago

kmongo v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

kmongo Build Status

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 koa ctx variable

  • option: the mongo options, see this doc for more details.

  • context: if you wanna use ready event, then pass a trigger here, best value is your app.

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

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago