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-delete-mongoose-plugin

v1.1.3

Published

mongoose soft delete plugin

Downloads

864

Readme

soft-delete-mongoose-plugin

A simple and friendly soft delete plugin for mongoose,implementation using TS.

Methods were added and overridden on mongoose model to realize soft deletion logic.

Features

  • Soft delete data using soft delete flag and date markers is friendly for scenarios where a unique index needs to be created
  • User-defined soft delete field names are supported
  • Add independent soft delete methods to the mongoose model, all hard delete methods are retained completely
  • Rewrite all query and update methods on mongoose Model and automatically inject soft delete filtering conditions; If the user filter contains any queries related to soft delete fields, the program will assume that the user needs to have full control of the data and will not automatically inject soft delete filtering conditions

Quick Start

Install

$ npm install soft-delete-mongoose-plugin

Usage

Typescript

Use of the SoftDeleteModel type, instead of the Model type.

import { set, Schema, model, connect, connection, plugin } from 'mongoose';
import { SoftDelete, SoftDeleteModel } from 'soft-delete-mongoose-plugin';

async function main() {
  set('debug', true);

  await connect('mongodb://localhost:27017/test?directConnection=true');

  // defind soft delete field name
  const IS_DELETED_FIELD = 'isDeleted';
  const DELETED_AT_FIELD = 'deletedAt';

  // use soft delete plugin
  plugin(
    new SoftDelete({
      isDeletedField: IS_DELETED_FIELD,
      deletedAtField: DELETED_AT_FIELD,
    }).getPlugin(),
  );

  interface ISoftDelete {
    [IS_DELETED_FIELD]: boolean;
    [DELETED_AT_FIELD]: Date | null;
  }

  interface IPerson extends ISoftDelete {
    name: string;
  }

  const personSchema = new Schema<IPerson>({
    name: { type: String, required: true },
    isDeleted: { type: Boolean, default: false },
    deletedAt: { type: Date, default: null },
  });

  // use of the SoftDeleteModel type, instead of the Model type.
  const personModel = model<IPerson, SoftDeleteModel<IPerson>>(
    'persons',
    personSchema,
  );

  // It's ready to use studentModel to soft delete documents
  await personModel.softDeleteMany();

  await connection.close();
}

main();

JavaScript

const { set, Schema, model, connect, connection, plugin } = require('mongoose');
const { SoftDelete } = require('soft-delete-mongoose-plugin');

async function main() {
  set('debug', true);

  await connect('mongodb://localhost:27017/test?directConnection=true');

  // defind soft delete field name
  const IS_DELETED_FIELD = 'isDeleted';
  const DELETED_AT_FIELD = 'deletedAt';

  // use soft delete plugin
  plugin(
    new SoftDelete({
      isDeletedField: IS_DELETED_FIELD,
      deletedAtField: DELETED_AT_FIELD,
    }).getPlugin(),
  );

  const personSchema = new Schema({
    name: { type: String, required: true },
    isDeleted: { type: Boolean, default: false },
    deletedAt: { type: Date, default: null },
  });

  // use of the SoftDeleteModel type, instead of the Model type.
  const personModel = model('persons', personSchema);

  // It's ready to use studentModel to soft delete documents
  await personModel.softDeleteMany();

  await connection.close();
}

main();

API

Class: SoftDelete

Parameters:

  • options <Object>

    isDeletedField <string> Soft delete flag field name, field type: boolean

    deletedAtField <string> Soft delete date field name, field type: Date | null

    mongoDBVersion? <string> Rewrite with better query statements based on the mongoDB version used, default the last MongoDB version

    override <OverrideOptions> Sets whether the specified method needs to be overridden

Overridden model methods are supported by default:

aggregate, bulkWrite, count, countDocuments, distinct, exists, find, findOne, findOneAndReplace, findOneAndUpdate, replaceOne, update, updateMany, updateOne

Example usage:

new SoftDelete({
  isDeletedField: 'isDeleted',
  deletedAtField: 'deletedAt',
  mongoDBVersion: "5.0.5",
  override: { aggregate: false }, // not override aggregate method
});

Method: softDelete.getPlugin

return <Function> The mongoose plugin function

Soft delete methods

Add independent soft delete methods to the mongoose model, the soft delete method actually calls the corresponding mongoose model update method:

| soft delete method | update method | | --------------------- | ----------------- | | softDeleteOne | updateOne | | softDeleteMany | updateMany | | findByIdAndSoftDelete | findByIdAndUpdate |

These functions take the same parameters as the corresponding update methods, except that the update option parameters are automatically replaced with soft delete field updates.