Besides the local collection events, mongodb supports also global collection events that run for all collection on pre and post levels
var dbproxy = require('mongodb-proxy') var db = dbproxy.create({...}) db.configure(function (config) { config.bind("preread", require("./events/preread.js")) config.bind("postread", require("./events/postread.js")) config.bind("precreate", require("./events/precreate.js")) config.bind("postcreate", require("./events/postcreate.js")) config.bind("preupdate", require("./events/preupdate.js")) config.bind("postupdate", require("./events/postupdate.js")) config.bind("predelete", require("./events/predelete.js")) config.bind("postdelete", require("./events/postdelete.js")) })
./events/preread.js
PreRead events are triggered before fetching from the database and before any local "get" events.
module.exports = function (sender, collection, context) { // if the request is not coming from an internal proxy if (!context.internal) // check that the user has permissions on the current store and the current item if(!isUserAuthorised(currentUser, collection, "get")) // return Unauthorized return context.error(401, "Access denied") // trigger the async callback context.done() }
./events/postread.js
PostRead events are triggered after the results are retrieved from the database and after any local "get" events.
module.exports = function (sender, collection, context, data) { // the request is not coming from an internal proxy if (!context.internal) // in case the data is restricted if(isRestricted(collection, data)) // return Unauthorized return context.error(401, "Access denied") // trigger the async callback context.done() }
./events/precreate.js
PreCreate events are triggered before posting data to the database and before any local "post" events.
module.exports = function (sender, collection, context, data) { // if the request is not coming from an internal proxy if (!context.internal) // check that the user has permissions on the current store and the current item if(!isUserAuthorised(currentUser, collection, "post")) // return Unauthorized return context.error(401, "Access denied") // trigger the async callback context.done() }
./events/postcreate.js
PostCreate events are triggered after the results are posted to the database and after any local "post" events.
module.exports = function (sender, collection, context, data) { // record the post action logAction(sender, collection, "post", data, function (err, res) { if (err) return context.error(err) // trigger the async callback context.done() }) }
./events/preupdate.js
PreUpdate events are triggered before updating data to the database and before any local "put" events.
module.exports = function (sender, collection, context, data) { // if the request is not coming from an internal proxy if (!context.internal) // check that the user has permissions on the current store and the current item if(!isUserAuthorised(currentUser, collection, "put")) // return Unauthorized return context.error(401, "Access denied") // trigger the async callback context.done() }
./events/postupdate.js
PostUpdate events are triggered after the results are updated in the database and after any local "put" events.
module.exports = function (sender, collection, context, data) { // record the post action logAction(sender, collection, "put", data, function (err, res) { if (err) return context.error(err) // trigger the async callback context.done() }) }
./events/predelete.js
PreDelete events are triggered before removing data from the database and before any local "del" events.
module.exports = function (sender, collection, context, data) { // if the request is not coming from an internal proxy if (!context.internal) // check that the user has permissions on the current store and the current item if(!isUserAuthorised(currentUser, collection, "del")) // return Unauthorized return context.error(401, "Access denied") // trigger the async callback context.done() }
./events/postdelete.js
PostDelete events are triggered after the results are removed from the database and after any local "put" events.
module.exports = function (sender, collection, context, data) { // record the post action logAction(sender, collection, "del", data, function (err, res) { if (err) return context.error(err) // trigger the async callback context.done() }) }