Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Last active Aug 29, 2015
Embed
What would you like to do?
var mongoi = require('mongoi');
// connect to test database
mongoi.connect('mongodb://localhost/test');
// register generic middleware
mongoi.use(function(context, next) {
// do some logging
next();
});
// register accounts specific middleware
mongoi.accounts.use(function(context, next) {
if (context.method === 'fetch') {
// map context.document on Model prototype to model
} else {
// validate context.document
}
next();
});
// register accounts specific middleware only on destroy operations
mongoi.accounts.use('destroy', function(context, next) {
// remove related documents
next();
});
// do find query
mongo.accounts.find({ age: { gt: 18, lt: 30 } }, function(err, result) {
if (err) throw err;
process.stdout.write(JSON.stringify(result));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment