<commit_before># Meteor Collection Hooks

Extends Meteor.Collection with before/after hooks for insert/update/remove/find/findOne methods.

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).

The only downside to these hooks is that they currently match the function signatures of their respective methods, i.e. the hook is fired on a per method-call basis, not per-document.

This means that when an `update` method is called, a hook callback will have the same function signature (with `userId` added though, if applicable). You will need to run a query to take any action on the affected documents:

```
MyCollection.before("update", function (userId, selector) {
	var docs = MyCollection.find(selector);
	docs.forEach(function (doc) {
		// etc
	});
});
```

### Example usage:

```
var test = new Meteor.Collection("test");

test.before("insert", function (userId, doc) {
	doc.created = Date.now();
});

test.after("insert", function (userId, doc) {
	// In the case of after insert, doc will have been pre-fetched for you
});

test.after("update", function (userId, selector, modifier, options, previous) {
	// Notice the "previous" parameter, which is also available in "remove" (only for the after type).
	// It contains an array of the affected documents before update/remove was applied
	doSomething();
});
```

#### Contributors

- <NAME> (@matb33)
- <NAME> (@wizonesolutions)
- <NAME> (@mizzao)<commit_msg>added find example in readme<commit_after># Meteor Collection Hooks

Extends Meteor.Collection with before/after hooks for insert/update/remove/find/findOne methods.

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).

The only downside to these hooks is that they currently match the function signatures of their respective methods, i.e. the hook is fired on a per method-call basis, not per-document.

This means that when an `update` method is called, a hook callback will have the same function signature (with `userId` added though, if applicable). You will need to run a query to take any action on the affected documents:

```
MyCollection.before("update", function (userId, selector) {
	var docs = MyCollection.find(selector);
	docs.forEach(function (doc) {
		// etc
	});
});
```

### Example usage:

```
var test = new Meteor.Collection("test");

test.before("insert", function (userId, doc) {
	doc.created = Date.now();
});

test.after("insert", function (userId, doc) {
	// In the case of after insert, doc will have been pre-fetched for you
});

test.after("update", function (userId, selector, modifier, options, previous) {
	// Notice the "previous" parameter, which is also available in "remove" (only for the after type).
	// It contains an array of the affected documents before update/remove was applied
	doSomething();
});

test.before("find", function (userId, selector, options) {
	// Note that userId will be available even when find is invoked within a Meteor.publish
});
```

#### Contributors

- <NAME> (@matb33)
- <NAME> (@wizonesolutions)
- <NAME> (@mizzao)
{'fraction_non_alphanumeric': 0.10989010989010989, 'fraction_numerical': 0.001221001221001221, 'mean_word_length': 4.5636672325976235, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4680822', 'n_tokens_mistral': 918, 'n_tokens_neox': 895, 'n_words': 406}