Mongo Collection Behaviours
Extends Mongo.Collection with custom behaviour patterns.
Uses the excellent collection-hooks package to hook into collection hooks.
Getting Started
Installation:
meteor add sewdn:collection-behaviours
.timestampable()
Adds the timestampable behaviour.
Every document inserted into the collection, will have a createdAt timestamp added to it. Every document of the collection that is being updated, will have a updatedAt timestamp added to it (or updated).
1var test = new Mongo.Collection("test"); 2 3test.timestampable();
TODO
- add options to choose the name of the createdAt and updateAt fields
- add options to enable/disable createdAt, updatedAt timestamp
.autoIncrementable()
Adds an autoIncremented value.
Every document that gets inserted into the collection, will have a field added to it, containing a unique integer that is one (or other increment) higher than the previously inserted document.
1var test = new Mongo.Collection("test"); 2 3test.autoIncrementable('fieldName', 2);
.softRemovable()
Adds the soft delete behaviour.
This behaviour is useful to keep track of removed documents. Every document that gets removed from the collection, will not really be removed, but a removed boolean and a removedAt timestamp will be added. Documents that are searched in the collection, will not be found if they have been indicated as being removed.
1var test = new Mongo.Collection("test"); 2 3test.softRemovable();
TODO
- add options to choose the name of the removed and removedAt fields
.sortable()
Adds the sortable pattern.
This pattern is useful to add a default sorting to a collection, other than insertion order. This behaviour uses the autoIncrement behaviour to add a field with an integer value. Extra methods are added to the prototype of the transformed document to change the position in the sorted list.
1var test = new Mongo.Collection("test"); 2 3test.sortable('position'); 4 5_(10).times(function(n){ 6 test.insert({name: n}); 7} 8test.findOne({name: 5}).up(); 9test.findOne({name: 7}).down(2); 10_.pluck(test.find().fetch(), "name"); //returns [1,2,3,5,4,6,8,9,7,10] 11
.trackable()
Adds the trackable pattern.
This pattern is useful to track the evolution of one or more fields. For every update of a document of the collection affecting one of the configured field, a trackRecord is kept. This trackRecord consists of an array of objects, containing the previous value and a trackedAt timestamp stating when this value was changed.
1var test = new Mongo.Collection("test"); 2 3test.trackable('field1', 'field2'); 4test.trackable(['field1', 'field2']); 5
Define your own behaviours
- Look at the source of the behaviours in the behaviours folder to be able to add your own custom behaviours.
- Simply register a new behaviour with the CollectionBehaviours.defineBehaviour method.
1CollectionBehaviours.defineBehaviour('blamable', function(getTransform, args){ 2 var self = this; 3 self.before.insert(function (userId, doc) { 4 doc.createdBy = userId; 5 }); 6 self.before.update(function (userId, doc, fieldNames, modifier, options) { 7 if(!modifier.$set) 8 modifier.$set = {}; 9 modifier.$set.lastUpdatedBy = userId; 10 }); 11} 12 13var test = new Mongo.Collection("test"); 14 15test.blamable();
Contributors
- Pieter Soudan (@sewdn)