raragao:collection-softremovable

v1.0.7Published 3 years ago

This package is a fork from https://github.com/zimme/meteor-collection-softremovable to support Autoform 6.3+ and I converted code to JavaScript. Now it supports last Meteor releases (tested up to 2.4).

Soft remove for collections

Add soft remove to collections.

Install

meteor add zimme:collection-softremovable

Usage

Basic usage examples.

Attach

1Posts = new Mongo.Collection('posts');
2
3//Attach behaviour with the default options
4Posts.attachBehaviour('softRemovable');
5
6//Attach behaviour with custom options
7Posts.attachBehaviour('softRemovable', {
8  removed: 'deleted',
9  removedBy: false,
10  restoredAt: 'undeletedAt',
11  restoredBy: false
12});

Remove/Restore

1// Soft remove document by _id
2Posts.softRemove({_id: 'BFpDzGuWG8extPwrE'});
3
4// Restore document by _id
5Posts.restore('BFpDzGuWG8extPwrE');
6
7// Actually remove document from collection
8Posts.remove({_id: 'BFpDzGuWG8extPwrE'});

Find

1// Find all posts except soft removed posts
2Posts.find({});
3
4// Find only posts that have been soft removed
5Posts.find({removed: true});
6
7// Find all posts including removed
8Posts.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.

1Meteor.publish('posts', function() {
2  return Posts.find({});
3});
4
5Meteor.publish('removedPosts', function() {
6  return Posts.find({removed: true});
7});
8
9Meteor.publish('allPosts', function() {
10  return Posts.find({}, {removed: true});
11});

Options

The following options can be used:

  • removed: Optional. Set to 'string' to change the fields name. This field can't be omitted.

  • removedAt: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • removedBy: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • restoredAt: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • restoredBy: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • systemId: Optional. Set to 'string' to change the id representing the system.

Global configuration

1// Configure behaviour globally
2// All collection using this behaviour will use these settings as defaults
3// The settings below are the package default settings
4CollectionBehaviours.configure('softRemovable',{
5  removed: 'removed',
6  removedAt: 'removedAt',
7  removedBy: 'removedBy',
8  restoredAt: 'restoredAt',
9  restoredBy: 'restoredAt',
10  systemId: '0'
11});

Notes

  • This package attaches a schema to the collection if aldeed:simple-schema, aldeed:collection2 and/or aldeed:autoform are used in the application.