mongoose-disable
v0.6.2
Published
Mongoose disable plugin
Downloads
6
Readme
Mongoose Disable Plugin
mongoose-disable is a simple and lightweight plugin that enables document disabling in MongoDB. This code is based on dsanel plugin mongoose-delete
Inspiration
mongoose-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.
Although 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.
Features
- Add disable() method on document (do not override standard remove() method)
- Add disableById() static method
- Add disabled (true-false) key on document
- Add disabledAt key to store time of disabling
- Add disabledBy key to record who disabled document
- Restore disabled documents using enable method
- Bulk disable and enable
- Option to override static methods (count, countDocuments, find, findOne, findOneAndUpdate, update, updateMany)
- For overridden methods we have two additional methods: methodDisabled and methodWithDisabled
- Disable model validation on disabled
- Option to create index on disabled fields (disabled, disabledAt, disabledBy)
- Option to disable use of
$ne
operator using{use$neOperator: false}
. Before you start to use this option please check #50.
Installation
Install using npm
npm install mongoose-disable
Usage
We can use this plugin with or without options.
Simple usage
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: String
});
PetSchema.plugin(mongooseDisabled);
const Pet = mongoose.model('Pet', PetSchema);
const fluffy = new Pet({ name: 'Fluffy' });
fluffy.save(function () {
// mongodb: { disabled: false, name: 'Fluffy' }
// note: you should invoke exactly disable() method instead of standard fluffy.remove()
fluffy.disable(function () {
// mongodb: { disabled: true, name: 'Fluffy' }
fluffy.enable(function () {
// mongodb: { disabled: false, name: 'Fluffy' }
});
});
});
const examplePetId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");
// INFO: Example usage of disableById static method
Pet.disableById(examplePetId, function (err, petDocument) {
// mongodb: { disabled: true, name: 'Fluffy', _id: '53da93b1...' }
});
Save time of disabling
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: String
});
PetSchema.plugin(mongooseDisabled, { disabledAt : true });
const Pet = mongoose.model('Pet', PetSchema);
const fluffy = new Pet({ name: 'Fluffy' });
fluffy.save(function () {
// mongodb: { disabled: false, name: 'Fluffy' }
fluffy.disable(function () {
// mongodb: { disabled: true, name: 'Fluffy', disabledAt: ISODate("2014-08-01T10:34:53.171Z")}
fluffy.enable(function () {
// mongodb: { disabled: false, name: 'Fluffy' }
});
});
});
Who has disabled the data?
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: String
});
PetSchema.plugin(mongooseDisabled, { disabledBy : true });
const Pet = mongoose.model('Pet', PetSchema);
const fluffy = new Pet({ name: 'Fluffy' });
fluffy.save(function () {
// mongodb: { disabled: false, name: 'Fluffy' }
const idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");
fluffy.disable(idUser, function () {
// mongodb: { disabled: true, name: 'Fluffy', disabledBy: ObjectId("53da93b16b4a6670076b16bf")}
fluffy.enable(function () {
// mongodb: { disabled: false, name: 'Fluffy' }
});
});
});
Bulk disable and enable
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: String,
age: Number
});
PetSchema.plugin(mongooseDisabled);
const Pet = mongoose.model('Pet', PetSchema);
const idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");
// Disable multiple object, callback
Pet.disable(function (err, result) { ... });
Pet.disable({age:10}, function (err, result) { ... });
Pet.disable({}, idUser, function (err, result) { ... });
Pet.disable({age:10}, idUser, function (err, result) { ... });
// Disable multiple object, promise
Pet.disable().exec(function (err, result) { ... });
Pet.disable({age:10}).exec(function (err, result) { ... });
Pet.disable({}, idUser).exec(function (err, result) { ... });
Pet.disable({age:10}, idUser).exec(function (err, result) { ... });
// Enable multiple object, callback
Pet.enable(function (err, result) { ... });
Pet.enable({age:10}, function (err, result) { ... });
// Enable multiple object, promise
Pet.enable().exec(function (err, result) { ... });
Pet.enable({age:10}).exec(function (err, result) { ... });
Method overridden
We 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.
| only not disabled documents | only disabled documents | all documents | |----------------------------|-------------------------|-----------------------------| | count() | countDisabled | countWithDisabled | | countDocuments() | countDocumentsDisabled | countDocumentsWithDisabled | | find() | findDisabled | findWithDisabled | | findOne() | findOneDisabled | findOneWithDisabled | | findOneAndUpdate() | findOneAndUpdateDisabled | findOneAndUpdateWithDisabled | | update() | updateDisabled | updateWithDisabled | | updateMany() | updateManyDisabled | updateManyWithDisabled |
Examples how to override one or multiple methods
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: String
});
// Override all methods
PetSchema.plugin(mongooseDisabled, { overrideMethods: 'all' });
// or
PetSchema.plugin(mongooseDisabled, { overrideMethods: true });
// Overide only specific methods
PetSchema.plugin(mongooseDisabled, { overrideMethods: ['count', 'find', 'findOne', 'findOneAndUpdate', 'update'] });
// or
PetSchema.plugin(mongooseDisabled, { overrideMethods: ['count', 'countDocuments', 'find'] });
// or (unrecognized method names will be ignored)
PetSchema.plugin(mongooseDisabled, { overrideMethods: ['count', 'find', 'errorXyz'] });
const Pet = mongoose.model('Pet', PetSchema);
// Example of usage overridden methods
Pet.find(function (err, documents) {
// will return only NOT DISABLED documents
});
Pet.findDisabled(function (err, documents) {
// will return only DISABLED documents
});
Pet.findWithDisabled(function (err, documents) {
// will return ALL documents
});
Disable model validation on disable
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: { type: String, required: true }
});
// By default, validateBeforeDisable is set to true
PetSchema.plugin(mongooseDisabled);
// the previous line is identical to next line
PetSchema.plugin(mongooseDisabled, { validateBeforeDisable: true });
// To disable model validation on disable, set validateBeforeDisable option to false
PetSchema.plugin(mongooseDisabled, { validateBeforeDisable: false });
// NOTE: This is based on existing Mongoose validateBeforeSave option
// http://mongoosejs.com/docs/guide.html#validateBeforeSave
Create index on fields
const mongooseDisabled = require('mongoose-disable');
const PetSchema = new Schema({
name: String
});
// Index all field related to plugin (disabled, disabledAt, disabledBy)
PetSchema.plugin(mongooseDisabled, { indexFields: 'all' });
// or
PetSchema.plugin(mongooseDisabled, { indexFields: true });
// Index only specific fields
PetSchema.plugin(mongooseDisabled, { indexFields: ['disabled', 'disabledBy'] });
// or
PetSchema.plugin(mongooseDisabled, { indexFields: ['disabledAt'] });
License
The MIT License
Copyright (c) 2020 Ernesto García https://github.com/ernestognw
Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE 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.