{"id":14975610,"url":"https://github.com/nour-karoui/mongoose-soft-delete","last_synced_at":"2025-08-13T16:32:22.523Z","repository":{"id":44647983,"uuid":"362052937","full_name":"nour-karoui/mongoose-soft-delete","owner":"nour-karoui","description":"a mongoose plugin that allows you to soft delete documents and restore them (for TS \u0026 JS)","archived":false,"fork":false,"pushed_at":"2024-06-17T19:40:54.000Z","size":1908,"stargazers_count":39,"open_issues_count":6,"forks_count":14,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-06T05:25:57.275Z","etag":null,"topics":["javascript","mongodb","mongoose","mongoose-plugin","npm-package","restore","soft-delete","softdelete","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nour-karoui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-27T09:20:56.000Z","updated_at":"2024-12-01T14:18:57.000Z","dependencies_parsed_at":"2024-06-17T21:58:13.290Z","dependency_job_id":null,"html_url":"https://github.com/nour-karoui/mongoose-soft-delete","commit_stats":{"total_commits":44,"total_committers":8,"mean_commits":5.5,"dds":0.2727272727272727,"last_synced_commit":"9e3f6043d494cc27769f3317c9021507fed11026"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nour-karoui%2Fmongoose-soft-delete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nour-karoui%2Fmongoose-soft-delete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nour-karoui%2Fmongoose-soft-delete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nour-karoui%2Fmongoose-soft-delete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nour-karoui","download_url":"https://codeload.github.com/nour-karoui/mongoose-soft-delete/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229770424,"owners_count":18121595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","mongodb","mongoose","mongoose-plugin","npm-package","restore","soft-delete","softdelete","typescript"],"created_at":"2024-09-24T13:52:16.949Z","updated_at":"2025-08-13T16:32:22.475Z","avatar_url":"https://github.com/nour-karoui.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eWelcome to soft-delete-plugin-mongoose 👋\u003c/h1\u003e\n\u003cp\u003e\n  \u003ca href=\"https://www.npmjs.com/package/soft-delete-plugin-mongoose\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Version\" src=\"https://img.shields.io/npm/v/soft-delete-plugin-mongoose.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/nour-karoui/mongoose-soft-delete#readme\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Documentation\" src=\"https://img.shields.io/badge/documentation-yes-brightgreen.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/nour-karoui/mongoose-soft-delete/graphs/commit-activity\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Maintenance\" src=\"https://img.shields.io/badge/Maintained%3F-yes-green.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/nour-karoui/mongoose-soft-delete/blob/master/LICENSE\" target=\"_blank\"\u003e\n    \u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/github/license/bishkou/password-pwnd\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003e a mongoose plugin that allows you to soft delete documents and restore them in MongoDB (for JS \u0026 TS)\n\n* **Soft delete your MongoDB documents and restore them**\n\n* **JS and TS**\n\n\n### 🏠 [Homepage](https://github.com/nour-karoui/mongoose-soft-delete)\n\n\n## Install\n\n```sh\nnpm install soft-delete-plugin-mongoose\n```\n\n## How It Works\n\n**Javascript Version**\n```js\nconst mongoose = require('mongoose');\nconst { softDeletePlugin } = require('soft-delete-plugin-mongoose');\nconst Schema = mongoose.Schema;\n\nconst TestSchema = new Schema({\n    name: String,\n    lastName: String\n});\n\nTestSchema.plugin(softDeletePlugin);\nconst TestModel =  mongoose.model(\"Test\", TestSchema);\n\nconst test = new TestModel({name: 'hello', lastName: \"world\"});\n\n/*** returns an object containing the number of softDeleted elements ***/\n/***\n    {deleted: number} \n***/\n/***\n    the argument options is optional\n***/\nconst options = { validateBeforeSave: false };\nconst deleted = await TestModel.softDelete({ _id: test._id, name: test.name }, options);\n/** \n const deleted = await Test.softDelete({ _id: test._id, name: test.name }); is also valid\n**/\n\n/*** returns an object containing the number of restored elements ***/\n/***\n    {restored: number} \n***/\nconst restored = await TestModel.restore({ _id: test._id, name: test.name });\n\n/*** returns all deleted elements ***/\nconst deletedElements = await TestModel.findDeleted();\n\n/*** returns all available elements (not deleted) ***/\nconst availableElements = await TestModel.find();\n\n/*** counts all available elements (not deleted) ***/\nconst countAvailable = await TestModel.count();\n\n/*** findById returns the document whether deleted or not  ***/\n```\n\n**Typescript Version**\n```ts\nimport * as mongoose from 'mongoose';\nimport { softDeletePlugin, SoftDeleteModel } from 'soft-delete-plugin-mongoose';\n\ninterface Test extends mongoose.Document {\n    name: string;\n    lastName: string;\n}\n\nconst TestSchema = new mongoose.Schema({\n    name: String,\n    lastName: String\n});\nTestSchema.plugin(softDeletePlugin);\n// two different ways of implementing model depending on technology used\n// 1st way\nconst testModel = mongoose.model\u003cTest, SoftDeleteModel\u003cTest\u003e\u003e('Test', TestSchema);\n\n//2nd way (nestjs way)\nconstructor(@InjectModel('Test') private readonly testModel: SoftDeleteModel\u003cTest\u003e) {}\n\nconst test = await new this.testModel({name: 'hello', lastName: 'world'});\n\n/*** returns an object containing the number of softDeleted elements ***/\n/***\n    {deleted: number} \n***/\n/***\n    the argument options is optional\n***/\nconst options = { validateBeforeSave: false };\nconst deleted = await this.testModel.softDelete({ _id: test._id, name: test.name }, options);\n/** \n const deleted = await Test.softDelete({ _id: test._id, name: test.name }); is also valid\n**/\n\n/*** returns an object containing the number of restored elements ***/\n/***\n    {restored: number} \n***/\nconst restored = await this.testModel.restore({ _id: test._id, name: test.name });\n\n/*** returns all deleted elements ***/\nconst deletedElements = await this.testModel.findDeleted();\n\n/*** returns all available elements (not deleted) ***/\nconst availableElements = await this.testModel.find();\n\n/*** counts all available elements (not deleted) ***/\nconst countAvailable = await this.test.count();\n\n/*** findById returns the document whether deleted or not  ***/\n\n/*** NEW in v2.0.0: Aggregation pipeline operations now automatically filter out soft-deleted documents ***/\nconst aggregationResults = await this.testModel.aggregate([\n    { $match: { name: 'hello' } }, // Soft-deleted documents are automatically excluded\n    { $lookup: { from: 'other', localField: '_id', foreignField: 'testId', as: 'related' } } // Lookup also respects soft-delete\n]);\n\n/*** NEW in v2.0.0: distinct() method now supports soft-delete filtering ***/\nconst distinctNames = await this.testModel.distinct('name'); // Returns only non-deleted documents\n\n/*** NEW in v2.0.0: findOneAndUpdate() method now supports soft-delete filtering ***/\nconst updated = await this.testModel.findOneAndUpdate(\n    { name: 'hello' }, \n    { lastName: 'updated' }, \n    { new: true }\n); // Will only find and update non-deleted documents\n```\n\n## What's New\n\n### Version 2.0.0 🎉\n\n**⚠️ Breaking Changes:**\n- Enhanced aggregation pipeline support with automatic soft-delete filtering\n- Improved query hooks for better performance and consistency\n\n**New Features:**\n- **Aggregation Pipeline Support**: `$match` and `$lookup` stages now automatically exclude soft-deleted documents\n- **Enhanced Method Support**: Added soft-delete aware hooks for:\n  - `distinct()` - Returns only non-deleted documents\n  - `findOneAndUpdate()` - Only operates on non-deleted documents\n- **Improved Query Performance**: Optimized query hooks for better database performance\n\n**Migration Guide:**\nIf you were previously working around soft-delete filtering in aggregation pipelines, you can now remove those manual filters as they're handled automatically.\n\n## Author\n\n👤 **Nour**\n\n* Github: [@nour-karoui](https://github.com/nour-karoui)\n* LinkedIn: [@nourkaroui](https://www.linkedin.com/in/nourkaroui/)\n\n## 🤝 Contributing\n\nContributions, issues and feature requests are welcome!\u003cbr /\u003eFeel free to check [issues page](https://github.com/nour-karoui/mongoose-soft-delete/issues). You can also take a look at the [contributing guide](https://github.com/nour-karoui/mongoose-soft-delete/blob/master/CONTRIBUTING.md).\n\n## Show your support\n\nGive a [STAR](https://github.com/nour-karoui/mongoose-soft-delete) if this project helped you!\n\n## 📝 License\n\n* Copyright © 2021 [Nour](https://github.com/nour-karoui).\n* This project is [MIT](https://github.com/nour-karoui/mongoose-soft-delete/blob/master/LICENSE) licensed.\n\n***\n_This README was generated with by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnour-karoui%2Fmongoose-soft-delete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnour-karoui%2Fmongoose-soft-delete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnour-karoui%2Fmongoose-soft-delete/lists"}