koa-mongoose
v2.0.0
Published
mongoose middleware for koa
Downloads
87
Readme
koa-mongoose
mongoose middleware for koa
Install
Features
- use mongoose-q
- use with models
- use with schemas
- use with different database
Examples
With models
var app = require('koa')()
var mongoose = require('koa-mongoose')
var User = require('./models/user')
app.use(mongoose({
mongoose: require('mongoose-q')(),//custom mongoose
user: '',
pass: '',
host: '127.0.0.1',
port: 27017,
database: 'test',
db: {
native_parser: true
},
server: {
poolSize: 5
}
}))
app.use(function* (next) {
var user = new User({
account: 'test',
password: 'test'
})
yield user.saveQ()
this.body = 'OK'
})
With schemas
var app = require('koa')()
var mongoose = require('koa-mongoose')
app.use(mongoose({
username: '',
password: '',
host: '127.0.0.1',
port: 27017,
database: 'test',
schemas: './schemas'
db: {
native_parser: true
},
server: {
poolSize: 5
}
}))
app.use(function* (next) {
var User = this.model('User')
var user = new User({
account: 'test',
password: 'test'
})
//or
var user = this.document('User', {
account: 'test',
password: 'test'
})
yield user.saveQ()
this.body = 'OK'
})
With database
var app = require('koa')()
var mongoose = require('koa-mongoose')
app.use(mongoose({
username: '',
password: '',
host: '127.0.0.1',
port: 27017,
database: ctx => {
return ctx.headers['x-app']
},
schemas: './schemas'
db: {
native_parser: true
},
server: {
poolSize: 5
}
}))
app.use(function* (next) {
var user = this.document('User', {
account: 'test',
password: 'test'
})
yield user.saveQ()
this.body = 'OK'
})
Tests
cd test && docker-compose up -d
HOST=YOUR-DOCKER-HOST npm test