Archive
Archive is an easy way to add an archive mechanism to your Meteor app. When a document is archived, it's removed from its original collection and placed in an archive collection so that it can be restored if needed. Its key features are:
- Zero config needed (though you can customize)
- Isomorphic so that it works with Optimistic UI
- Automatically overrides
removeAsync
to perform an archive (can be turned off) - Explicitly archive with
archiveAsync
collection method (optional) - Restore archived docs with
restoreAsync
collection method - Optionally exclude specific collections
- Compatible with Meteor
3.0.2+
Usage
Add the package to your app
meteor add jam:archive
Create your Archives collection
Create an Archives collection in your app just as you would any other collection.
1const Archives = new Mongo.Collection('archives');
Documents in the Archives collection will have this shape:
1{ 2 _id, // auto-generated by Meteor as with other collection _ids 3 _collection, // the name of the collection, e.g. 'todos', that the doc belonged to originally 4 archivedAt, // the timestamp when the document was removed from its original collection and inserted into the archive 5 id, // the original doc _id renamed to prevent conflict with the auto-generated one above. when restored, it will be renamed back to _id automatically by this package 6 /* 7 ...rest of original doc 8 */ 9}
Deleting permanently
By default, this package overrides the removeAsync
collection method so that it archives the document(s) rather that removing them from the database. To delete permanently, pass in the option forever: true
, e.g.:
1Collection.removeAsync(/* your filter */, { forever: true })
If you prefer, you can prevent overriding the removeAsync
by setting overrideRemove: false
. See Configuring for more details.
Explicitly archiving
If you prefer, you can explicity use archiveAsync
, e.g.:
1Collection.archiveAsync(/* your filter */)
Restoring a document
To restore an archived document, use restoreAsync
, e.g.:
1Collection.restoreAsync(/* your filter */)
Configuring (optional)
If you like the defaults, then you won't need to configure anything. But there is some flexibility in how you use this package.
Here are the global defaults:
1const config = { 2 name: 'archives', // if your Archives collection uses a different name, you'll want to change this 3 overrideRemove: true, // overrides the Collection.removeAsync method to make it an archive instead 4 exclude: ['roles', 'role-assignment'] // exclude specific collections from using archive so that when they are removed, the are permanently removed from the db. defaults to excluding the collections created by the meteor roles package 5};
To change the global defaults, use:
1// put this in a file that's imported on both the client and server 2import { Archive } from 'meteor/jam:archive'; 3 4Archive.configure({ 5 // ... change the defaults here ... // 6});