-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update some usage documents * Update the theme color
- Loading branch information
Showing
19 changed files
with
90 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
SQLAlchemy CRUD Plus 内部很多方法都提供了 `commit` 参数,默认值为 `False`,它既不会执行提交操作,也不包含 `flush` | ||
等行为,要想真正写入到数据库,你可以通过以下几种方案 | ||
|
||
## `commit=True` | ||
|
||
这通常适用于手动创建的 [session 生成器](https://fastapi.tiangolo.com/tutorial/sql-databases/?h=get_db#create-a-dependency), | ||
SQLAlchemy CRUD Plus 将在内部自动执行提交 | ||
|
||
```py hl_lines="31" | ||
from fastapi import FastAPI, Depends | ||
|
||
--8<-- "docs/ext/get_db.py" | ||
|
||
app = FastAPI() | ||
|
||
|
||
class CreateIns(BaseModel): | ||
# your pydantic schema | ||
pass | ||
|
||
|
||
@app.post('/api', summary='新增一条数据') | ||
async def create(self, obj: CreateIns, db: AsyncSession = Depends(get_db)) -> None: | ||
await self.create_model(db, obj, commit=True) | ||
``` | ||
|
||
## `begin()` | ||
|
||
适用于自动提交,[这一切都由 sqlalchemy 在内部完成](https://docs.sqlalchemy.org.cn/en/20/orm/session_transaction.html) | ||
,因此,用户无需重复调用 commit 方法 | ||
|
||
```py hl_lines="9" | ||
--8<-- "docs/ext/async_db_session.py" | ||
|
||
|
||
async def create(self, obj: CreateIns) -> None: | ||
async with async_db_session.begin() as db: | ||
await self.create_model(db, obj) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | ||
|
||
async_engine = create_async_engine('数据库连接', future=True) | ||
async_db_session = async_sessionmaker(async_engine, autoflush=False, expire_on_commit=False) | ||
|
||
|
||
async def get_db() -> AsyncSession: | ||
""" | ||
session 生成器 | ||
""" | ||
session = async_db_session() | ||
try: | ||
yield session | ||
except Exception as se: | ||
await session.rollback() | ||
raise se | ||
finally: | ||
await session.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,12 @@ | |
pdm add sqlalchemy-crud-plus | ||
``` | ||
|
||
=== "uv" | ||
|
||
```sh | ||
uv add sqlalchemy-crud-plus | ||
``` | ||
|
||
## 示例 | ||
|
||
=== "api.py" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,9 @@ | |
```sh | ||
pdm add sqlalchemy-crud-plus | ||
``` | ||
|
||
=== "uv" | ||
|
||
```sh | ||
uv add sqlalchemy-crud-plus | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters