Easily add soft-deletion to your SQLAlchemy Models.
Project description
SQLAlchemy Easy Soft-Delete
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.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()):
# 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:
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.
License
- BSD-3-Clause
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_easy_softdelete-0.7.0.tar.gz.
File metadata
- Download URL: sqlalchemy_easy_softdelete-0.7.0.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10ad117c30255409e9b9696c98e5c9b9c13f8c28ba1f6293bba3123b8afa2a7a
|
|
| MD5 |
53698093989de5c3b7276238f43f1332
|
|
| BLAKE2b-256 |
40997dc7170e29b64921a627dd79abc68bf5aa5b63ed4460fb7178a7e1a64f7d
|
File details
Details for the file sqlalchemy_easy_softdelete-0.7.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_easy_softdelete-0.7.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e311e8464419bee347bbe473509c90a2d9e3b014e40b35cf2ac803078e7a531f
|
|
| MD5 |
11c25f62f535634124cfa033d737f7a3
|
|
| BLAKE2b-256 |
31557c65713b79e713c2913bfc1da427fd45a3e7a4d1f0775145327851a13444
|