https://github.com/flipbit03/sqlalchemy-easy-softdelete
Easily add soft-deletion to your SQLAlchemy Models
https://github.com/flipbit03/sqlalchemy-easy-softdelete
python sqlalchemy
Last synced: 5 months ago
JSON representation
Easily add soft-deletion to your SQLAlchemy Models
- Host: GitHub
- URL: https://github.com/flipbit03/sqlalchemy-easy-softdelete
- Owner: flipbit03
- License: other
- Created: 2022-08-27T23:14:30.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-18T07:40:31.000Z (about 2 years ago)
- Last Synced: 2026-01-14T09:24:50.819Z (6 months ago)
- Topics: python, sqlalchemy
- Language: Python
- Homepage:
- Size: 170 KB
- Stars: 70
- Watchers: 5
- Forks: 14
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# SQLAlchemy Easy Soft-Delete
[](https://pypi.org/project/sqlalchemy-easy-softdelete/)
[](https://pypi.org/project/sqlalchemy-easy-softdelete/)
[](https://pypi.org/project/sqlalchemy-easy-softdelete/)
[](https://pypi.org/project/sqlalchemy-easy-softdelete/)

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:
```py
from sqlalchemy_easy_softdelete.mixin import generate_soft_delete_mixin_class
from sqlalchemy_easy_softdelete.hook import IgnoredTable
from sqlalchemy.orm import declarative_base
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(
# 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 hint for autocomplete IDE support
deleted_at: datetime
# Apply the mixin to your Models
Base = declarative_base()
class Fruit(Base, SoftDeleteMixin):
__tablename__ = "fruit"
id = Column(Integer)
```
### Example Usage:
```py
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.
```py
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](CONTRIBUTING.md) for development setup and guidelines.
### License
* BSD-3-Clause