Gathering detailed insights and metrics for @npmteam2024/praesentium-itaque-inventore
Gathering detailed insights and metrics for @npmteam2024/praesentium-itaque-inventore
Gathering detailed insights and metrics for @npmteam2024/praesentium-itaque-inventore
Gathering detailed insights and metrics for @npmteam2024/praesentium-itaque-inventore
npm install @npmteam2024/praesentium-itaque-inventore
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
32
@npmteam2024/praesentium-itaque-inventore is simple and lightweight plugin that enables soft deletion of documents in MongoDB. This code is based on riyadhalnur's plugin mongoose-softdelete.
$ne
operator using {use$neOperator: false}
. Before you start to use this option please check #50.{ withDeleted: true }
)Install using npm
npm install @npmteam2024/praesentium-itaque-inventore
The plugin currently does not have its own type definition. Please be free to use @types/@npmteam2024/praesentium-itaque-inventore.
In doing so, you should make use of the SoftDeleteModel
type, instead of the Model
type.
1import { Schema, model, connect } from 'mongoose'; 2import { SoftDeleteModel }, MongooseDelete from '@npmteam2024/praesentium-itaque-inventore'; 3 4interface Pet extends SoftDeleteDocument { 5 name: string; 6} 7 8const PetSchema = new Schema<Pet>({ 9 name: String 10}); 11 12PetSchema.plugin(MongooseDelete, { deletedBy: true, deletedByType: String }); 13 14const model: SoftDeleteModel = model<Pet>('Pet', PetSchema); 15 16export default model;
We can use this plugin with or without options.
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String 5}); 6 7PetSchema.plugin(mongoose_delete); 8 9var Pet = mongoose.model('Pet', PetSchema); 10 11var fluffy = new Pet({ name: 'Fluffy' }); 12 13fluffy.save(function () { 14 // mongodb: { deleted: false, name: 'Fluffy' } 15 16 // note: you should invoke exactly delete() method instead of standard fluffy.remove() 17 fluffy.delete(function () { 18 // mongodb: { deleted: true, name: 'Fluffy' } 19 20 fluffy.restore(function () { 21 // mongodb: { deleted: false, name: 'Fluffy' } 22 }); 23 }); 24 25}); 26 27var examplePetId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); 28 29// INFO: Example usage of deleteById static method 30Pet.deleteById(examplePetId, function (err, petDocument) { 31 // mongodb: { deleted: true, name: 'Fluffy', _id: '53da93b1...' } 32}); 33
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String 5}); 6 7PetSchema.plugin(mongoose_delete, { deletedAt : true }); 8 9var Pet = mongoose.model('Pet', PetSchema); 10 11var fluffy = new Pet({ name: 'Fluffy' }); 12 13fluffy.save(function () { 14 // mongodb: { deleted: false, name: 'Fluffy' } 15 16 // note: you should invoke exactly delete() method instead of standard fluffy.remove() 17 fluffy.delete(function () { 18 // mongodb: { deleted: true, name: 'Fluffy', deletedAt: ISODate("2014-08-01T10:34:53.171Z")} 19 20 fluffy.restore(function () { 21 // mongodb: { deleted: false, name: 'Fluffy' } 22 }); 23 }); 24 25});
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String 5}); 6 7PetSchema.plugin(mongoose_delete, { deletedBy : true }); 8 9var Pet = mongoose.model('Pet', PetSchema); 10 11var fluffy = new Pet({ name: 'Fluffy' }); 12 13fluffy.save(function () { 14 // mongodb: { deleted: false, name: 'Fluffy' } 15 16 var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); 17 18 // note: you should invoke exactly delete() method instead of standard fluffy.remove() 19 fluffy.delete(idUser, function () { 20 // mongodb: { deleted: true, name: 'Fluffy', deletedBy: ObjectId("53da93b16b4a6670076b16bf")} 21 22 fluffy.restore(function () { 23 // mongodb: { deleted: false, name: 'Fluffy' } 24 }); 25 }); 26 27});
The type for deletedBy
does not have to be ObjectId
, you can set a custom type, such as String
.
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String 5}); 6 7PetSchema.plugin(mongoose_delete, { deletedBy: true, deletedByType: String }); 8 9var Pet = mongoose.model('Pet', PetSchema); 10 11var fluffy = new Pet({ name: 'Fluffy' }); 12 13fluffy.save(function () { 14 // mongodb: { deleted: false, name: 'Fluffy' } 15 16 var idUser = "my-custom-user-id"; 17 18 // note: you should invoke exactly delete() method instead of standard fluffy.remove() 19 fluffy.delete(idUser, function () { 20 // mongodb: { deleted: true, name: 'Fluffy', deletedBy: 'my-custom-user-id' } 21 22 fluffy.restore(function () { 23 // mongodb: { deleted: false, name: 'Fluffy' } 24 }); 25 }); 26});
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String, 5 age: Number 6}); 7 8PetSchema.plugin(mongoose_delete); 9 10var Pet = mongoose.model('Pet', PetSchema); 11 12var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); 13 14// Delete multiple object, callback 15Pet.delete(function (err, result) { ... }); 16Pet.delete({age:10}, function (err, result) { ... }); 17Pet.delete({}, idUser, function (err, result) { ... }); 18Pet.delete({age:10}, idUser, function (err, result) { ... }); 19 20// Delete multiple object, promise 21Pet.delete().exec(function (err, result) { ... }); 22Pet.delete({age:10}).exec(function (err, result) { ... }); 23Pet.delete({}, idUser).exec(function (err, result) { ... }); 24Pet.delete({age:10}, idUser).exec(function (err, result) { ... }); 25 26// Restore multiple object, callback 27Pet.restore(function (err, result) { ... }); 28Pet.restore({age:10}, function (err, result) { ... }); 29 30// Restore multiple object, promise 31Pet.restore().exec(function (err, result) { ... }); 32Pet.restore({age:10}).exec(function (err, result) { ... });
We have the option to override all standard methods or only specific methods. Overridden methods will exclude deleted documents from results, documents that have deleted = true
. Every overridden method will have two additional methods, so we will be able to work with deleted documents.
only not deleted documents | only deleted documents | all documents |
---|---|---|
count() | countDeleted | countWithDeleted |
countDocuments() | countDocumentsDeleted | countDocumentsWithDeleted |
find() | findDeleted | findWithDeleted |
findOne() | findOneDeleted | findOneWithDeleted |
findOneAndUpdate() | findOneAndUpdateDeleted | findOneAndUpdateWithDeleted |
update() | updateDeleted | updateWithDeleted |
updateOne() | updateOneDeleted | updateOneWithDeleted |
updateMany() | updateManyDeleted | updateManyWithDeleted |
aggregate() | aggregateDeleted | aggregateWithDeleted |
findById() | Please use findOne | Please use findOneWithDeleted |
findByIdAndUpdate() | Please use findOneAndUpdateDeleted | Please use findOneAndUpdateWithDeleted |
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String 5}); 6 7// Override all methods 8PetSchema.plugin(mongoose_delete, { overrideMethods: 'all' }); 9// or 10PetSchema.plugin(mongoose_delete, { overrideMethods: true }); 11 12// Overide only specific methods 13PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find', 'findOne', 'findOneAndUpdate', 'update'] }); 14// or 15PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'countDocuments', 'find'] }); 16// or (unrecognized method names will be ignored) 17PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find', 'errorXyz'] }); 18 19 20var Pet = mongoose.model('Pet', PetSchema); 21 22// Example of usage overridden methods 23 24Pet.find(function (err, documents) { 25 // will return only NOT DELETED documents 26}); 27 28Pet.findDeleted(function (err, documents) { 29 // will return only DELETED documents 30}); 31 32Pet.findWithDeleted(function (err, documents) { 33 // will return ALL documents 34}); 35
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: { type: String, required: true } 5}); 6 7// By default, validateBeforeDelete is set to true 8PetSchema.plugin(mongoose_delete); 9// the previous line is identical to next line 10PetSchema.plugin(mongoose_delete, { validateBeforeDelete: true }); 11 12// To disable model validation on delete, set validateBeforeDelete option to false 13PetSchema.plugin(mongoose_delete, { validateBeforeDelete: false }); 14 15// NOTE: This is based on existing Mongoose validateBeforeSave option 16// http://mongoosejs.com/docs/guide.html#validateBeforeSave 17
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: { type: String, required: true } 5}); 6 7// By default, validateBeforeRestore is set to true 8PetSchema.plugin(mongoose_delete); 9// the previous line is identical to next line 10PetSchema.plugin(mongoose_delete, { validateBeforeRestore: true }); 11 12// To disable model validation on restore, set validateBeforeRestore option to false 13PetSchema.plugin(mongoose_delete, { validateBeforeRestore: false }); 14 15// NOTE: This is based on existing Mongoose validateBeforeSave option 16// http://mongoosejs.com/docs/guide.html#validateBeforeSave
1var mongoose_delete = require('@npmteam2024/praesentium-itaque-inventore'); 2 3var PetSchema = new Schema({ 4 name: String 5}); 6 7// Index all field related to plugin (deleted, deletedAt, deletedBy) 8PetSchema.plugin(mongoose_delete, { indexFields: 'all' }); 9// or 10PetSchema.plugin(mongoose_delete, { indexFields: true }); 11 12// Index only specific fields 13PetSchema.plugin(mongoose_delete, { indexFields: ['deleted', 'deletedBy'] }); 14// or 15PetSchema.plugin(mongoose_delete, { indexFields: ['deletedAt'] }); 16 17
The MIT License
Copyright (c) 2014 Sanel Deljkic http://dsanel.github.io/
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.