Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodokaiser/8918737 to your computer and use it in GitHub Desktop.
Save bodokaiser/8918737 to your computer and use it in GitHub Desktop.
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