Skip to main content

Use SQLAlchemy in a Pythonic way.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

flask-sqlalchemy-crud

一个针对 Flask-SQLAlchemy 的轻量级 CRUD/事务辅助库,提供:

  • 上下文管理器式的 CRUD 读写与子事务(with CRUD(Model) as crud:
  • 函数级事务装饰器(@CRUD.transaction())支持 join 语义
  • 可配置的错误策略(error_policy="raise"|"status")与日志接口
  • 类型友好的 CRUDQuery 链式包装

仓库仍在重构阶段,API 可能会有改动。

安装

pip install -e .

需要 Python 3.10+ 且已安装 flask-sqlalchemy>=3.0sqlalchemy>=1.4

快速开始

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import Mapped, mapped_column
from flask_sqlalchemy_crud import CRUD

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///./crud_example.db"
db = SQLAlchemy(app)

class User(db.Model):  # type: ignore[misc]
    __tablename__ = "example_user"
    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str] = mapped_column(db.String(255), unique=True, nullable=False)

with app.app_context():
    db.drop_all()
    db.create_all()
    CRUD.configure(session=db.session)

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

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

函数级事务示例

from flask_sqlalchemy_crud import CRUD

@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()
  • 最外层事务负责提交/回滚;内层 CRUD 上下文在遇到异常时仅标记状态,最终由装饰器处理。
  • error_policy="status" 会在回滚后吞掉 SQLAlchemyError,由调用方检查 crud.status / crud.error

示例与文档

  • 完整示例:docs/examples/basic_crud.py
  • 事务重构设计与 TODO:docs/crud_refactor_todo.md
  • 类型增强方向:docs/todo.md

运行测试

  1. 在环境变量或 .env 中设置可访问的数据库 URI:TEST_DB=sqlite:///./test.db(或其他驱动)。
  2. 安装测试依赖后执行:
    pytest -q
    

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

flask_sqlalchemy_crud-0.0.3.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

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

flask_sqlalchemy_crud-0.0.3-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file flask_sqlalchemy_crud-0.0.3.tar.gz.

File metadata

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

File hashes

Hashes for flask_sqlalchemy_crud-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5c2da55e0c84442719a17ecbbdda5bea413432a0be6dfb70a5d32ee1a97103a8
MD5 83f787acba4d259e3556c18956f9d504
BLAKE2b-256 0d85a6becee262fb1b6924cec90efd47a2917d9b7bf93aa685e76495c2bd36ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_sqlalchemy_crud-0.0.3.tar.gz:

Publisher: publish-pypi.yml on ZM-Kimu/flask_sqlalchemy_crud

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_sqlalchemy_crud-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_sqlalchemy_crud-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f41aaaeb9ac12c0a6912125fcf437a431b279b38493c2cf6dbd337fe1dd6aeb8
MD5 a83060ea117758ad04d5308bd89bef2e
BLAKE2b-256 3157dbb92694c52f29b0d9cf56258434a5da6779b287fb80dfbbd8483c51c6f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_sqlalchemy_crud-0.0.3-py3-none-any.whl:

Publisher: publish-pypi.yml on ZM-Kimu/flask_sqlalchemy_crud

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