Extends Meteor.Collection with before
/after
hooks for insert
/update
/remove
/find
/findOne
.
Works across both client, server or a mix. Also works when a client initiates a collection method and the server runs the hook, all while respecting the collection validators (allow/deny).
Fired before the doc is inserted.
Gives you an opportunity to modify doc as needed, or run additional functionality
this.transform()
obtains transformed version of document, if a transform was defined.
var test = new Meteor.Collection("test");
test.before.insert(function (userId, doc) {
doc.createdAt = Date.now();
});
Fired before the doc is updated.
Gives you an opportunity to change the modifier
as needed, or run additional
functionality. Important: note that we are changing modifier
, and not doc
.
Changing doc
won't have any effect as the document is a copy and is not what
ultimately gets sent down to the real update
method.
this.transform()
obtains transformed version of document, if a transform was defined.
test.before.update(function (userId, doc, fieldNames, modifier) {
modifier.$set.modifiedAt = Date.now();
});
Fired just before the doc is removed.
Gives you an opportunity to affect your system while the document is still in existence -- useful for maintaining system integrity, such as cascading deletes.
this.transform()
obtains transformed version of document, if a transform was defined.
test.before.remove(function (userId, doc) {
// ...
});
Fired after the doc was inserted.
Gives you an opportunity to run post-insert tasks, such as sending notifications of new document insertions.
this.transform()
obtains transformed version of document, if a transform was defined.
test.after.insert(function (userId, doc) {
// ...
});
Fired after the doc was updated.
Gives you an opportunity to run post-update tasks, potentially comparing the previous and new documents to take further action.
this.previous
contains the document before it was updated.this.transform()
obtains transformed version of document, if a transform was defined.
test.after.update(function (userId, doc, fieldNames, modifier) {
// ...
});
Fired after the doc was removed.
doc
contains a copy of the document before it was removed.
Gives you an opportunity to run post-removal tasks that don't necessarily depend on the document being found in the database (external service clean-up for instance).
this.transform()
obtains transformed version of document, if a transform was defined.
test.after.remove: function (userId, doc) {
// ...
});
Fired before a find query.
Gives you an opportunity to adjust selector/options on-the-fly.
test.before.find: function (userId, selector, options) {
// ...
});
Fired after a find query.
Gives you an opportunity to act on a given find query. The cursor resulting from the query is provided as the last argument for convenience.
test.after.find: function (userId, selector, options, cursor) {
// ...
});
Fired before a findOne query.
Gives you an opportunity to adjust selector/options on-the-fly.
test.before.findOne: function (userId, selector, options) {
// ...
});
Fired after a findOne query.
Gives you an opportunity to act on a given findOne query. The document resulting from the query is provided as the last argument for convenience.
test.after.findOne: function (userId, selector, options, doc) {
// ...
});
- Mathieu Bouchard (@matb33)
- Kevin Kaland (@wizonesolutions)
- Andrew Mao (@mizzao)