Giter Club home page Giter Club logo

soft-deleting-mongoose's Introduction

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.

soft-deleting-mongoose's People

Contributors

muhammadrifat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.