Skip to main content

Use SQLAlchemy in a Pythonic way.

Project description

sqlalchemy-crud-tx

English | 中文

A lightweight CRUD + transaction helper for SQLAlchemy (Flask glue can be added via extensions):

  • Context-managed CRUD with nested savepoints: with CRUD(Model) as crud:
  • Function-level transactions via @CRUD.transaction() with join semantics
  • Configurable error policy (error_policy="raise"|"status_only") and pluggable logger
  • Type-friendly CRUDQuery wrapper for common chainable operations

Install

pip install sqlalchemy-crud-tx
# with Flask integration
pip install "sqlalchemy-crud-tx[flask]"
# or local editable install
pip install -e .

Requires Python 3.11+ with sqlalchemy>=1.4 (optional Flask integration can be added separately).

Quick Start (pure SQLAlchemy)

from sqlalchemy import String, Integer, create_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, sessionmaker
from sqlalchemy_crud_tx import CRUD

engine = create_engine("sqlite:///./crud_example.db", echo=False)
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "example_user"
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)


Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

CRUD.configure(session_provider=SessionLocal, error_policy="raise")

with CRUD(User) as crud:
    user = crud.add(email="demo@example.com")
    print("created", user)

with CRUD(User, email="demo@example.com") as crud:
    row = crud.first()
    print("fetched", row)

with CRUD(User) as crud:
    crud.delete(row)
# or
with CRUD(User, email="demo@example.com") as crud:
    crud.delete()

Function-Level Transactions

from sqlalchemy_crud_tx import CRUD

CRUD.configure(session_provider=SessionLocal, error_policy="raise")

@CRUD.transaction(error_policy="raise")
def create_two_users():
    with CRUD(User) as crud1:
        crud1.add(email="a@example.com")
    with CRUD(User) as crud2:
        crud2.add(email="b@example.com")

create_two_users()
  • The outermost call commits or rolls back; inner CRUD contexts only mark status when exceptions occur.
  • With error_policy="status_only", SQLAlchemyError is rolled back and caught; check crud.status / crud.error instead.

Docs & Examples

  • Full example: docs/examples/basic_crud.py
  • Transaction refactor notes/TODO: docs/crud_refactor_todo.md
  • Typing directions: docs/todo.md

Testing

  1. Provide a DB URI via env or .env: TEST_DB=sqlite:///./test.db (or another driver).
  2. Install test deps, then:
    pytest -q
    

Notes

  • SQLAlchemy-first; optional Flask integration can be layered via extensions.
  • Always call CRUD.configure(session_provider=...) before using CRUD instances.
  • If a Session may already be in a transaction (e.g., AUTOBEGIN after expire_on_commit), set existing_txn_policy in CRUD.configure(...) to control how CRUD behaves (error, join, savepoint, adopt_autobegin, reset).

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_crud_tx-1.0.0.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_crud_tx-1.0.0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_crud_tx-1.0.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_crud_tx-1.0.0.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlalchemy_crud_tx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ee6a26ab160e78ceb894beee2768de4b967c3cb1def06a0170fe8385fb4a1ab1
MD5 a29da1f68c91f34b49ae331de0c65d68
BLAKE2b-256 81a52021478d276bd24e268dc3f0bf97aacd6c77d45d36bc035f7b6ed88d622c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_crud_tx-1.0.0.tar.gz:

Publisher: publish-pypi.yml on ZM-Kimu/sqlalchemy-crud-tx

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_crud_tx-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_crud_tx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb686661f1cce4d985186395b5bc40b086f82f1bfee64cbcd57cb63abf4ced0a
MD5 f3b17f530a1aeb7268d44828716b3516
BLAKE2b-256 9bab5721a8fa8dd8085e4c436ae05c18f4d7477be2182a4b678f3a092bd1d2f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_crud_tx-1.0.0-py3-none-any.whl:

Publisher: publish-pypi.yml on ZM-Kimu/sqlalchemy-crud-tx

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