Gathering detailed insights and metrics for mongoose-plugins-trashable
Gathering detailed insights and metrics for mongoose-plugins-trashable
Gathering detailed insights and metrics for mongoose-plugins-trashable
Gathering detailed insights and metrics for mongoose-plugins-trashable
npm install mongoose-plugins-trashable
Typescript
Module System
Min. Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
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:
1npm install mongoose-plugins-trashable
1var Schema = require('mongoose').Schema; 2var trashable = require('mongoose-plugins-trashable'); 3var should = require('should'); 4 5var TrashableSchema = new Schema({}); 6TrashableSchema.plugin(trachsable); 7Trashable = mongoose.model('Trashable', TrashableSchema); 8 9var trashable = new TrashableModel(); // assume async 10 11// the attributes are created with null values 12trashable.should.have.property('trashedAt').that.is.null; 13trashable.should.have.property('trashedBy').that.is.null; 14 15 16// 17// promise style 18// 19 20// trash an object 21trashable.trash.exec().then(function(trashable) { 22 trashable.should.have.property('trashedAt').that.is.a('Date'); 23 trashable.should.have.property('trashedBy').that.is.null; 24}).done(); 25 26// trash an object, and mark the User responsible 27var User = mongoose.model('User'); 28var user = new User({}); // assume async 29trashable.trash().by(user).exec().then(function(trashable) { 30 trashable.should.have.property('trashedAt').that.is.a('Date'); 31 trashable.should.have.property('trashedBy'); 32 trashable.trashedBy.toString().should.equals(user.id); 33}).done(); 34 35// restore a trashed object 36trashable.restore().exec().then(function(trashable) { 37 trashable.should.have.property('trashedAt').that.is.null; 38 trashable.should.have.property('trashedBy').that.is.null; 39}).done(); 40 41 42// 43// callback style 44// 45 46// trash an object 47trashable.trash(function(err, trashable) { 48 trashable.should.have.property('trashedAt').that.is.a('Date'); 49 trashable.should.have.property('trashedBy').that.is.null; 50}); 51 52// trash an object, and mark the User responsible 53var User = mongoose.model('User'); 54var user = new User({}); // assume async 55trashable.trash().by(user, function(err, trashable) { 56 trashable.should.have.property('trashedAt').that.is.a('Date'); 57 trashable.should.have.property('trashedBy'); 58 trashable.trashedBy.toString().should.equals(user.id); 59}); 60 61// restore a trashed object 62trashable.restore(function(err, trashable) { 63 trashable.should.have.property('trashedAt').that.is.null; 64 trashable.should.have.property('trashedBy').that.is.null; 65});
Copyright (c) 2014 Justin Lau justin@tclau.com
Licensed under the MIT license.
No vulnerabilities found.
No security vulnerabilities found.