Skip to main content

Easily add soft-deletion to your SQLAlchemy Models.

Project description

SQLAlchemy Easy Soft-Delete

PyPI - Python Version PyPI - Version PyPI - Types PyPI - Downloads Supported SQLAlchemy Versions

Easily add soft-deletion to your SQLAlchemy Models and automatically filter out soft-deleted objects from your queries and relationships.

This package can generate a tailor-made SQLAlchemy Mixin that can be added to your SQLAlchemy Models, making them contain a field that, when set, will mark the entity as being soft-deleted.

The library also installs a hook which dynamically rewrites all selects which are sent to the database for all tables that implement the soft-delete mixin, providing a seamless experience in both manual queries and model relationship accesses.

Mixin generation is fully customizable and you can choose the field name, its type, and the presence of (soft-)delete/undelete methods.

The default implementation will generate a deleted_at field in your models, of type DateTime(timezone=True), and will also provide a .delete(v: Optional = datetime.utcnow()) and .undelete() methods.

Installation:

pip install sqlalchemy-easy-softdelete

How to use:

from sqlalchemy_easy_softdelete.mixin import generate_soft_delete_mixin_class
from sqlalchemy_easy_softdelete.hook import IgnoredTable
from sqlalchemy.orm import declarative_base, Mapped
from sqlalchemy import Column, Integer
from datetime import datetime

# Create a Class that inherits from our class builder
class SoftDeleteMixin(
    generate_soft_delete_mixin_class(  # type: ignore[misc]
        # This table will be ignored by the hook
        # even if the table has the soft-delete column
        ignored_tables=[IgnoredTable(table_schema="public", name="cars"),]
    )
):
    # type: ignore[misc] is required because the mixin is dynamically generated

    # Type hint for IDE autocomplete and type checker support.
    # Using Mapped[T | None] ensures type checkers understand this is a
    # SQLAlchemy column that supports query operations like .where()
    deleted_at: Mapped[datetime | None]

    # Optional: Add method stubs for delete/undelete for type checker support.
    # The actual implementations are provided by the generated mixin class.
    def delete(self, v: datetime | None = None) -> None:
        super().delete(v)  # type: ignore[misc]

    def undelete(self) -> None:
        super().undelete()  # type: ignore[misc]

# Apply the mixin to your Models
Base = declarative_base()

class Fruit(Base, SoftDeleteMixin):
    __tablename__ = "fruit"
    id = Column(Integer)

Example Usage:

all_active_fruits = session.query(Fruit).all()

This will generate a query with an automatic WHERE fruit.deleted_at IS NULL condition added to it.

all_fruits = session.query(Fruit).execution_options(include_deleted=True).all()

Setting include_deleted=True (attribute name can be customized) in the query disables soft delete for that query.

Contributing

Contributions are welcome! See CONTRIBUTING.md for development setup and guidelines.

License

  • BSD-3-Clause

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_easy_softdelete-0.9.0.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_easy_softdelete-0.9.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_easy_softdelete-0.9.0.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_easy_softdelete-0.9.0.tar.gz
Algorithm Hash digest
SHA256 c458464aaa1ac1ffa7e61e7ec98a0c9961d8fe0b365e07391a7bc17d6511a768
MD5 bfa8c2874a0f39d35a2a964ad1a6497d
BLAKE2b-256 4c592d8b1effe2eec1cb55b3007f14a85d57c8fc2caa48edaf5d03ec41d135d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_easy_softdelete-0.9.0.tar.gz:

Publisher: publish.yml on flipbit03/sqlalchemy-easy-softdelete

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlalchemy_easy_softdelete-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_easy_softdelete-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 934efdfc8b558d9aa7e665940ea5789baf8bf963b4c9a70620e0319fde31faf2
MD5 32cee474ef262a2cfa70bfec1307bf17
BLAKE2b-256 a92538936fcddfaf190c1d49a69bd82d9daa1a4f0ad9dc12f9d3c55f915b0fb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_easy_softdelete-0.9.0-py3-none-any.whl:

Publisher: publish.yml on flipbit03/sqlalchemy-easy-softdelete

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page