# mongoose-soft-deleting

> Package for operating soft delete on mongoose models

Latest version **1.0.1** (published 2020-10-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-soft-deleting
pnpm add mongoose-soft-deleting
yarn add mongoose-soft-deleting
bun add mongoose-soft-deleting
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-10-04 |
| First published | 2020-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | QuantumGlitch |
| Maintainers | exsoax |
| Keywords | mongoose, soft, delete, author, deleted by, deleted at |

## Links

- npm: https://www.npmjs.com/package/mongoose-soft-deleting
- Repository: https://github.com/QuantumGlitch/mongoose-soft-delete
- Homepage: https://github.com/QuantumGlitch/mongoose-soft-delete#readme
- Issues: https://github.com/QuantumGlitch/mongoose-soft-delete/issues
- npm.io page: https://npm.io/package/mongoose-soft-deleting

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-10-04
- 1.0.0 — 2020-09-29

## README

# Package: mongoose-soft-deleting

Package for operating soft delete on mongoose models

# Install

```shell
npm i mongoose-soft-deleting
```

# Usage

By default the plugin include two new fields in the schema to which you want to apply it: \_deleted, \_deletedAt.
Which answer to the questions 'Is the document deleted ?' and 'When has been the document deleted ?'.
Optionally you can specify a \_deletedBy field too to answer the question 'Who deleted it?'.

The package provide a middleware hook too for intercepting the pre soft deleting.

## Example without user

```js
  const softDeletePlugin = require('mongoose-soft-deleting');

  // ...

  const TestSchema = new mongoose.Schema({
    A: { type: String, default: 'A' },
    B: { type: String, default: 'B' },
  });

  TestSchema.plugin(softDeletePlugin,
    {
        deleted: {
            // Optional mongoose field options
            ... deletedOptions
        },

        deletedAt: {
            // Optional mongoose field options
            ... deletedAtOptions
            // Example
            default: Date.now
        }
    }
  );

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

  // ...

  // Pre save
  TestSchema.preSoftDelete(async function(doc){
      // Document is trying to be soft deleted
  })

  // ...

  const doc = await TestModel.findOne(...);

  // To soft delete
  await doc.softDelete(true);

  // To restore from soft delete
  await doc.softDelete(false);

  // To completely remove: call it twice
  await doc.softDelete(true);
  await doc.softDelete(true);

  // or
  await doc.remove();

  // To know if it is deleted
  const isDeleted = doc.isSoftDeleted() || doc._deleted;

  // To know when
  const deletedAt = doc._deletedAt;
```

## Example with user

```js
const softDeletePlugin = require('mongoose-soft-deleting');

// ...

const TestSchema = new mongoose.Schema({
  A: { type: String, default: 'A' },
  B: { type: String, default: 'B' },
});

const UserTestSchema = new mongoose.Schema({
  name: { type: String, default: 'Name' },
  password: { type: String, default: '🤫' },
});

TestSchema.plugin(softDeletePlugin, {
  deletedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'UserTest',
    required: false,
  },
});

const TestModel = mongoose.model('Test', TestSchema);
const UserTestModel = mongoose.model('UserTest', UserTestSchema);

// ...

const user = UserTestModel.findById(...);
const test = TestModel.findById(...);

test.softDelete(true, user);

```

# Test

You can try the tests using the following command ( before you need to change the connection to MongoDB ) :

```shell
npm run test
```

---
_Source: https://npm.io/package/mongoose-soft-deleting · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
