# mongoose-trashcan

> Mongoose softDelete Plugin that puts deleted documents in an archived collection

Latest version **1.0.5** (published 2019-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-trashcan
pnpm add mongoose-trashcan
yarn add mongoose-trashcan
bun add mongoose-trashcan
```

## 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.5 |
| Published | 2019-11-22 |
| First published | 2019-11-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Author | Simon Holm |
| Maintainers | spixooze |
| Keywords | mongoose, typegoose, soft, delete, remove, softdelete, archive, archiving, plugin |

## Links

- npm: https://www.npmjs.com/package/mongoose-trashcan
- Repository: https://github.com/spixooze/mongoose-trashcan
- Homepage: https://github.com/spixooze/mongoose-trashcan#README.md
- Issues: https://github.com/spixooze/mongoose-trashcan/issues
- npm.io page: https://npm.io/package/mongoose-trashcan

## Dependencies (1)

- [mongoose](https://npm.io/package/mongoose.md) ^5.7.12

## 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.5 (latest) — 2019-11-22
- 1.0.4 — 2019-11-22
- 1.0.3 — 2019-11-22
- 1.0.2 — 2019-11-22
- 1.0.1 — 2019-11-22
- 1.0.0 — 2019-11-22

## README

# Mongoose Trashcan 🗑

Eat & Keep the 🍰 with the Mongoose Trashcan Plugin

Delete documents as you normally do but have them automagically end up in an archived collection - just in case you need to pull something out of the trash in the future.

## How

### Install

```
npm install mongoose-trashcan
/* or */
yarn add mongoose-trashcan
```

### Create a Mongoose Model for archived items

See the example folder for implementation in JS, Typescript and Typegoose. Copy 🍝

```javascript
// Example Model Schema using Mongoose and JS
const archivedItemSchema = new Schema({
  originCollection: {
    type: String,
    required: true
  },
  doc: {
    type: Schema.Types.Mixed,
    required: true
  }
});

archivedItemSchema.methods.restore = async function(restoreGlueFn) {
  try {
    await restoreGlueFn(this.originCollection, this.doc);
    await this.remove();
  } catch (error) {
    throw error;
  }
};

const ArchivedItemModel = model("ArchivedItem", archivedItemSchema);
export { ArchivedItemModel };
```

### Plug-in the Plugin

```javascript
// Import the plugin from "mongoose-trashcan" and your own model for the archived item
import { trashcan } from "mongoose-trashcan";
import { ArchivedItemModel } from "../YourModelsDirectory";

/* Your Schema... */
// pass your model for the archived item
yourSchema.plugin(trashcan, { ArchivedItemModel });
```

```typescript
// Using Typegoose?
import { trashcan } from "mongoose-trashcan";

@plugin(traschan, { ArchivedItemModel })
class YourTypegooseClass {
  //...
}
```

### Go Dumpster diving

Restore an archived item by calling the restore method.
This requires your own logic of what to do with the archived document, what model should be recreated etc.

Based on the name of the collection the archived document came from (originCollection) you can discern what model should be used to recreate the document.

In the example we assume the archived document is a user from a collection called users.

```javascript
import { ArchivedItemModel } from '../YourModelsDirectory'

// Find archived item by its id or query based on the type of document you archived.
const archivedItem = await ArchivedItemModel.findById(someId)

// 🕯💀
await archivedItem.restore(async (originCollection, doc) => {
  if (originCollection === "users") {
    await YourUserModel.create(doc)
  } else {
    throw `Expected archived document to be User but it's origin is ${originCollection}`,
  }
})

```

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