kmo

kmongo

koa-mongo: use node-mongodb-native in koajs togather with connection pool.

Showing:

Popularity

Downloads/wk

8

GitHub Stars

0

Maintenance

Last Commit

7yrs ago

Contributors

1

Package

Dependencies

2

Size (min+gzip)

71.8KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100