npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

Build Status Coverage Status

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

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.