Skip to content

tjmehta/mongooseware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongooseware

Build Status Code Climate Test Coverage Dependency Status devDependency Status

NPM

Magic mongoose middleware for express
Works great with tjmehta/dat-middleware

Installation

npm install mongooseware

Examples

  • Model class methods
var mw = require('dat-middleware');
var BlogModel = require('mongoose').model('blogs', BlogSchema);
var blogs = require('mongooseware')(BlogModel);
var app = require('express')();

app.post('blogs',
  mw.body('name').require().string().pick(),
  blogs.create('body'),
  mw.res.send('blog'));

app.get('blogs',
  mw.query('name').require().string().pick()
  blogs.find('query'),
  mw.res.send('blogs'));

app.get('blogs/:blogId',
  blogs.findOne({ _id: 'params.blogId' }),
  mw.req('blog').require()
    .else(
      mw.res.next(mw.Boom.notFound('Blog not found'))
    ),
  mw.res.send('blog'));
  • Model instance methods
var mw = require('dat-middleware');
var BlogModel = require('mongoose').model('blogs', BlogSchema);
var blogs = require('mongooseware')(BlogModel);
var app = require('express')();

app.patch('blogs/:blogId',
  blogs.findOne({ _id: 'params.blogId' }),
  mw.req('blog').require()
    .else(
      mw.res.next(mw.Boom.notFound('Blog not found'))
    ),
  mw.body('name').require().string().pick(),
  blogs.model.update({
    $set: 'body'
  }),
  mw.res.send('blog'));

method-chain.exec([reqKey])

Specify the key to which an async method's results should be saved to on req

method-chain.sync([reqKey])

Specify the key to which an sync method's return value should be saved to on req

License

MIT

About

Magic mongoose middleware for express

Resources

License

Stars

Watchers

Forks

Packages

No packages published