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

soft-deleting-mongoose

v1.0.3

Published

This is a simple, easy-to-understand soft deleting package for MongoDB using Mongoose ODM.

Downloads

3

Readme

soft-deleting-mongoose

This is a simple, easy-to-understand soft deleting package for MongoDB using Mongoose ODM. This package gives you all functionality to soft delete any Mongodb document. So let's start soft deleting...

Installation

npm install soft-deleting-mongoose

Usage

Defining schema

Firstly import MongooseSchema and create a schema.

const userSchema = new MongooseSchema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
    },
    role: {
        type: String,
        default: "user"
    },
    password: String
});

Creating a model

const User = mongoose.model('User', userSchema);

Delete a user by using soft deleting

app.delete("/user/:_id", async (req, res, next) => {
    try {
        const _id = req.params._id;
        const user = await User.softDeleteById(_id);

        return res.status(200).json({ success: true, data: user });
    } catch (error) {
        next(error);
    }
});

Provided the following query helpers for soft deleting.

  • .notDeleted()
  • .onlyDeleted()
  • .withDeleted()

Provided the following static functions for soft deleting.

  • Model.softDelete(query)
  • Model.softDeleteById(_id)
  • Model.restoreById(_id)
  • Model.restore(query)
  • Model.restoreAll()
  • Model.forceDeleteById(_id)
  • Model.forceDelete(query)

Usage Example of query helpers

In the above, I have declared a model named User. Now I am giving examples using the User model.

.notDeleted()

  • Get not deleted documents
User.find({}).notDeleted();

.onlyDeleted()

  • Get only deleted documents
User.find({}).onlyDeleted();

.withDeleted()

  • Get both types of documents (deleted + not deleted)
User.find({}).withDeleted();

Usage Example of static functions

In the above, I have declared a model named User. Now I am giving examples using the User model.

Model.softDelete(query)

  • Soft delete the matched documents by query
User.softDelete({ role: "user" });

Model.softDeleteById(_id)

  • Soft delete the document by _id
const _id = "6405d153a208cbfd7b88b0c5";
User.softDeleteById(_id);

Model.restoreById(_id)

  • Restore the deleted document by _id
const _id = "6405d153a208cbfd7b88b0c5";
User.restoreById(_id);

Model.restore(query)

  • Restore the matched deleted documents by query
const _id = "6405d153a208cbfd7b88b0c5";
User.restoreById(_id);

Model.restoreAll()

  • Restore all deleted documents
User.restoreAll();

Model.forceDeleteById(_id)

  • Permanently delete a document by _id
const _id = "6405d153a208cbfd7b88b0c5";
User.forceDeleteById(_id);

Model.forceDelete(query)

  • Permanently delete the matched documents by query
User.forceDeleteById({ role: "sub-admin" });

Usage Example in TypeScript

Creating a model

const User = mongoose.model<IUserDoc, Model<IUserDoc, IQueryHelpers<IUserDoc>>>('User', userSchema);

Heder, IUserDoc is the interface of user, Model is imported from mongoose, and IQueryHelpers is imported from soft-deleting-mongoose package.