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-delete-ts

v2.1.0

Published

Mongoose soft delete plugin

Downloads

1,271

Readme

Mongoose Delete TS Plugin

mongoose-delete-ts is simple and lightweight plugin that enables soft deletion of documents in MongoDB. This code is based on plugin mongoose-delete. But completely re-written in TypeScript and using mongoose query helpers.

Build Status

Installation

Install using npm

npm install mongoose-delete-ts

Before you get started

This plugin uses $equal: true for improved query performance. If you are adding this plugin to an existing project, make sure to manually update all existing documents with deleted=false

Usage

We can use this plugin with or without options.

Simple usage

import mongooseDelete, { Deleted, DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts';

type Pet = { name: string } & Deleted;
type PetQueryHelpers = DeletedQueryHelpers<Pet>;
type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers>;
type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods> & PetStaticMethods;

const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({
	name: String
});

petSchema.plugin(mongooseDelete);

const PetPetModel = mongoose.model<PetModel>('Pet', petSchema);

const fluffy = new PetModel({ name: 'Fluffy' });

await fluffy.save();
// mongodb: { name: 'Fluffy', deleted: false }
await fluffy.delete();
// mongodb: { name: 'Fluffy', deleted: true }
await fluffy.restore();
// mongodb: { name: 'Fluffy', deleted: false }

const examplePetId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

const petDocument = await PetModel.deleteById(examplePetId);
// mongodb: { _id: '53da93b1...', name: 'Fluffy', deleted: true }

Deleted at timestamp

import mongooseDelete, { Deleted, DeletedAt DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts';

type Pet = { name: string } & Deleted & DeletedAt;
type PetQueryHelpers = DeletedQueryHelpers<Pet>;
type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers>;
type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods> & PetStaticMethods;

const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({
	name: String
});

petSchema.plugin(mongooseDelete, { deletedAt: true });

const PetModel = mongoose.model<Pet, PetModel>('Pet', petSchema);

const fluffy = new PetModel({ name: 'Fluffy' });

await fluffy.save();
// mongodb: { name: 'Fluffy', deleted: false }

await fluffy.delete();
// mongodb: { name: 'Fluffy', deleted: true, deletedAt: ISODate("2014-08-01T10:34:53.171Z")}

await fluffy.restore();
// mongodb: { name: 'Fluffy', deleted: false }

Deleted by user

import mongooseDelete, { Deleted, DeletedBy DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts';

type Pet = { name: string } & Deleted & DeletedBy<Types.ObjectId>;
type PetQueryHelpers = DeletedQueryHelpers<Pet>;
type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers> & DeletedByStaticMethods<Pet, Types.ObjectId, PetQueryHelpers>
type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods & DeletedByMethods<Types.ObjectId>> & PetStaticMethods;

const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({
	name: String
});

petSchema.plugin(mongooseDelete, { deletedBy: true });

const PetModel = mongoose.model<Pet, PetModel>('Pet', petSchema);

const fluffy = new PetModel({ name: 'Fluffy' });

await fluffy.save();
// mongodb: { name: 'Fluffy', deleted: false }

const idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");

await fluffy.deleteByUser(idUser);
// mongodb: { name: 'Fluffy', deleted: true, deletedBy: ObjectId("53da93b16b4a6670076b16bf")}

await fluffy.restore();
// mongodb: { name: 'Fluffy', deleted: false }

The type for deletedBy does not have to be ObjectId, you can set a custom type, such as String.

import mongooseDelete, { Deleted, DeletedBy DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts';

type Pet = { name: string } & Deleted & DeletedBy<string>;
type PetQueryHelpers = DeletedQueryHelpers<Pet>;
type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers> & DeletedByStaticMethods<Pet, string, PetQueryHelpers>
type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods & DeletedByMethods<string>> & PetStaticMethods;

const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({
	name: String
});

petSchema.plugin(mongooseDelete, { deletedBy: { type: String } });

const PetModel = mongoose.model<Pet, PetModel>('Pet', petSchema);

const fluffy = new PetModel({ name: 'Fluffy' });

await fluffy.save();
// mongodb: { deleted: false, name: 'Fluffy' }

const userName = 'John Doe';

await fluffy.deleteByUser(userName)
// mongodb: { name: 'Fluffy', deleted: true, deletedBy: 'John Doe' }

await fluffy.restore()

TypeScript support

Document types

| Type | Adds property | --- | --- | Deleted | document.deleted | DeletedAt | document.deletedAt | DeletedBy<TUser> | document.deletedBy

Method types

| Type | Adds method | --- | --- | DeletedMethods | document.delete(), document.restore() | DeletedByMethods<TUser> | document.deleteByUser(user: TUser)

Static types

| Type | Adds static methods | --- | --- | DeletedStaticMethods<DocType> | Model.restoreOne(...), Model.restoreMany(...) | DeletedByStaticMethods<DocType, TUser> | Model.deleteOneByUser(user: TUser, ...), Model.deleteManyByUser(user: TUser, ...)

Query helper types

| Type | Adds query helpers | --- | --- | DeletedQuery<T> | allDocuments(), deletedDocuments()

Bulk delete and restore

// Delete multiple object
PetModel.deleteMany({});
PetModel.deleteMany({ age:10 });
PetModel.deleteManyByUser(idUser, {});
PetModel.deleteManyByUser(idUser, { age: 10 });

// Restore multiple object
PetModel.restoreMany({});
PetModel.restoreMany({ age: 10 });

Method overridden

By default, all standard methods will exclude deleted documents from results, documents that have deleted = true. To override this behavior use allDocuments() or deletedDocuments() query helper functions or simply specify the deleted attribute manually.

| only non deleted documents | only deleted documents | all documents | |----------------------------|-------------------------|-----------------------------| | countDocuments() | countDocuments().deletedDocuments() | countDocuments().allDocuments() | | find() | find().deletedDocuments() | find().allDocuments() | | findById() | findById().deletedDocuments() | findById().allDocuments() | | findOne() | findOne().deletedDocuments() | findOne().allDocuments() | | findOneAndUpdate() | findOneAndUpdate().deletedDocuments() | findOneAndUpdate().allDocuments() | | findByIdAndUpdate() | findByIdAndUpdate().deletedDocuments()| findByIdAndUpdate().allDocuments()| | updateOne() | updateOne({ deleted: true }) | updateOne({ deleted: { $in: [true, false] } }) | | updateMany() | updateMany({ deleted: true }) | updateMany({ deleted: { $in: [true, false] } }) | | aggregate() | aggregate([{ $match: { deleted: true }}]) | aggregate([{ $match: { deleted: { $in: [true, false] } }}]) |

Examples how to override one or multiple methods

// Override all methods (default)
petSchema.plugin(mongooseDelete, { overrideMethods: true });

// Overide only specific methods
petSchema.plugin(mongooseDelete, { overrideMethods: ['countDocuments', 'find', 'findOne'] });

Example of query helpers

// will return only NON DELETED documents
const documents = await PetModel.find();

// will return only DELETED documents
const deletedDocuments = await PetModel.find().deletedDocuments();

// will return ALL documents
const allDocuments = await PetModel.find().allDocuments();

Disable model validation on delete

// By default, validateBeforeDelete is set to true
petSchema.plugin(mongooseDelete);
// the previous line is identical to next line
petSchema.plugin(mongooseDelete, { validateBeforeDelete: true });

// To disable model validation on delete, set validateBeforeDelete option to false
petSchema.plugin(mongooseDelete, { validateBeforeDelete: false });

This is based on existing Mongoose validateBeforeSave option

Create index on fields

// Index only specific fields (default)
petSchema.plugin(mongooseDelete, { indexFields: ['deleted'] });
// or
petSchema.plugin(mongooseDelete, { indexFields: ['deleted', 'deletedAt'] });

// Index all field related to plugin (deleted, deletedAt, deletedBy)
petSchema.plugin(mongooseDelete, { indexFields: true });

Custom field names or schema type definition

type Pet = { name: string; deleted_by?: string; deleted_at?: Date };
type PetQueryHelpers = DeletedQueryHelpers<Pet>;
type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers> & DeletedByStaticMethods<Pet, string, PetQueryHelpers>
type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods & DeletedByMethods<string>> & PetStaticMethods;

const petSchema = new Schema<Pet, PetModel>({
	name: String
});

// Add a custom name for each property, will create alias for the original name (deletedBy/deletedAt)
petSchema.plugin(mongooseDelete, { deletedBy: 'deleted_by', deletedAt: 'deleted_at' });

// Use custom schema type definition by supplying an object
petSchema.plugin(mongooseDelete, { deletedBy: { name: 'deleted_by', type: String }, deletedAt: { name: 'deleted_at' } });

Expects a Mongoose Schema Types object with the added option of name.

License

The MIT License

Copyright (c) 2021 Emil Janitzek https://pixel2.se/

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.