koa-mongoose

2.0.0 • Public • Published

koa-mongoose

NPM version build status David deps node version Gittip

mongoose middleware for koa

Install

NPM

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

Licences

MIT

Readme

Keywords

Package Sidebar

Install

npm i koa-mongoose

Weekly Downloads

17

Version

2.0.0

License

MIT

Last publish

Collaborators

  • jackong