npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-delete-plugin

v1.0.4

Published

Mongoose soft delete plugin

Downloads

67

Readme

Mongoose Delete Plugin

mongoose-delete is simple and lightweight plugin that enables soft deletion of documents in MongoDB. This code is based on mongoose-delete which in turn was based on riyadhalnur's plugin mongoose-softdelete.

Features

Installation

Install using npm

npm install mongoose-delete

Usage

We can use this plugin with or without options.

Simple usage

var mongoose_delete = require('mongoose-delete-plugin');

var PetSchema = new Schema({
    name: String
});

PetSchema.plugin(mongoose_delete);

var Pet = mongoose.model('Pet', PetSchema);

var fluffy = new Pet({ name: 'Fluffy' });

fluffy.save(function () {
    // mongodb: { deleted: false, name: 'Fluffy' }

    // note: you should invoke exactly delete() method instead of standard fluffy.remove()
    fluffy.delete(function () {
        // mongodb: { deleted: true, name: 'Fluffy' }

        fluffy.restore(function () {
            // mongodb: { deleted: false, name: 'Fluffy' }
        });
    });

});

var examplePetId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

// INFO: Example usage of deleteById static method
Pet.softDeleteById(examplePetId, function (err, petDocument) {
    // mongodb: { deleted: true, name: 'Fluffy', _id: '53da93b1...' }
});

Save time of deletion

var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
    name: String
});

PetSchema.plugin(mongoose_delete, { deletedAt : true });

var Pet = mongoose.model('Pet', PetSchema);

var fluffy = new Pet({ name: 'Fluffy' });

fluffy.save(function () {
    // mongodb: { deleted: false, name: 'Fluffy' }

    // note: you should invoke exactly delete() method instead of standard fluffy.remove()
    fluffy.softDelete(function () {
        // mongodb: { deleted: true, name: 'Fluffy', deletedAt: ISODate("2014-08-01T10:34:53.171Z")}

        fluffy.restore(function () {
            // mongodb: { deleted: false, name: 'Fluffy' }
        });
    });

});

Who has deleted the data?

var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
    name: String
});

PetSchema.plugin(mongoose_delete, { deletedBy : true });

var Pet = mongoose.model('Pet', PetSchema);

var fluffy = new Pet({ name: 'Fluffy' });

fluffy.save(function () {
    // mongodb: { deleted: false, name: 'Fluffy' }

    var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

    // note: you should invoke exactly delete() method instead of standard fluffy.remove()
    fluffy.softDelete(idUser, function () {
        // mongodb: { deleted: true, name: 'Fluffy', deletedBy: ObjectId("53da93b16b4a6670076b16bf")}

        fluffy.restore(function () {
            // mongodb: { deleted: false, name: 'Fluffy' }
        });
    });

});

Bulk delete and restore

var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
    name: String,
    age: Number
});

PetSchema.plugin(mongoose_delete);

var Pet = mongoose.model('Pet', PetSchema);

var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

// Delete multiple object, callback
Pet.softDelete(function (err, result) { ... });
Pet.softDelete({age:10}, function (err, result) { ... });
Pet.softDelete({}, idUser, function (err, result) { ... });
Pet.softDelete({age:10}, idUser, function (err, result) { ... });

// Delete multiple object, promise
Pet.softDelete().exec(function (err, result) { ... });
Pet.softDelete({age:10}).exec(function (err, result) { ... });
Pet.softDelete({}, idUser).exec(function (err, result) { ... });
Pet.softDelete({age:10}, idUser).exec(function (err, result) { ... });

// Restore multiple object, callback
Pet.restore(function (err, result) { ... });
Pet.restore({age:10}, function (err, result) { ... });

// Restore multiple object, promise
Pet.restore().exec(function (err, result) { ... });
Pet.restore({age:10}).exec(function (err, result) { ... });

Method overridden

We have the option to override all standard methods or only specific methods. Overridden methods will exclude deleted documents from results, documents that have deleted = true. Every overridden method will have two additional methods, so we will be able to work with deleted documents.

| only not deleted documents | only deleted documents | all documents | |----------------------------|-------------------------|-----------------------------| | count() | countDeleted | countWithDeleted | | countDocuments() | countDocumentsDeleted | countDocumentsWithDeleted | | find() | findDeleted | findWithDeleted | | findOne() | findOneDeleted | findOneWithDeleted | | findOneAndUpdate() | findOneAndUpdateDeleted | findOneAndUpdateWithDeleted | | update() | updateDeleted | updateWithDeleted | | updateOne() | updateOneDeleted | updateOneWithDeleted | | updateMany() | updateManyDeleted | updateManyWithDeleted | | aggregate() | aggregateDeleted | aggregateWithDeleted |

Examples how to override one or multiple methods

var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
    name: String
});

// Override all methods
PetSchema.plugin(mongoose_delete, { overrideMethods: 'all' });
// or 
PetSchema.plugin(mongoose_delete, { overrideMethods: true });

// Overide only specific methods
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find', 'findOne', 'findOneAndUpdate', 'update'] });
// or
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'countDocuments', 'find'] });
// or (unrecognized method names will be ignored)
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find', 'errorXyz'] });


var Pet = mongoose.model('Pet', PetSchema);

// Example of usage overridden methods

Pet.find(function (err, documents) {
  // will return only NOT DELETED documents
});

Pet.findDeleted(function (err, documents) {
  // will return only DELETED documents
});

Pet.findWithDeleted(function (err, documents) {
  // will return ALL documents
});

Disable model validation on delete

var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
    name: { type: String, required: true }
});

// By default, validateBeforeDelete is set to true
PetSchema.plugin(mongoose_delete);
// the previous line is identical to next line
PetSchema.plugin(mongoose_delete, { validateBeforeDelete: true });

// To disable model validation on delete, set validateBeforeDelete option to false
PetSchema.plugin(mongoose_delete, { validateBeforeDelete: false });

// NOTE: This is based on existing Mongoose validateBeforeSave option
// http://mongoosejs.com/docs/guide.html#validateBeforeSave

Create index on fields

var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
    name: String
});

// Index all field related to plugin (deleted, deletedAt, deletedBy)
PetSchema.plugin(mongoose_delete, { indexFields: 'all' });
// or 
PetSchema.plugin(mongoose_delete, { indexFields: true });

// Index only specific fields
PetSchema.plugin(mongoose_delete, { indexFields: ['deleted', 'deletedBy'] });
// or
PetSchema.plugin(mongoose_delete, { indexFields: ['deletedAt'] });

License

The MIT License

Copyright (c) 2014 Sanel Deljkic http://dsanel.github.io/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.