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 🍝
// Example Model Schema using Mongoose and JSconst archivedItemSchema = originCollection: type: String required: true doc: type: SchemaTypesMixed required: true ; archivedItemSchemamethods { try await ; await this; catch error throw error; }; const ArchivedItemModel = ;;
Plug-in the Plugin
// Import the plugin from "mongoose-trashcan" and your own model for the archived item;; /* Your Schema... */// pass your model for the archived itemyourSchema;
// Using Typegoose?;
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.
// Find archived item by its id or query based on the type of document you archived.const archivedItem = await ArchivedItemModel // 🕯💀await archivedItem