mongoose-deleted
v1.5.0
Published
a soft-delete implementation utilizing mongoose middleware
Downloads
138
Maintainers
Readme
mongoose-deleted
a soft-delete implementation utilizing mongoose middleware
usage
var mongoose = require('mongoose');
var mongoose_deleted = require('mongoose-deleted');
var user = new mongoose.Schema({ name: String });
mongoose_deleted(user);
user = mongoose.model('user', user);
var name = "John Q Public";
var user1 = new user({ name : name });
user1.save(function() {
user.findOne({ name : name }, function(err, doc) {
if (err || !doc) console.log('failed to find document');
doc.delete(function(err) {
user.findOne({ name: name }, function() {
if (!doc) console.log('soft delete worked');
})
});
});
});
documents
mongoose-deleted
utilizes mongoose middleware to transparently modify queries to select for documents that are not { deleted: true }
. Documents that are .delete()
-ed will not be returned. To explicitly return documents that are deleted:
schema.find({ deleted: true }, function(err, docs) {
// ...
});
Additionally, the deleted
boolean property is set by default to not be selected/returned on a document.
To have deleted
normally returned:
schema.plugin(mongoose_deleted, { select : true });
To have the deleted
property included, in addition to the normal properties:
schema.findOne(query).select('+deleted').exec(function(err, doc) {
console.log(doc.deleted);
});
Or, to retrieve the deleted
property only on a particular query, manually select for it:
schema.findOne({}, { deleted : 1 }, function(err, doc) {
console.log(doc.deleted);
});
toJSON
By default, mongoose-deleted
hides the deleted
property on doc.toJSON()
. This is configurable in the options:
schema.plugin(mongoose_deleted, { toJSON : true });
This can be overriden in a toJSON()
call:
var json = doc.toJSON({ deleted : true });
history
mongoose-deleted
allows an optional integration with mongoose-history-log
by passing in the options:
mongoose_deleted(schema, { history: true });
This will automatically insert a { status: 'deleted' }
object with the current time.