Gathering detailed insights and metrics for mongoose-delete-ts
Gathering detailed insights and metrics for mongoose-delete-ts
Gathering detailed insights and metrics for mongoose-delete-ts
Gathering detailed insights and metrics for mongoose-delete-ts
npm install mongoose-delete-ts
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
10 Stars
141 Commits
4 Forks
1 Watching
1 Branches
3 Contributors
Updated on 16 Sept 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-13.3%
39
Compared to previous day
Last week
-3.4%
230
Compared to previous week
Last month
-15.6%
1,274
Compared to previous month
Last year
26.9%
18,523
Compared to previous year
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.
Install using npm
npm install mongoose-delete-ts
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
We can use this plugin with or without options.
1import mongooseDelete, { Deleted, DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts'; 2 3type Pet = { name: string } & Deleted; 4type PetQueryHelpers = DeletedQueryHelpers<Pet>; 5type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers>; 6type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods> & PetStaticMethods; 7 8const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({ 9 name: String 10}); 11 12petSchema.plugin(mongooseDelete); 13 14const PetPetModel = mongoose.model<PetModel>('Pet', petSchema); 15 16const fluffy = new PetModel({ name: 'Fluffy' }); 17 18await fluffy.save(); 19// mongodb: { name: 'Fluffy', deleted: false } 20await fluffy.delete(); 21// mongodb: { name: 'Fluffy', deleted: true } 22await fluffy.restore(); 23// mongodb: { name: 'Fluffy', deleted: false } 24 25const examplePetId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); 26 27const petDocument = await PetModel.deleteById(examplePetId); 28// mongodb: { _id: '53da93b1...', name: 'Fluffy', deleted: true }
1import mongooseDelete, { Deleted, DeletedAt DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts'; 2 3type Pet = { name: string } & Deleted & DeletedAt; 4type PetQueryHelpers = DeletedQueryHelpers<Pet>; 5type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers>; 6type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods> & PetStaticMethods; 7 8const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({ 9 name: String 10}); 11 12petSchema.plugin(mongooseDelete, { deletedAt: true }); 13 14const PetModel = mongoose.model<Pet, PetModel>('Pet', petSchema); 15 16const fluffy = new PetModel({ name: 'Fluffy' }); 17 18await fluffy.save(); 19// mongodb: { name: 'Fluffy', deleted: false } 20 21await fluffy.delete(); 22// mongodb: { name: 'Fluffy', deleted: true, deletedAt: ISODate("2014-08-01T10:34:53.171Z")} 23 24await fluffy.restore(); 25// mongodb: { name: 'Fluffy', deleted: false }
1import mongooseDelete, { Deleted, DeletedBy DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts'; 2 3type Pet = { name: string } & Deleted & DeletedBy<Types.ObjectId>; 4type PetQueryHelpers = DeletedQueryHelpers<Pet>; 5type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers> & DeletedByStaticMethods<Pet, Types.ObjectId, PetQueryHelpers> 6type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods & DeletedByMethods<Types.ObjectId>> & PetStaticMethods; 7 8const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({ 9 name: String 10}); 11 12petSchema.plugin(mongooseDelete, { deletedBy: true }); 13 14const PetModel = mongoose.model<Pet, PetModel>('Pet', petSchema); 15 16const fluffy = new PetModel({ name: 'Fluffy' }); 17 18await fluffy.save(); 19// mongodb: { name: 'Fluffy', deleted: false } 20 21const idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); 22 23await fluffy.deleteByUser(idUser); 24// mongodb: { name: 'Fluffy', deleted: true, deletedBy: ObjectId("53da93b16b4a6670076b16bf")} 25 26await fluffy.restore(); 27// mongodb: { name: 'Fluffy', deleted: false }
The type for deletedBy
does not have to be ObjectId
, you can set a custom type, such as String
.
1import mongooseDelete, { Deleted, DeletedBy DeletedQueryHelpers, DeletedMethods, DeletedStaticMethods } from 'mongoose-delete-ts'; 2 3type Pet = { name: string } & Deleted & DeletedBy<string>; 4type PetQueryHelpers = DeletedQueryHelpers<Pet>; 5type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers> & DeletedByStaticMethods<Pet, string, PetQueryHelpers> 6type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods & DeletedByMethods<string>> & PetStaticMethods; 7 8const petSchema = new Schema<Pet, PetModel, DeletedMethods, PetQueryHelpers, {}, PetStaticMethods>({ 9 name: String 10}); 11 12petSchema.plugin(mongooseDelete, { deletedBy: { type: String } }); 13 14const PetModel = mongoose.model<Pet, PetModel>('Pet', petSchema); 15 16const fluffy = new PetModel({ name: 'Fluffy' }); 17 18await fluffy.save(); 19// mongodb: { deleted: false, name: 'Fluffy' } 20 21const userName = 'John Doe'; 22 23await fluffy.deleteByUser(userName) 24// mongodb: { name: 'Fluffy', deleted: true, deletedBy: 'John Doe' } 25 26await fluffy.restore()
Type | Adds property |
---|---|
Deleted | document.deleted |
DeletedAt | document.deletedAt |
DeletedBy<TUser> | document.deletedBy |
Type | Adds method |
---|---|
DeletedMethods | document.delete() , document.restore() |
DeletedByMethods<TUser> | document.deleteByUser(user: TUser) |
Type | Adds static methods |
---|---|
DeletedStaticMethods<DocType> | Model.restoreOne(...) , Model.restoreMany(...) |
DeletedByStaticMethods<DocType, TUser> | Model.deleteOneByUser(user: TUser, ...) , Model.deleteManyByUser(user: TUser, ...) |
Type | Adds query helpers |
---|---|
DeletedQuery<T> | allDocuments() , deletedDocuments() |
1// Delete multiple object 2PetModel.deleteMany({}); 3PetModel.deleteMany({ age:10 }); 4PetModel.deleteManyByUser(idUser, {}); 5PetModel.deleteManyByUser(idUser, { age: 10 }); 6 7// Restore multiple object 8PetModel.restoreMany({}); 9PetModel.restoreMany({ age: 10 });
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] } }}]) |
1// Override all methods (default) 2petSchema.plugin(mongooseDelete, { overrideMethods: true }); 3 4// Overide only specific methods 5petSchema.plugin(mongooseDelete, { overrideMethods: ['countDocuments', 'find', 'findOne'] });
1// will return only NON DELETED documents 2const documents = await PetModel.find(); 3 4// will return only DELETED documents 5const deletedDocuments = await PetModel.find().deletedDocuments(); 6 7// will return ALL documents 8const allDocuments = await PetModel.find().allDocuments();
1// By default, validateBeforeDelete is set to true 2petSchema.plugin(mongooseDelete); 3// the previous line is identical to next line 4petSchema.plugin(mongooseDelete, { validateBeforeDelete: true }); 5 6// To disable model validation on delete, set validateBeforeDelete option to false 7petSchema.plugin(mongooseDelete, { validateBeforeDelete: false });
This is based on existing Mongoose validateBeforeSave option
1// Index only specific fields (default) 2petSchema.plugin(mongooseDelete, { indexFields: ['deleted'] }); 3// or 4petSchema.plugin(mongooseDelete, { indexFields: ['deleted', 'deletedAt'] }); 5 6// Index all field related to plugin (deleted, deletedAt, deletedBy) 7petSchema.plugin(mongooseDelete, { indexFields: true });
1type Pet = { name: string; deleted_by?: string; deleted_at?: Date }; 2type PetQueryHelpers = DeletedQueryHelpers<Pet>; 3type PetStaticMethods = DeletedStaticMethods<Pet, PetQueryHelpers> & DeletedByStaticMethods<Pet, string, PetQueryHelpers> 4type PetModel = Model<Pet, PetQueryHelpers, DeletedMethods & DeletedByMethods<string>> & PetStaticMethods; 5 6const petSchema = new Schema<Pet, PetModel>({ 7 name: String 8}); 9 10// Add a custom name for each property, will create alias for the original name (deletedBy/deletedAt) 11petSchema.plugin(mongooseDelete, { deletedBy: 'deleted_by', deletedAt: 'deleted_at' }); 12 13// Use custom schema type definition by supplying an object 14petSchema.plugin(mongooseDelete, { deletedBy: { name: 'deleted_by', type: String }, deletedAt: { name: 'deleted_at' } });
Expects a Mongoose Schema Types object with the added option of name
.
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.
No vulnerabilities found.
No security vulnerabilities found.