{"id":14975608,"url":"https://github.com/go-die/soft-delete-mongoose-plugin","last_synced_at":"2026-03-05T00:32:16.658Z","repository":{"id":43014493,"uuid":"468595683","full_name":"GO-DIE/soft-delete-mongoose-plugin","owner":"GO-DIE","description":"A simple and friendly soft delete plugin for mongoose，implementation using TS.","archived":false,"fork":false,"pushed_at":"2022-11-01T15:17:01.000Z","size":847,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T06:11:12.185Z","etag":null,"topics":["mongoose","mongoose-plugin","node","soft-delete","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/GO-DIE.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}},"created_at":"2022-03-11T03:33:45.000Z","updated_at":"2023-04-28T15:10:29.000Z","dependencies_parsed_at":"2023-01-20T19:02:27.268Z","dependency_job_id":null,"html_url":"https://github.com/GO-DIE/soft-delete-mongoose-plugin","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GO-DIE%2Fsoft-delete-mongoose-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GO-DIE%2Fsoft-delete-mongoose-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GO-DIE%2Fsoft-delete-mongoose-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GO-DIE%2Fsoft-delete-mongoose-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GO-DIE","download_url":"https://codeload.github.com/GO-DIE/soft-delete-mongoose-plugin/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238508511,"owners_count":19484143,"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":["mongoose","mongoose-plugin","node","soft-delete","typescript"],"created_at":"2024-09-24T13:52:16.766Z","updated_at":"2025-10-27T14:30:33.285Z","avatar_url":"https://github.com/GO-DIE.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"soft-delete-mongoose-plugin\n================\n\nA simple and friendly soft delete plugin for mongoose，implementation using TS.\n\nMethods were added and overridden on mongoose model to realize soft deletion logic.\n\n# Features\n\n- Soft delete data using soft delete flag and date markers is friendly for scenarios where a unique index needs to be created\n- User-defined soft delete field names are supported\n- [Add independent soft delete methods to the mongoose model](#soft-delete-methods), all hard delete methods are retained completely\n- Rewrite all query and update methods on mongoose Model and automatically inject soft delete filtering conditions; If the user filter contains any queries related to soft delete fields, the program will assume that the user needs to have full control of the data and will not automatically inject soft delete filtering conditions\n\n\n\n# Quick Start\n\n## Install\n\n```\n$ npm install soft-delete-mongoose-plugin\n```\n\n\n\n## Usage\n\n### Typescript\n\nUse of the **SoftDeleteModel** type, instead of the **Model** type.\n\n```typescript\nimport { set, Schema, model, connect, connection, plugin } from 'mongoose';\nimport { SoftDelete, SoftDeleteModel } from 'soft-delete-mongoose-plugin';\n\nasync function main() {\n  set('debug', true);\n\n  await connect('mongodb://localhost:27017/test?directConnection=true');\n\n  // defind soft delete field name\n  const IS_DELETED_FIELD = 'isDeleted';\n  const DELETED_AT_FIELD = 'deletedAt';\n\n  // use soft delete plugin\n  plugin(\n    new SoftDelete({\n      isDeletedField: IS_DELETED_FIELD,\n      deletedAtField: DELETED_AT_FIELD,\n    }).getPlugin(),\n  );\n\n  interface ISoftDelete {\n    [IS_DELETED_FIELD]: boolean;\n    [DELETED_AT_FIELD]: Date | null;\n  }\n\n  interface IPerson extends ISoftDelete {\n    name: string;\n  }\n\n  const personSchema = new Schema\u003cIPerson\u003e({\n    name: { type: String, required: true },\n    isDeleted: { type: Boolean, default: false },\n    deletedAt: { type: Date, default: null },\n  });\n\n  // use of the SoftDeleteModel type, instead of the Model type.\n  const personModel = model\u003cIPerson, SoftDeleteModel\u003cIPerson\u003e\u003e(\n    'persons',\n    personSchema,\n  );\n\n  // It's ready to use studentModel to soft delete documents\n  await personModel.softDeleteMany();\n\n  await connection.close();\n}\n\nmain();\n```\n\n\n\n### JavaScript\n\n```javascript\nconst { set, Schema, model, connect, connection, plugin } = require('mongoose');\nconst { SoftDelete } = require('soft-delete-mongoose-plugin');\n\nasync function main() {\n  set('debug', true);\n\n  await connect('mongodb://localhost:27017/test?directConnection=true');\n\n  // defind soft delete field name\n  const IS_DELETED_FIELD = 'isDeleted';\n  const DELETED_AT_FIELD = 'deletedAt';\n\n  // use soft delete plugin\n  plugin(\n    new SoftDelete({\n      isDeletedField: IS_DELETED_FIELD,\n      deletedAtField: DELETED_AT_FIELD,\n    }).getPlugin(),\n  );\n\n  const personSchema = new Schema({\n    name: { type: String, required: true },\n    isDeleted: { type: Boolean, default: false },\n    deletedAt: { type: Date, default: null },\n  });\n\n  // use of the SoftDeleteModel type, instead of the Model type.\n  const personModel = model('persons', personSchema);\n\n  // It's ready to use studentModel to soft delete documents\n  await personModel.softDeleteMany();\n\n  await connection.close();\n}\n\nmain();\n```\n\n\n\n# API\n\n## Class: SoftDelete\n\n**Parameters：**\n\n- *options* **\\\u003cObject\\\u003e**\n\n    *isDeletedField* **\\\u003cstring\\\u003e**  Soft delete flag field name, field type: **boolean**\n\n    *deletedAtField* **\\\u003cstring\\\u003e**  Soft delete date field name, field type: **Date | null**\n\n    *mongoDBVersion*? **\\\u003cstring\\\u003e**  Rewrite with better query statements based on the mongoDB version used, default the last MongoDB version\n\n    *override* **\\\u003cOverrideOptions\\\u003e** Sets whether the specified method needs to be overridden\n\n    \n\n\u003e Overridden model methods are supported by default：\n\u003e\n\u003e aggregate, bulkWrite, count, countDocuments, distinct, exists, find, findOne, findOneAndReplace, findOneAndUpdate, replaceOne, update, updateMany, updateOne\n\n\n\n**Example usage:**\n\n```typescript\nnew SoftDelete({\n  isDeletedField: 'isDeleted',\n  deletedAtField: 'deletedAt',\n  mongoDBVersion: \"5.0.5\",\n  override: { aggregate: false }, // not override aggregate method\n});\n```\n\n\n\n## Method: softDelete.getPlugin\n\n**return** **\\\u003cFunction\\\u003e**  The mongoose plugin function\n\n\n\n# Soft delete methods\n\nAdd independent soft delete methods to the mongoose model, the soft delete method actually calls the corresponding mongoose model update method：\n\n| soft delete method    | update method     |\n| --------------------- | ----------------- |\n| softDeleteOne         | updateOne         |\n| softDeleteMany        | updateMany        |\n| findByIdAndSoftDelete | findByIdAndUpdate |\n\nThese functions take the same parameters as the corresponding update methods, except that the update option parameters are automatically replaced with soft delete field updates.  \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-die%2Fsoft-delete-mongoose-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-die%2Fsoft-delete-mongoose-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-die%2Fsoft-delete-mongoose-plugin/lists"}