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_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:
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
File details
Details for the file sqlalchemy_easy_softdelete-0.8.3.tar.gz
.
File metadata
- Download URL: sqlalchemy_easy_softdelete-0.8.3.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 827c7fcb3949eb1cb1f9addf2f40a7cfc45571815dde4f8855061df09305b7d2 |
|
MD5 | 518ce9a5a79e5ad3a7fcf1cb268ca5b0 |
|
BLAKE2b-256 | eb3f4df43ba6182db71dcbed8d7451ce4ad24d56fcb67eb703c4b68ba429e665 |
File details
Details for the file sqlalchemy_easy_softdelete-0.8.3-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_easy_softdelete-0.8.3-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d0ae4a6bdffc082ad11618c27086a07eaa2ff9c86a5ee8899f47f8d2daa4efe |
|
MD5 | 45cf9b2e2ea6b398fdc5f97b6696fdfe |
|
BLAKE2b-256 | 6308339100252ae79b5dd7497c91175f3ecd1577d654b8e803e05f87633e700f |