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.0、sqlalchemy>=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
运行测试
- 在环境变量或
.env中设置可访问的数据库 URI:TEST_DB=sqlite:///./test.db(或其他驱动)。 - 安装测试依赖后执行:
pytest -q
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c2da55e0c84442719a17ecbbdda5bea413432a0be6dfb70a5d32ee1a97103a8
|
|
| MD5 |
83f787acba4d259e3556c18956f9d504
|
|
| BLAKE2b-256 |
0d85a6becee262fb1b6924cec90efd47a2917d9b7bf93aa685e76495c2bd36ec
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_sqlalchemy_crud-0.0.3.tar.gz -
Subject digest:
5c2da55e0c84442719a17ecbbdda5bea413432a0be6dfb70a5d32ee1a97103a8 - Sigstore transparency entry: 747841011
- Sigstore integration time:
-
Permalink:
ZM-Kimu/flask_sqlalchemy_crud@00404c981a1a15b05c55b649a233e3eea41825a0 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/ZM-Kimu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@00404c981a1a15b05c55b649a233e3eea41825a0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file flask_sqlalchemy_crud-0.0.3-py3-none-any.whl.
File metadata
- Download URL: flask_sqlalchemy_crud-0.0.3-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f41aaaeb9ac12c0a6912125fcf437a431b279b38493c2cf6dbd337fe1dd6aeb8
|
|
| MD5 |
a83060ea117758ad04d5308bd89bef2e
|
|
| BLAKE2b-256 |
3157dbb92694c52f29b0d9cf56258434a5da6779b287fb80dfbbd8483c51c6f4
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_sqlalchemy_crud-0.0.3-py3-none-any.whl -
Subject digest:
f41aaaeb9ac12c0a6912125fcf437a431b279b38493c2cf6dbd337fe1dd6aeb8 - Sigstore transparency entry: 747841023
- Sigstore integration time:
-
Permalink:
ZM-Kimu/flask_sqlalchemy_crud@00404c981a1a15b05c55b649a233e3eea41825a0 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/ZM-Kimu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@00404c981a1a15b05c55b649a233e3eea41825a0 -
Trigger Event:
push
-
Statement type: