<commit_before>#Needs Update
# Meteor Collection Helpers

Collection helpers automatically sets up a transformation on your collections using Meteor's Mongo.Collection `transform` option, allowing for simple models, with an interface similar to template helpers.

## Installation

There is not currently a package with this functionality. Just clone into your packages folder and do 
```sh
add dburles:collection-helpers.
```
This package is not currently compatible with dburles:collection-helpers and is offered as an alternative. It is also quite new so stuff like the fact that it still has the same name etc. will be sorted out as time allows.

## Usage - TODO: Better examples

It's recommended to set up helpers to run on both server and client. This way your helpers can be accessed both server side and client side. 

###Pre-requisites
_Define some collections._

```javascript
Books = new Mongo.Collection('books');
Authors = new Mongo.Collection('authors');

// A book document looks like:
// {
//     name: "A book written for me",
// 	   authorId: "xsdafdawd...",
// 	   publisher: "Pubs united",
// 	   foreword: {
// 		   author: "Foreword Author"
// 		   title: "Foreword"
// 		   paragraphs: [ ... ] /*strings*/,		
// 	   },
// 	   chapters: [
//	       { title: "...", pages: [ ... ] /*strings*/ }
//	   ]
// }
```

###Basic Helpers
Attaches helpers to the root document(s) returned from a collection.
Books.helpers({
  author: function() {
    return Authors.findOne(this.authorId);
  },
  remove: function(){
	Books.remove({_id: this._id});
  }
});
// bookInstance.remove() // remove this book from the Books collection
// bookInstance.author().firstName 

###Nested Helpers
Books.helpers({
	"foreword.numberOfParagraphs": function(helperContext){
		//helperContext -> { parentDocument: { ... }, rootDocument: { ... } }
		//this -> { author: "...", title: "...", paragraphs: [...] }	
		
		return this.paragraphs ? this.paragraphs.length : 0;
	}
});
// var n = bookInstance.foreword.numberOfParagraphs() // number of paragraphs in foreword

###Array Nested Helpers
Books.helpers({
	"chapters.$.numberOfPages": function(helperContext){
		//helperContext -> { parentDocument: { ... }, rootDocument: { ... } }
		//this -> { title: "...", pages: [ ... ] }	
		
		return this.pages ? this.pages.length : 0;
	}
});
// bookInstance.chapters[2].numberOfPages(); // number of pages in chapter three
// bookInstance.chapters[5].numberOfPages(); // number of pages in chapter six

### Example use within a template

Our relationships are resolved by the collection helper, avoiding unnecessary template helpers. So we can simply write:

```javascript<commit_msg>Fixed Readme<commit_after>#Needs Update
# Meteor Collection Helpers

Collection helpers automatically sets up a transformation on your collections using Meteor's Mongo.Collection `transform` option, allowing for simple models, with an interface similar to template helpers.

## Installation

There is not currently a package with this functionality. Just clone into your packages folder and do 
```sh
$ meteor add dburles:collection-helpers
```
This package is not currently compatible with dburles:collection-helpers and is offered as an alternative. It is also quite new so stuff like the fact that it still has the same name etc. will be sorted out as time allows.

## Usage - TODO: Better examples

It's recommended to set up helpers to run on both server and client. This way your helpers can be accessed both server side and client side. 

###Pre-requisites
_Define some collections._

```javascript
Books = new Mongo.Collection('books');
Authors = new Mongo.Collection('authors');

// A book document looks like:
// {
//     name: "A book written for me",
// 	   authorId: "xsdafdawd...",
// 	   publisher: "Pubs united",
// 	   foreword: {
// 		   author: "Foreword Author"
// 		   title: "Foreword"
// 		   paragraphs: [ ... ] /*strings*/,		
// 	   },
// 	   chapters: [
//	       { title: "...", pages: [ ... ] /*strings*/ }
//	   ]
// }
```

###Basic Helpers
Attaches helpers to the root document(s) returned from a collection.
```javascript
Books.helpers({
  author: function() {
    return Authors.findOne(this.authorId);
  },
  remove: function(){
	Books.remove({_id: this._id});
  }
});
// bookInstance.remove() // remove this book from the Books collection
// bookInstance.author().firstName 
```

###Nested Helpers
```javascript
Books.helpers({
	"foreword.numberOfParagraphs": function(helperContext){
		//helperContext -> { parentDocument: { ... }, rootDocument: { ... } }
		//this -> { author: "...", title: "...", paragraphs: [...] }	
		
		return this.paragraphs ? this.paragraphs.length : 0;
	}
});
// var n = bookInstance.foreword.numberOfParagraphs() // number of paragraphs in foreword
```

###Array Nested Helpers
```javascript
Books.helpers({
	"chapters.$.numberOfPages": function(helperContext){
		//helperContext -> { parentDocument: { ... }, rootDocument: { ... } }
		//this -> { title: "...", pages: [ ... ] }	
		
		return this.pages ? this.pages.length : 0;
	}
});
// bookInstance.chapters[2].numberOfPages(); // number of pages in chapter three
// bookInstance.chapters[5].numberOfPages(); // number of pages in chapter six
```

### Example use within a template

Our relationships are resolved by the collection helper, avoiding unnecessary template helpers. So we can simply write:

```javascript
{'fraction_non_alphanumeric': 0.14039636969809224, 'fraction_numerical': 0.0014817558807186516, 'mean_word_length': 3.4226044226044228, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 11, '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': '4896405', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1521, 'n_words': 538}