1.0.10 • Published 5 months ago

delete-soft-mongoose v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

Mongoose Soft Delete Plugin

This Mongoose plugin provides soft deletion functionality for MongoDB, allowing you to mark documents as deleted without physically removing them from the database.

Installation

npm install delete-soft-mongoose
yarn add delete-soft-mongoose

Usage

  1. Import the plugin in your Mongoose schema file.

    const softDeletePlugin = require('delete-soft-mongoose');
  2. Apply the plugin.

    Apply the plugin To your schema:

    const YourSchema = new mongoose.Schema({
      // ... your schema fields
    });
    YourSchema.plugin(softDeletePlugin);

    Apply the plugin globally.

       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.

    const exampleDocument = new YourModel({
      // ... your document fields
    });
    
    // Soft delete document
    await exampleDocument.softDelete();

Methods

softDelete(query, options, deletedBy)

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.

softDeleteById(id, deletedBy)

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.

restore(query)

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.

restoreById(id)

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.

findAll(query)

Finds all documents that are not marked as deleted.

  • query: Additional query parameters.

Returns an array of documents.

findDeleted(query)

Finds all documents that are marked as deleted.

  • query: Additional query parameters.

Returns an array of documents.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

  • Inspired by the need for soft deletion in MongoDB.

Contributing

Simply fork and send pull request.

Issues

Please use the issue tracker to report any issues.

Authors

  • Smile Pant

Acknowledgments

..........................


1.0.10

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago