Gathering detailed insights and metrics for soft-delete-mongoose-plugin
Gathering detailed insights and metrics for soft-delete-mongoose-plugin
Gathering detailed insights and metrics for soft-delete-mongoose-plugin
Gathering detailed insights and metrics for soft-delete-mongoose-plugin
A simple and friendly soft delete plugin for mongoose,implementation using TS.
npm install soft-delete-mongoose-plugin
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7 Stars
29 Commits
1 Watching
2 Branches
1 Contributors
Updated on 28 Apr 2023
TypeScript (96.71%)
Shell (2.84%)
JavaScript (0.45%)
Cumulative downloads
Total Downloads
Last day
91.4%
67
Compared to previous day
Last week
71.3%
334
Compared to previous week
Last month
15.4%
1,035
Compared to previous month
Last year
66.4%
7,706
Compared to previous year
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.
$ npm install soft-delete-mongoose-plugin
Use of the SoftDeleteModel type, instead of the Model type.
1import { set, Schema, model, connect, connection, plugin } from 'mongoose'; 2import { SoftDelete, SoftDeleteModel } from 'soft-delete-mongoose-plugin'; 3 4async function main() { 5 set('debug', true); 6 7 await connect('mongodb://localhost:27017/test?directConnection=true'); 8 9 // defind soft delete field name 10 const IS_DELETED_FIELD = 'isDeleted'; 11 const DELETED_AT_FIELD = 'deletedAt'; 12 13 // use soft delete plugin 14 plugin( 15 new SoftDelete({ 16 isDeletedField: IS_DELETED_FIELD, 17 deletedAtField: DELETED_AT_FIELD, 18 }).getPlugin(), 19 ); 20 21 interface ISoftDelete { 22 [IS_DELETED_FIELD]: boolean; 23 [DELETED_AT_FIELD]: Date | null; 24 } 25 26 interface IPerson extends ISoftDelete { 27 name: string; 28 } 29 30 const personSchema = new Schema<IPerson>({ 31 name: { type: String, required: true }, 32 isDeleted: { type: Boolean, default: false }, 33 deletedAt: { type: Date, default: null }, 34 }); 35 36 // use of the SoftDeleteModel type, instead of the Model type. 37 const personModel = model<IPerson, SoftDeleteModel<IPerson>>( 38 'persons', 39 personSchema, 40 ); 41 42 // It's ready to use studentModel to soft delete documents 43 await personModel.softDeleteMany(); 44 45 await connection.close(); 46} 47 48main();
1const { set, Schema, model, connect, connection, plugin } = require('mongoose'); 2const { SoftDelete } = require('soft-delete-mongoose-plugin'); 3 4async function main() { 5 set('debug', true); 6 7 await connect('mongodb://localhost:27017/test?directConnection=true'); 8 9 // defind soft delete field name 10 const IS_DELETED_FIELD = 'isDeleted'; 11 const DELETED_AT_FIELD = 'deletedAt'; 12 13 // use soft delete plugin 14 plugin( 15 new SoftDelete({ 16 isDeletedField: IS_DELETED_FIELD, 17 deletedAtField: DELETED_AT_FIELD, 18 }).getPlugin(), 19 ); 20 21 const personSchema = new Schema({ 22 name: { type: String, required: true }, 23 isDeleted: { type: Boolean, default: false }, 24 deletedAt: { type: Date, default: null }, 25 }); 26 27 // use of the SoftDeleteModel type, instead of the Model type. 28 const personModel = model('persons', personSchema); 29 30 // It's ready to use studentModel to soft delete documents 31 await personModel.softDeleteMany(); 32 33 await connection.close(); 34} 35 36main();
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:
1new SoftDelete({ 2 isDeletedField: 'isDeleted', 3 deletedAtField: 'deletedAt', 4 mongoDBVersion: "5.0.5", 5 override: { aggregate: false }, // not override aggregate method 6});
return <Function> The mongoose plugin function
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.
No vulnerabilities found.
No security vulnerabilities found.