Magic mongoose middleware for express
Works great with tjmehta/dat-middleware
npm install mongooseware
- 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'));
Specify the key to which an async method's results should be saved to on req
Specify the key to which an sync method's return value should be saved to on req