UNPKG

mongoose-smart-delete

Version:

A Mongoose plugin for implementing soft delete functionality, allowing documents to be marked as deleted without being removed from the database.

44 lines (37 loc) 767 B
const mongoose = require('mongoose') module.exports = function (schema, config) { const deletedField = { type: Boolean, index: true } if (config.mode === 'strict') { deletedField.default = false } schema.add({ [config.deleted.field]: deletedField }) if (config.deletedAt) { schema.add({ [config.deletedAt.field]: { type: Date } }) } if (config.deletedBy) { schema.add({ [config.deletedBy.field]: { type: mongoose.Schema.Types.ObjectId, ref: config.deletedBy.ref, index: true } }) } if (config.deletionId) { schema.add({ [config.deletionId.field]: { type: mongoose.Schema.Types.ObjectId, index: true } }) } }