Skip to main content

A simple soft delete implementation for sqlalchemy

Project description

Suave Deletes

Suave Deletes is a Python library that provides a soft delete functionality for SQLAlchemy models.

Soft deletes allow you to mark records as deleted without actually removing them from the database, making it easy to recover or audit data as needed.

Features

  • Soft delete: Mark records as deleted without permanently removing them from the database.
  • Automatic Query Filtering: Automatically filter out soft-deleted records from your queries.
  • Customizable: Configure which models and fields use soft delete functionality.
  • Simple Integration: Easily integrate with your existing SQLAlchemy models.

Installation

You can install Suave Deletes using pip:
pip install suave_deletes

Usage

To use Suave Deletes, simply import and apply it to your SQLAlchemy models.
Here is a basic example:

Configuration

from sqlalchemy import create_engine
from suave_deletes.session import create_suave_delete_session

def create_session():
    engine = create_engine(
        "Your database URI"
    )

    current_session = create_suave_delete_session(engine)

    with current_session() as session:
        yield session

Implementation

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from suave_deletes.mixins import SuaveDeleteMixin

DeclarativeBase = declarative_base()
session = create_session()

class Base(DeclarativeBase, SuaveDeleteMixin):
    deleted_at = mapped_column(DateTime, default=None)


class MyModel(Base):
    __tablename__ = 'my_model'
    id = Column(Integer, primary_key=True)
    name = Column(String)

# Creates instance 
obj = MyModel(name="example")
session.add(obj)
session.commit()

# Soft delete the instance
session.delete(obj)
session.commit()

# Query only non-deleted records
non_deleted_records = session.query(MyModel).all()

# Query all records, including soft deleted ones 
all_records = session.query(MyModel).with_deleted_at().all()

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you would like to contribute code, please fork the repository and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Thanks to the SQLAlchemy community for their excellent library and documentation.

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

suave_deletes-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

suave_deletes-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file suave_deletes-0.1.0.tar.gz.

File metadata

  • Download URL: suave_deletes-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for suave_deletes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b70f8bf74ec4d498b6509d90d9b96bd94eb0f60e8c35b0f464d9235136282749
MD5 eccdaaec50086daa3011d80568272f77
BLAKE2b-256 7f6d163337f4086dd00b1a1823c0b8dcff475b8ee52b604dd32f8afa70b9530c

See more details on using hashes here.

Provenance

File details

Details for the file suave_deletes-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for suave_deletes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bb465a70140606237546b3856390d17671d380d07e58d7f697e23bb1670f131
MD5 9ce41d15d88cb4659f0f4afa5e6c4d28
BLAKE2b-256 cad15a709018da7e7452e098e98f61ab47ff5113e8b8cd47677f3000d174ce2b

See more details on using hashes here.

Provenance

Supported by

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