
Security News
High Salaries No Longer Enough to Attract Top Cybersecurity Talent
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
mongoose-plugins-trashable
Advanced tools
A mongoose plugin that adds ablity to trash/untrash (soft deletes) model instances.
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 the module with:
npm install mongoose-plugins-trashable
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;
});
Copyright (c) 2014 Justin Lau justin@tclau.com
Licensed under the MIT license.
FAQs
A mongoose plugin that adds ablity to trash/untrash (soft deletes) model instances.
The npm package mongoose-plugins-trashable receives a total of 0 weekly downloads. As such, mongoose-plugins-trashable popularity was classified as not popular.
We found that mongoose-plugins-trashable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.