mongoose-paranoid_remove

Created: 2014-05-18 19:59
Updated: 2016-10-13 14:04
License: mit

README.md

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);
    });

  })
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more