mongoose-plugins-trashable
v1.1.2
Published
A mongoose plugin that adds ablity to trash/untrash (soft deletes) model instances.
Downloads
3
Maintainers
Readme
mongoose-plugins-trashable
This mongoose plugin adds the ability to soft-delete model instances, optionally records the id of a model (usually a User) responsible for the action.
Also, the Model.count()
, Model.find()
, Model.findOne()
,
Model.findById()
and other static methods is augmented to show only
non-trashed instances by default. You can still retrieve trashed instances by
using new static methods Model.findTrashed()
, Model.findWithTrashed
etc.
Refer to the test cases for their usage.
Use with Caution
This plugin internally overriden the model's count()
, find()
and many
static methods to retrieve only trashed instances. It also adds a by()
to
mongoose.Query
to enable a sweet syntax to record Users responsible for the
delete action.
If you don't feel right about modifying/extending native APIs, please don't use this plugin.
This plugin is inspired by yi's plugin.
Install
Install the module with:
npm install mongoose-plugins-trashable
Usage
var Schema = require('mongoose').Schema;
var trashable = require('mongoose-plugins-trashable');
var should = require('should');
var TrashableSchema = new Schema({});
TrashableSchema.plugin(trachsable);
Trashable = mongoose.model('Trashable', TrashableSchema);
var trashable = new TrashableModel(); // assume async
// the attributes are created with null values
trashable.should.have.property('trashedAt').that.is.null;
trashable.should.have.property('trashedBy').that.is.null;
//
// promise style
//
// trash an object
trashable.trash.exec().then(function(trashable) {
trashable.should.have.property('trashedAt').that.is.a('Date');
trashable.should.have.property('trashedBy').that.is.null;
}).done();
// trash an object, and mark the User responsible
var User = mongoose.model('User');
var user = new User({}); // assume async
trashable.trash().by(user).exec().then(function(trashable) {
trashable.should.have.property('trashedAt').that.is.a('Date');
trashable.should.have.property('trashedBy');
trashable.trashedBy.toString().should.equals(user.id);
}).done();
// restore a trashed object
trashable.restore().exec().then(function(trashable) {
trashable.should.have.property('trashedAt').that.is.null;
trashable.should.have.property('trashedBy').that.is.null;
}).done();
//
// callback style
//
// trash an object
trashable.trash(function(err, trashable) {
trashable.should.have.property('trashedAt').that.is.a('Date');
trashable.should.have.property('trashedBy').that.is.null;
});
// trash an object, and mark the User responsible
var User = mongoose.model('User');
var user = new User({}); // assume async
trashable.trash().by(user, function(err, trashable) {
trashable.should.have.property('trashedAt').that.is.a('Date');
trashable.should.have.property('trashedBy');
trashable.trashedBy.toString().should.equals(user.id);
});
// restore a trashed object
trashable.restore(function(err, trashable) {
trashable.should.have.property('trashedAt').that.is.null;
trashable.should.have.property('trashedBy').that.is.null;
});
License
Copyright (c) 2014 Justin Lau [email protected]
Licensed under the MIT license.