npm package: @mongoose-plugins/delete
Description: Mongoose soft delete plugin
Author: Alek Smith
Latest version: 1.0.2

# Mongoose Delete Plugin

Simple and lightweight plugin that enables soft deletion of documents in MongoDB.

[![Build Status](https://github.com/dsanel/mongoose-delete/workflows/Test/badge.svg)](https://github.com/dsanel/mongoose-delete/actions/workflows/test.yml)

## Features

- [Add **delete()** method on document (do not override standard **remove()** method)](#simple-usage)
- [Add **deleteById()** static method](#simple-usage)
- [Add **deleted** (true-false) key on document](#simple-usage)
- [Add **deletedAt** key to store time of deletion](#save-time-of-deletion)
- [Add **deletedBy** key to record who deleted document](#who-has-deleted-the-data)
- Restore deleted documents using **restore** method
- [Bulk delete and restore](#bulk-delete-and-restore)
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (**count, countDocuments, find, findOne, findOneAndUpdate, update, updateOne, updateMany**)
- [For overridden methods we have two additional methods](#method-overridden): **methodDeleted** and **methodWithDeleted**
- [Disable model validation on delete](#disable-model-validation-on-delete)
- [Option to create index on delete fields](#create-index-on-fields) (**deleted**, **deletedAt**, **deletedBy**)
- Option to disable use of `$ne` operator using `{use$neOperator: false}`. Before you start to use this option please check [#50](https://github.com/dsanel/mongoose-delete/issues/50).
- Option to override **aggregate**.
- Option to **populate** with deleted documents (`{ withDeleted: true }`)

## Installation

Install using [npm](https://npmjs.org)

```
npm install @mongoose-plugins/delete
```

```
yarn add @mongoose-plugins/delete
```

## TypeScript support

The plugin currently does not have its own type definition. Please be free to use [@types/mongoose-delete](https://www.npmjs.com/package/@types/mongoose-delete).

In doing so, you should make use of the `SoftDeleteModel` type, instead of the `Model` type.

```typescript
import { Schema, model, connect } from 'mongoose';
import { SoftDeleteModel }, MongooseDelete from 'mongoose-delete';

interface User extends SoftDeleteDocument {
  name: string;
}

const UserSchema = new Schema<User>({
    name: String
});

UserSchema.plugin(MongooseDelete, { deletedBy: true, deletedByType: String });

const model: SoftDeleteModel = model<User>('User', UserSchema);

export default model;
```

## Usage

We can use this plugin with or without options.

### Simple usage

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: String,
});

UserSchema.plugin(mongoose_delete);

var User = mongoose.model("User", UserSchema);

var user = new User({ name: "Fluffy" });

user.save(function () {
  // mongodb: { deleted: false, name: 'Fluffy' }

  // note: you should invoke exactly delete() method instead of standard user.remove()
  user.delete(function () {
    // mongodb: { deleted: true, name: 'Fluffy' }

    user.restore(function () {
      // mongodb: { deleted: false, name: 'Fluffy' }
    });
  });
});

var exampleUserId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

// INFO: Example usage of deleteById static method
User.deleteById(exampleUserId, function (err, userDocument) {
  // mongodb: { deleted: true, name: 'Fluffy', _id: '53da93b1...' }
});
```

### Save time of deletion

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: String,
});

UserSchema.plugin(mongoose_delete, { deletedAt: true });

var User = mongoose.model("User", UserSchema);

var user = new User({ name: "Fluffy" });

user.save(function () {
  // mongodb: { deleted: false, name: 'Fluffy' }

  // note: you should invoke exactly delete() method instead of standard user.remove()
  user.delete(function () {
    // mongodb: { deleted: true, name: 'Fluffy', deletedAt: ISODate("2014-08-01T10:34:53.171Z")}

    user.restore(function () {
      // mongodb: { deleted: false, name: 'Fluffy' }
    });
  });
});
```

### Who has deleted the data?

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: String,
});

UserSchema.plugin(mongoose_delete, { deletedBy: true });

var User = mongoose.model("User", UserSchema);

var user = new User({ name: "Fluffy" });

user.save(function () {
  // mongodb: { deleted: false, name: 'Fluffy' }

  var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

  // note: you should invoke exactly delete() method instead of standard user.remove()
  user.delete(idUser, function () {
    // mongodb: { deleted: true, name: 'Fluffy', deletedBy: ObjectId("53da93b16b4a6670076b16bf")}

    user.restore(function () {
      // mongodb: { deleted: false, name: 'Fluffy' }
    });
  });
});
```

The type for `deletedBy` does not have to be `ObjectId`, you can set a custom type, such as `String`.

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: String,
});

UserSchema.plugin(mongoose_delete, { deletedBy: true, deletedByType: String });

var User = mongoose.model("User", UserSchema);

var user = new User({ name: "Fluffy" });

user.save(function () {
  // mongodb: { deleted: false, name: 'Fluffy' }

  var idUser = "my-custom-user-id";

  // note: you should invoke exactly delete() method instead of standard user.remove()
  user.delete(idUser, function () {
    // mongodb: { deleted: true, name: 'Fluffy', deletedBy: 'my-custom-user-id' }

    user.restore(function () {
      // mongodb: { deleted: false, name: 'Fluffy' }
    });
  });
});
```

### Bulk delete and restore

```javascript
var mongoose_delete = require('mongoose-delete');

var UserSchema = new Schema({
    name: String,
    age: Number
});

UserSchema.plugin(mongoose_delete);

var User = mongoose.model('User', UserSchema);

var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

// Delete multiple object, callback
User.delete(function (err, result) { ... });
User.delete({age:10}, function (err, result) { ... });
User.delete({}, idUser, function (err, result) { ... });
User.delete({age:10}, idUser, function (err, result) { ... });

// Delete multiple object, promise
User.delete().exec(function (err, result) { ... });
User.delete({age:10}).exec(function (err, result) { ... });
User.delete({}, idUser).exec(function (err, result) { ... });
User.delete({age:10}, idUser).exec(function (err, result) { ... });

// Restore multiple object, callback
User.restore(function (err, result) { ... });
User.restore({age:10}, function (err, result) { ... });

// Restore multiple object, promise
User.restore().exec(function (err, result) { ... });
User.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

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: String,
});

// Override all methods
UserSchema.plugin(mongoose_delete, { overrideMethods: "all" });
// or
UserSchema.plugin(mongoose_delete, { overrideMethods: true });

// Overide only specific methods
UserSchema.plugin(mongoose_delete, {
  overrideMethods: ["count", "find", "findOne", "findOneAndUpdate", "update"],
});
// or
UserSchema.plugin(mongoose_delete, {
  overrideMethods: ["count", "countDocuments", "find"],
});
// or (unrecognized method names will be ignored)
UserSchema.plugin(mongoose_delete, {
  overrideMethods: ["count", "find", "errorXyz"],
});

var User = mongoose.model("User", UserSchema);

// Example of usage overridden methods

User.find(function (err, documents) {
  // will return only NOT DELETED documents
});

User.findDeleted(function (err, documents) {
  // will return only DELETED documents
});

User.findWithDeleted(function (err, documents) {
  // will return ALL documents
});
```

### Disable model validation on delete

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: { type: String, required: true },
});

// By default, validateBeforeDelete is set to true
UserSchema.plugin(mongoose_delete);
// the previous line is identical to next line
UserSchema.plugin(mongoose_delete, { validateBeforeDelete: true });

// To disable model validation on delete, set validateBeforeDelete option to false
UserSchema.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

```javascript
var mongoose_delete = require("mongoose-delete");

var UserSchema = new Schema({
  name: String,
});

// Index all field related to plugin (deleted, deletedAt, deletedBy)
UserSchema.plugin(mongoose_delete, { indexFields: "all" });
// or
UserSchema.plugin(mongoose_delete, { indexFields: true });

// Index only specific fields
UserSchema.plugin(mongoose_delete, { indexFields: ["deleted", "deletedBy"] });
// or
UserSchema.plugin(mongoose_delete, { indexFields: ["deletedAt"] });
```

## License

The MIT License

Copyright (c) 2021 Alek Smith

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.