mongoose-paranoid_remove
v0.0.2
Published
Provides methods to set documents as deleted rather than destroy them.
Downloads
4
Maintainers
Readme
Mongoose paranoid_remove Plugin
A simple Mongoose plugin which adds
deletedAt
date attribute to models that include our plugin.
Installation
npm install mongoose-paranoid_remove --save
Usage
Then require mongoose-paranoid_remove
plugin on your schema file.
var removedTimestamp = require('mongoose-paranoid_remove');
var UserSchema = new Schema({
name: String
});
UserSchema.plugin(removedTimestamp);
module.exports = mongoose.model('User', UserSchema);
Useful methods provided by paranoid_remove:
user.paranoid_remove();
Updates user.deletedAt, use it instead of calling the usual remove middleware.
// users DESTROY action
.delete(function(req, res) {
User.findById(req.params.user_id, function(err, user) {
if (err) res.send(err);
user.paranoid_remove();
user.save(function(err) {
if (err) res.send(err);
res.json({ message: 'User deleted!' });
});
});
});
User.undeleted(err, users);
Retrieves documents that have not been "removed".
// users INDEX action
.get(function(req, res) {
User.undeleted(function(err, users) {
if (err) res.send(err);
res.json(creatures);
});
})