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

@nailahq/mongoose-delete

v1.0.1

Published

mongoose-soft-delete is simple and lightweight plugin that enables some basic required functionality for mongoose.

Downloads

2

Readme

Mongoose Soft Delete

mongoose-soft-delete is simple and lightweight plugin that enables some basic required functionality for mongoose.

Features

  • Soft Delete document using destroy method.

| Methods and Fields | Description | | ------------------------------------------- | ----------------------------------------------------------------- | | destroy() | method on document (do not override standard remove() method) | deleted | (true-false) key on document | | deletedAt | Add key to store time of deletion |

  • Restore deleted documents using restore method

| Feature | Description | | -------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | | Bulk destroy and restore | Bulk Destroy | | Option to override static methods | count, countDocuments, find, findOne, findOneAndUpdate, update | | Disable model validation on destroy | Disable Validation | | Option to create index on destroy fields | deleted, deletedAt, deletedBy |

Installation

Install using npm

npm install mongoose-soft-delete

Usage

Setup

const mongooseSoftDelete = require('mongoose-soft-delete');

const TestSchema = new Schema({
  name: String
});

// Apply on specific model.
// Can apply globally : eg: mongoose.plugin(mongooseSoftDelete, {})
TestSchema.plugin(mongooseSoftDelete, {
  paranoid: true,
});

const Test = mongoose.model('Test', TestSchema);

Options

paranoid : it needs to be true for soft deletion.

Simple usage

const test = new Test({ name: 'Test' });
test.save(function() {
  test.destroy(function() {
    // deleted: true
    test.restore(function() {});
    //deleted: false
  });
});

var exampleTestId = mongoose.Types.ObjectId('53da93b16b4a6670076b16bf');

Get sot soft deleted data.

// pass *{ paranoid: false }* as option.
// This will return response including deleted documents.
test.find({ name: 'Arpit' }, null, { paranoid: false }, (err, user) => {});

Bulk destroy and restore


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

// destroy multiple object, callback
Test.destroy(function (err, result) { ... });
Test.destroy({age:10}, function (err, result) { ... });

// destroy multiple object, promise
Test.destroy().exec(function (err, result) { ... });
Test.destroy({age:10}).exec(function (err, result) { ... });

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

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

Create index on fields

TestSchema.plugin(mongooseSoftDelete, { indexFields: true });

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

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.

NOTE : Method will be overridden if paranoid is true

| only not deleted documents | | -------------------------- | | count() | | find() | | findOne() | | findOneAndUpdate() | | update() |