Giter Site home page Giter Site logo

meteor-collection-behaviours's Introduction

Meteor Collection Behaviours

Extends Meteor.Collection with custom behaviour patterns.

Uses the excellent collection-hooks package to hook into collection hooks.


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

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

test.timestampable();

TODO

  • add options to choose the name of the createdAd 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.

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

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

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

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

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

test.sortable('position');

_(10).times(function(n){
  test.insert({name: n});
}
test.findOne({name: 5}).up();
test.findOne({name: 7}).down(2);
_.pluck(test.find().fetch(), "name"); //returns [1,2,3,5,4,6,8,9,7,10]

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

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

test.trackable('field1', 'field2');
test.trackable(['field1', 'field2']);

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.
CollectionBehaviours.defineBehaviour('blamable', function(getTransform, args){
  var self = this;
  self.before.insert(function (userId, doc) {
    doc.createdBy = userId;
  });
  self.before.update(function (userId, doc, fieldNames, modifier, options) {
    if(!modifier.$set)
      modifier.$set = {};
    modifier.$set.lastUpdatedBy = userId;
  });
}

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

test.blamable();

Contributors

  • Pieter Soudan (@sewdn)

meteor-collection-behaviours's People

Contributors

rhythmus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.