Last active
August 29, 2015 13:56
-
-
Save bodokaiser/8918737 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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