koa-mon
koa-mon is a koa middleware for working with mongoose. Features including
- Handles model loading
- Handles mongoose connection including auto reconnects
- Passing mongoose and models to koa context
- Handles multiple mongoose connections
- Nice database connection status logging with local timestamps
- Load mongoose global plugins
Installation
npm install koa-mon --save
Usage
Single connection
For single connection, don't need to specify connectionName
. By default,
mongoose's default connection is used. You can export your compiled models
directly.
const mongoose = ;app;
Multiple connections
For multiple connections, you need to specify connectionName
explicitly.
In your model definitions, you need to name it like 'User.js'
and export a
schema. Koa-mon handles the relationship between model and connections for you.
const mongoose = ;app;app;
To retrive your models:
app;
To define your model:
// Post.jsconst mongoose = ;const Schema = mongoose;// You need to export the schema, do not use mongoose.model('Post', postSchema)moduleexports =title: Stringcontent: String;