delete-soft-mongoose
Version:
soft delete for mongoose, mongodb javascript
128 lines (77 loc) • 2.96 kB
Markdown
This Mongoose plugin provides soft deletion functionality for MongoDB, allowing you to mark documents as deleted without physically removing them from the database.
```bash
npm install delete-soft-mongoose
```
```bash
yarn add delete-soft-mongoose
```
1. Import the plugin in your Mongoose schema file.
```javascript
const softDeletePlugin = require('delete-soft-mongoose');
```
2. Apply the plugin.
Apply the plugin To your schema:
```javascript
const YourSchema = new mongoose.Schema({
// ... your schema fields
});
YourSchema.plugin(softDeletePlugin);
```
Apply the plugin globally.
```javascript
const mongoose = require("mongoose");
const softDeletePlugin = require("delete-soft-mongoose");
mongoose.plugin(softDeletePlugin)
```
3. Your schema will now have a `deleted` field with `status`, `at`, and `by` properties.
```javascript
const exampleDocument = new YourModel({
// ... your document fields
});
// Soft delete document
await exampleDocument.softDelete();
```
Soft deletes documents based on the provided query.
- `query`: The query to find documents to soft-delete.
- `options`: Options to pass to the `save` method.
- `deletedBy`: The user or entity performing the soft deletion.
Returns an object with the count of soft-deleted documents.
Soft deletes a document by its ID.
- `id`: The ID of the document to soft-delete.
- `deletedBy`: The user or entity performing the soft deletion.
Returns an object with the count of soft-deleted documents and a message.
Restores soft-deleted documents based on the provided query.
- `query`: The query to find documents to restore.
Returns an object with the count of restored documents.
Restores a soft-deleted document by its ID.
- `id`: The ID of the document to restore.
Returns an object with the count of restored documents and a message.
Finds all documents that are not marked as deleted.
- `query`: Additional query parameters.
Returns an array of documents.
Finds all documents that are marked as deleted.
- `query`: Additional query parameters.
Returns an array of documents.
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
- Inspired by the need for soft deletion in MongoDB.
Simply fork and send pull request.
Please use the [issue tracker](https://github.com/smilepant/delete-soft-mongoose/issues) to report any issues.
- Smile Pant
..........................
---