mongoose-soft-delete icon indicating copy to clipboard operation
mongoose-soft-delete copied to clipboard

Minor fixes

Open Paradoxu opened this issue 1 year ago • 0 comments

Basic changes

  • Added comments for better documentation of the SoftDeleteModel interface
  • Added softDeleteById and restoreById for easier manipulation of single documents
  • Replaced Record<...> with mongoose.FilterQuery<...> for better self code documentation and linting
  • Errors should be handled by the callee, which is why I have added an optional onFail parameter so that the callee can decide how to handle errors.

Removed find and save

The previous code would use a query to find all documents that matches the given query and loop through each document, either delete/restore them and call save, this operation can be slow specially when running a query against many documents. For better performance and code readability I have added a simple updateMany for both the restore and softDelete methods, the return value will still be the number of modifiedDocuments by the query.

Exec

The previous code would not call exec when building a query a rellying entiry on the async/await which worked, but for safety reasons it's best to call exec when we actually intend to run the query against the database.

Context

These changes have been made to improve performance, code readability, and maintainability.

Paradoxu avatar May 29 '23 13:05 Paradoxu