lai:collection-extensions

v0.0.7Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Meteor Collection Extensions

This package gives you utility functions to extend your Mongo.Collection instances in (hopefully) the safest, easiest and coolest way. If you want to create a package that extends Mongo.Collection, you'll need this package. I am striving for this package to be a third-party official way of extending Mongo.Collection until, well, Meteor decides to create a core functionality to extend it.

This is currently alpha!

Installation

meteor add lai:collection-extensions

Why

Meteor gives you no easy way to extend the Mongo.Collection object, and therefore package publishers who want to extend its functionality resort to monkey-patching the Mongo.Collection constructor, and sometimes it's not done right. This package seeks to centralize one well-done monkey-patch with the ability to hook into the constructor as many times as possible. See my code.

I am hoping for all collection-extending package authors to to use this to end the package compatibility issues. In order for this to happen, I will fork major packages like matb33:collection-hooks, ongoworks:security, dburles:collection-instances, refactor the code to use this utility package, and run their test suites. If you want to help, that would be awesome.

API

Meteor.addCollectionExtension(fn)

Pass in a function that takes 1 required argument and an optional second that holds the Mongo.Collection instantiation arguments.

Meteor.addCollectionPrototype(name, fn)

Pass in the name of the prototype function as well as the function. Yes, I know you can simply just do Mongo.Collection.prototype.myPrototypeFunction = function (...) {}, which is fine. One of the things that this function does differently is to check whether you're in an older version of Meteor, in which Mongo.Collection doesn't exist but rather Meteor.Collection does.

Usage

The following code recreates this section of code of the dburles:collection-instances using Meteor.addCollectionExtension(fn) thereby eliminating the need to monkey-patch the Mongo.Collection constructor:

1var instances = [];
2
3Meteor.addCollectionExtension(function (inst, args) {
4  instances.push({
5    name: inst._name,
6    instance: inst,
7    options: args[1]
8  });
9});

The following code recreates the entire dburles:collection-helpers package using Meteor.addCollectionPrototype(name, fn):

1var Document = {};
2
3Meteor.addCollectionPrototype('helpers', function (helpers) {
4  var self = this;
5
6  if (self._transform && ! self._hasCollectionHelpers)
7    throw new Meteor.Error("Can't apply helpers to '" +
8      self._name + "' a transform function already exists!");
9
10  if (! self._hasCollectionHelpers) {
11    Document[self._name] = function(doc) { return _.extend(this, doc); };
12    self._transform = function(doc) { return new Document[self._name](doc); };
13    self._hasCollectionHelpers = true;
14  }
15  
16  _.each(helpers, function(helper, key) {
17    Document[self._name].prototype[key] = helper;
18  });
19});

Todo

Integrate this package into the following packages and test them:

Create tests.

Contributing

If you are interested in using this package in your package, or if you want me to test (or if YOU want to test it yourself) integrating this into your package let me know and I will add it to the Todo list.

Future

Add control over the execution order of each collection extension somehow.

License

MIT