flipbit03 / sqlalchemy-easy-softdelete

Easily add soft-deletion to your SQLAlchemy Models
Other
62 stars 13 forks source link

Feature Request: Allow arbitrary additional fields for soft deleted objects #25

Closed Avishayy closed 1 year ago

Avishayy commented 1 year ago

Having additional fields for soft deleted objects can be helpful to add visibility, for example (very rough prototype):

class SoftDeleteDetailedMixin(
    generate_soft_delete_mixin_class(...)
):
    deleted_at: datetime
    deleted_reason: Optional[str]
    deleted_by: Optional[str]

Then one would be able to do:

product.delete(deleted_reason="Not going to renew stock", deleted_by=admin_user.email)

I might have some time later this month to create a PR by myself, but I still wanted to write it out.

I like the project, thank you for developing it!

flipbit03 commented 1 year ago

Thank you for using it and I'm glad it's useful for you!

On the matter of additional fields - That's a custom pattern of your system - for softdeletion to work, only the deleted_at field is needed to be looped into the library stuff. and keeping it as simple as possible is a Good Goal To Have (TM) 😄

For customizing the mixin, you don't need to edit anything in the library, only subclass the mixin you created with the library with your own extra behavior. Here's how you can achieve that:

class SoftDeleteMixin(
    generate_soft_delete_mixin_class(...)
):
    deleted_at: datetime

class SoftDeleteDetailedMixin(SoftDeleteMixin):
    deleted_reason: Optional[str]
    deleted_by: Optional[str]

    # override delete with your custom logic/additional fields
    def delete(...)

then, just use your SoftDeleteDetailedMixin in all your tables. easy peasy 😉

Please let me know if this wasn't clear enough or if there's anything else I can be of help!

--Cadu

Avishayy commented 1 year ago

Ah, I didn't realize I could overwrite the delete method this way, thank you!