zimme / meteor-collection-softremovable

Add soft remove to collections
https://atmospherejs.com/zimme/collection-softremovable
MIT License
29 stars 9 forks source link
meteor

[Gitter] (https://gitter.im/zimme/meteor-collection-softremovable) [Code Climate] (https://codeclimate.com/github/zimme/meteor-collection-softremovable)

Soft remove for collections

Add soft remove to collections.

Install

meteor add zimme:collection-softremovable

Usage

Basic usage examples.

Attach

Posts = new Mongo.Collection('posts');

//Attach behaviour with the default options
Posts.attachBehaviour('softRemovable');

//Attach behaviour with custom options
Posts.attachBehaviour('softRemovable', {
  removed: 'deleted',
  removedBy: false,
  restoredAt: 'undeletedAt',
  restoredBy: false
});

Remove/Restore

// Soft remove document by _id
Posts.softRemove({_id: 'BFpDzGuWG8extPwrE'});

// Restore document by _id
Posts.restore('BFpDzGuWG8extPwrE');

// Actually remove document from collection
Posts.remove({_id: 'BFpDzGuWG8extPwrE'});

Find

// Find all posts except soft removed posts
Posts.find({});

// Find only posts that have been soft removed
Posts.find({removed: true});

// Find all posts including removed
Posts.find({}, {removed: true});

Publish

For you to be able to find soft removed documents on the client you will need to explicitly publish those. The example code below belongs in server-side code.

Meteor.publish('posts', function() {
  return Posts.find({});
});

Meteor.publish('removedPosts', function() {
  return Posts.find({removed: true});
});

Meteor.publish('allPosts', function() {
  return Posts.find({}, {removed: true});
});

Options

The following options can be used:

Global configuration

// Configure behaviour globally
// All collection using this behaviour will use these settings as defaults
// The settings below are the package default settings
CollectionBehaviours.configure('softRemovable',{
  removed: 'removed',
  removedAt: 'removedAt',
  removedBy: 'removedBy',
  restoredAt: 'restoredAt',
  restoredBy: 'restoredAt',
  systemId: '0'
});

Notes