{"id":15924303,"url":"https://github.com/ernestognw/mongoose-disable","last_synced_at":"2026-01-31T01:15:05.233Z","repository":{"id":43976196,"uuid":"247565735","full_name":"ernestognw/mongoose-disable","owner":"ernestognw","description":"Mongoose disable plugin","archived":false,"fork":false,"pushed_at":"2023-07-18T21:17:49.000Z","size":600,"stargazers_count":0,"open_issues_count":12,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-09T03:35:20.179Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/ernestognw.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2020-03-15T22:55:06.000Z","updated_at":"2023-02-03T21:03:34.000Z","dependencies_parsed_at":"2023-02-18T11:04:48.127Z","dependency_job_id":null,"html_url":"https://github.com/ernestognw/mongoose-disable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ernestognw%2Fmongoose-disable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ernestognw%2Fmongoose-disable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ernestognw%2Fmongoose-disable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ernestognw%2Fmongoose-disable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ernestognw","download_url":"https://codeload.github.com/ernestognw/mongoose-disable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247005412,"owners_count":20868019,"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":[],"created_at":"2024-10-06T21:21:21.791Z","updated_at":"2026-01-31T01:15:05.184Z","avatar_url":"https://github.com/ernestognw.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Mongoose Disable Plugin\n=========\n\nmongoose-disable is a simple and lightweight plugin that enables document disabling in MongoDB. This code is based on [dsanel](https://github.com/dsanel) plugin [mongoose-delete](https://github.com/dsanel/mongoose-delete)\n\n[![Build Status](https://travis-ci.com/ernestognw/mongoose-disable.svg?branch=master)](https://travis-ci.com/ernestognw/mongoose-disable)\n[![Coverage Status](https://coveralls.io/repos/github/ernestognw/mongoose-disable/badge.svg?branch=master)](https://coveralls.io/github/ernestognw/mongoose-disable?branch=master)\n\n## Inspiration\nmongoose-delete is a great implementation of a soft delete mechanism that enables users to hide documents without really deleting them. This isually allows to maintain references from other documents in DB without breaking your whole application.\n\nAlthough mongoose-delete is perfect for delete cases, deletion is a different state than inactive. And, for some cases, you can use deletion just to keep references working, but it can be considered as a non-recoverable state from user's perspective, which leads yout to search for intermediate states for particular reasons, such as temporary hiddens, archives, etc. This is why I decided to fork the initial implementation in order to have another flag for intermediate states between deleted and non deleted.\n\n## Features\n  - [Add __disable()__ method on document (do not override standard __remove()__ method)](#simple-usage)\n  - [Add __disableById()__ static method](#simple-usage)\n  - [Add __disabled__ (true-false) key on document](#simple-usage)\n  - [Add __disabledAt__ key to store time of disabling](#save-time-of-disabling)\n  - [Add __disabledBy__ key to record who disabled document](#who-has-disabled-the-data)\n  - Restore disabled documents using __enable__ method\n  - [Bulk disable and enable](#bulk-disabled-and-enable)\n  - [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, countDocuments, find, findOne, findOneAndUpdate, update, updateMany__)\n  - [For overridden methods we have two additional methods](#method-overridden): __methodDisabled__ and __methodWithDisabled__\n  - [Disable model validation on disabled](#disable-model-validation-on-disabled)\n  - [Option to create index on disabled fields](#create-index-on-fields) (__disabled__, __disabledAt__, __disabledBy__)\n  - Option to disable use of `$ne` operator using `{use$neOperator: false}`. Before you start to use this option please check [#50](https://github.com/dsanel/mongoose-delete/issues/50).  \n\n## Installation\nInstall using [npm](https://npmjs.org)\n```\nnpm install mongoose-disable\n```\n## Usage\n\nWe can use this plugin with or without options.\n\n### Simple usage\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: String\n});\n\nPetSchema.plugin(mongooseDisabled);\n\nconst Pet = mongoose.model('Pet', PetSchema);\n\nconst fluffy = new Pet({ name: 'Fluffy' });\n\nfluffy.save(function () {\n    // mongodb: { disabled: false, name: 'Fluffy' }\n\n    // note: you should invoke exactly disable() method instead of standard fluffy.remove()\n    fluffy.disable(function () {\n        // mongodb: { disabled: true, name: 'Fluffy' }\n\n        fluffy.enable(function () {\n            // mongodb: { disabled: false, name: 'Fluffy' }\n        });\n    });\n\n});\n\nconst examplePetId = mongoose.Types.ObjectId(\"53da93b16b4a6670076b16bf\");\n\n// INFO: Example usage of disableById static method\nPet.disableById(examplePetId, function (err, petDocument) {\n    // mongodb: { disabled: true, name: 'Fluffy', _id: '53da93b1...' }\n});\n\n```\n\n\n### Save time of disabling\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: String\n});\n\nPetSchema.plugin(mongooseDisabled, { disabledAt : true });\n\nconst Pet = mongoose.model('Pet', PetSchema);\n\nconst fluffy = new Pet({ name: 'Fluffy' });\n\nfluffy.save(function () {\n    // mongodb: { disabled: false, name: 'Fluffy' }\n\n    fluffy.disable(function () {\n        // mongodb: { disabled: true, name: 'Fluffy', disabledAt: ISODate(\"2014-08-01T10:34:53.171Z\")}\n\n        fluffy.enable(function () {\n            // mongodb: { disabled: false, name: 'Fluffy' }\n        });\n    });\n\n});\n```\n\n\n### Who has disabled the data?\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: String\n});\n\nPetSchema.plugin(mongooseDisabled, { disabledBy : true });\n\nconst Pet = mongoose.model('Pet', PetSchema);\n\nconst fluffy = new Pet({ name: 'Fluffy' });\n\nfluffy.save(function () {\n    // mongodb: { disabled: false, name: 'Fluffy' }\n\n    const idUser = mongoose.Types.ObjectId(\"53da93b16b4a6670076b16bf\");\n\n    fluffy.disable(idUser, function () {\n        // mongodb: { disabled: true, name: 'Fluffy', disabledBy: ObjectId(\"53da93b16b4a6670076b16bf\")}\n\n        fluffy.enable(function () {\n            // mongodb: { disabled: false, name: 'Fluffy' }\n        });\n    });\n\n});\n```\n\n### Bulk disable and enable\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: String,\n    age: Number\n});\n\nPetSchema.plugin(mongooseDisabled);\n\nconst Pet = mongoose.model('Pet', PetSchema);\n\nconst idUser = mongoose.Types.ObjectId(\"53da93b16b4a6670076b16bf\");\n\n// Disable multiple object, callback\nPet.disable(function (err, result) { ... });\nPet.disable({age:10}, function (err, result) { ... });\nPet.disable({}, idUser, function (err, result) { ... });\nPet.disable({age:10}, idUser, function (err, result) { ... });\n\n// Disable multiple object, promise\nPet.disable().exec(function (err, result) { ... });\nPet.disable({age:10}).exec(function (err, result) { ... });\nPet.disable({}, idUser).exec(function (err, result) { ... });\nPet.disable({age:10}, idUser).exec(function (err, result) { ... });\n\n// Enable multiple object, callback\nPet.enable(function (err, result) { ... });\nPet.enable({age:10}, function (err, result) { ... });\n\n// Enable multiple object, promise\nPet.enable().exec(function (err, result) { ... });\nPet.enable({age:10}).exec(function (err, result) { ... });\n```\n\n### Method overridden\n\nWe have the option to override all standard methods or only specific methods. Overridden methods will exclude disabled documents from results, documents that have ```disabled = true```. Every overridden method will have two additional methods, so we will be able to work with disabled documents.\n\n| only not disabled documents | only disabled documents  | all documents               |\n|----------------------------|-------------------------|-----------------------------|\n| count()                    | countDisabled            | countWithDisabled            |\n| countDocuments()           | countDocumentsDisabled   | countDocumentsWithDisabled   |\n| find()                     | findDisabled             | findWithDisabled             |\n| findOne()                  | findOneDisabled          | findOneWithDisabled          |\n| findOneAndUpdate()         | findOneAndUpdateDisabled | findOneAndUpdateWithDisabled |\n| update()                   | updateDisabled           | updateWithDisabled           |\n| updateMany()               | updateManyDisabled       | updateManyWithDisabled       |\n\n### Examples how to override one or multiple methods\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: String\n});\n\n// Override all methods\nPetSchema.plugin(mongooseDisabled, { overrideMethods: 'all' });\n// or \nPetSchema.plugin(mongooseDisabled, { overrideMethods: true });\n\n// Overide only specific methods\nPetSchema.plugin(mongooseDisabled, { overrideMethods: ['count', 'find', 'findOne', 'findOneAndUpdate', 'update'] });\n// or\nPetSchema.plugin(mongooseDisabled, { overrideMethods: ['count', 'countDocuments', 'find'] });\n// or (unrecognized method names will be ignored)\nPetSchema.plugin(mongooseDisabled, { overrideMethods: ['count', 'find', 'errorXyz'] });\n\n\nconst Pet = mongoose.model('Pet', PetSchema);\n\n// Example of usage overridden methods\n\nPet.find(function (err, documents) {\n  // will return only NOT DISABLED documents\n});\n\nPet.findDisabled(function (err, documents) {\n  // will return only DISABLED documents\n});\n\nPet.findWithDisabled(function (err, documents) {\n  // will return ALL documents\n});\n\n```\n\n### Disable model validation on disable\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: { type: String, required: true }\n});\n\n// By default, validateBeforeDisable is set to true\nPetSchema.plugin(mongooseDisabled);\n// the previous line is identical to next line\nPetSchema.plugin(mongooseDisabled, { validateBeforeDisable: true });\n\n// To disable model validation on disable, set validateBeforeDisable option to false\nPetSchema.plugin(mongooseDisabled, { validateBeforeDisable: false });\n\n// NOTE: This is based on existing Mongoose validateBeforeSave option\n// http://mongoosejs.com/docs/guide.html#validateBeforeSave\n\n```\n\n### Create index on fields\n\n```javascript\nconst mongooseDisabled = require('mongoose-disable');\n\nconst PetSchema = new Schema({\n    name: String\n});\n\n// Index all field related to plugin (disabled, disabledAt, disabledBy)\nPetSchema.plugin(mongooseDisabled, { indexFields: 'all' });\n// or \nPetSchema.plugin(mongooseDisabled, { indexFields: true });\n\n// Index only specific fields\nPetSchema.plugin(mongooseDisabled, { indexFields: ['disabled', 'disabledBy'] });\n// or\nPetSchema.plugin(mongooseDisabled, { indexFields: ['disabledAt'] });\n\n\n```\n\n## License\n\nThe MIT License\n\nCopyright (c) 2020 Ernesto García https://github.com/ernestognw\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fernestognw%2Fmongoose-disable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fernestognw%2Fmongoose-disable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fernestognw%2Fmongoose-disable/lists"}