Skip to content

Commit 2cc0fed

Browse files
authored
Add the flush usage (#32)
1 parent a875e3d commit 2cc0fed

File tree

11 files changed

+57
-15
lines changed

11 files changed

+57
-15
lines changed

docs/advanced/filter.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ items = await item_crud.select_models(
116116
`or` 过滤器的高级用法,每个键都应是库已支持的过滤器,仅允许字典
117117

118118
```python title="__mor"
119-
# 获取年龄等于 30 岁和 40 岁的员工
119+
# 获取年龄等于 30 岁或 40 岁的员工
120120
items = await item_crud.select_models(
121121
session=db,
122122
age__mor={'eq': [30, 40]}, # (1)
@@ -133,7 +133,7 @@ items = await item_crud.select_models(
133133
`or` 过滤器的更高级用法,每个值都应是一个已受支持的条件过滤器,它应该是一个数组
134134

135135
```python title="__gor__"
136-
# 获取年龄在 30 - 40 岁之间且薪资大于 20k 的员工
136+
# 获取年龄在 30 - 40 岁之间或薪资大于 20k 的员工
137137
items = await item_crud.select_models(
138138
session=db,
139139
__gor__=[

docs/advanced/flush.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## `flush()`
2+
3+
- `flush` 是将更改从 Python 移动到数据库的事务缓冲区
4+
- 它会生成必要的 SQL 语句并发送到数据库执行,但不会提交(commit)事务
5+
-`flush` 之后,数据会被写入数据库,但事务依然是活跃的(未提交),除非发生错误,在这种情况下,整个事务将回滚,`flush`
6+
的更改也会被撤销
7+
8+
!!! tip "提示"
9+
10+
如果你在事务提交前无需对新实例进行某些操作,`flush` 是没必要的,在 `commit` 时,SQLAlchemy 会隐式地调用一次 `flush`,
11+
确保所有挂起的更改都被同步到数据库,然后提交事务

docs/usage/create_model.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ async def create_model(
33
self,
44
session: AsyncSession,
55
obj: CreateSchema,
6+
flush: bool = False,
67
commit: bool = False,
78
**kwargs,
89
) -> Model:
910
````
1011

11-
此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
12+
- 此方法提供 `flush` 参数,详见:[冲洗](../advanced/flush.md)
13+
- 此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
1214

1315
!!! note "关键字参数"
1416

docs/usage/create_models.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ async def create_models(
33
self,
44
session: AsyncSession,
55
objs: Iterable[CreateSchema],
6+
flush: bool = False,
67
commit: bool = False,
78
**kwargs,
89
) -> list[Model]:
910
```
1011

12+
- 此方法提供 `flush` 参数,详见:[冲洗](../advanced/flush.md)
1113
- 此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
12-
1314
- 此方法还提供与 `create_model()` 相同用法的关键字参数,需要额外注意的是,`create_models()` 会将关键字参数写入每个实例中
1415

1516
## 示例

docs/usage/delete_model.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ async def delete_model(
33
self,
44
session: AsyncSession,
55
pk: int,
6+
flush: bool = False,
67
commit: bool = False,
78
) -> int:
89
```
910

10-
- 此方法使用主键 pk 参数,详见:[主键](../advanced/primary_key.md)
11-
11+
- 此方法提供 `flush` 参数,详见:[冲洗](../advanced/flush.md)
12+
- 此方法使用主键 `pk` 参数,详见:[主键](../advanced/primary_key.md)
1213
- 此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
1314

1415
## 示例

docs/usage/delete_model_by_column.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ async def delete_model_by_column(
55
allow_multiple: bool = False,
66
logical_deletion: bool = False,
77
deleted_flag_column: str = 'del_flag',
8+
flush: bool = False,
89
commit: bool = False,
910
**kwargs,
1011
) -> int:
1112
```
1213

14+
- 此方法提供 `flush` 参数,详见:[冲洗](../advanced/flush.md)
1315
- 此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
14-
1516
- 此方法可结合 [高级过滤器](../advanced/filter.md) 使用
1617

1718
## 删除多条

docs/usage/select_model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
async def select_model(self, session: AsyncSession, pk: int) -> Model | None:
33
```
44

5-
此方法使用主键 pk 参数,详见:[主键](../advanced/primary_key.md)
5+
此方法使用主键 `pk` 参数,详见:[主键](../advanced/primary_key.md)
66

77
## 示例
88

docs/usage/update_model.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ async def update_model(
44
session: AsyncSession,
55
pk: int,
66
obj: UpdateSchema | dict[str, Any],
7+
flush: bool = False,
78
commit: bool = False,
89
**kwargs,
910
) -> int:
1011
```
1112

12-
- 此方法使用主键 pk 参数,详见:[主键](../advanced/primary_key.md)
13-
13+
- 此方法提供 `flush` 参数,详见:[冲洗](../advanced/flush.md)
14+
- 此方法使用主键 `pk` 参数,详见:[主键](../advanced/primary_key.md)
1415
- 此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
15-
1616
- 此方法还提供与 `create_model()` 相同用法的关键字参数
1717

1818
## 示例

docs/usage/update_model_by_column.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ async def update_model_by_column(
44
session: AsyncSession,
55
obj: UpdateSchema | dict[str, Any],
66
allow_multiple: bool = False,
7+
flush: bool = False,
78
commit: bool = False,
89
**kwargs,
910
) -> int:
1011
```
1112

13+
- 此方法提供 `flush` 参数,详见:[冲洗](../advanced/flush.md)
1214
- 此方法提供 `commit` 参数,详见:[提交](../advanced/commit.md)
13-
1415
- 此方法可结合 [高级过滤器](../advanced/filter.md) 使用
1516

1617
## 更新多条

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ nav:
2727
- 高级用法: usage/delete_model_by_column.md
2828
- Advanced:
2929
- 主键: advanced/primary_key.md
30+
- 冲洗: advanced/flush.md
3031
- 提交: advanced/commit.md
3132
- 条件过滤: advanced/filter.md
3233
- Changelog: changelog.md

sqlalchemy_crud_plus/crud.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ async def create_model(
3030
self,
3131
session: AsyncSession,
3232
obj: CreateSchema,
33+
flush: bool = False,
3334
commit: bool = False,
3435
**kwargs,
3536
) -> Model:
@@ -38,6 +39,7 @@ async def create_model(
3839
3940
:param session: The SQLAlchemy async session.
4041
:param obj: The Pydantic schema containing data to be saved.
42+
:param flush: If `True`, flush all object changes to the database. Default is `False`.
4143
:param commit: If `True`, commits the transaction immediately. Default is `False`.
4244
:param kwargs: Additional model data not included in the pydantic schema.
4345
:return:
@@ -47,6 +49,8 @@ async def create_model(
4749
else:
4850
ins = self.model(**obj.model_dump(), **kwargs)
4951
session.add(ins)
52+
if flush:
53+
await session.flush()
5054
if commit:
5155
await session.commit()
5256
return ins
@@ -55,6 +59,7 @@ async def create_models(
5559
self,
5660
session: AsyncSession,
5761
objs: Iterable[CreateSchema],
62+
flush: bool = False,
5863
commit: bool = False,
5964
**kwargs,
6065
) -> list[Model]:
@@ -63,6 +68,7 @@ async def create_models(
6368
6469
:param session: The SQLAlchemy async session.
6570
:param objs: The Pydantic schema list containing data to be saved.
71+
:param flush: If `True`, flush all object changes to the database. Default is `False`.
6672
:param commit: If `True`, commits the transaction immediately. Default is `False`.
6773
:param kwargs: Additional model data not included in the pydantic schema.
6874
:return:
@@ -75,6 +81,8 @@ async def create_models(
7581
ins = self.model(**obj.model_dump(), **kwargs)
7682
ins_list.append(ins)
7783
session.add_all(ins_list)
84+
if flush:
85+
await session.flush()
7886
if commit:
7987
await session.commit()
8088
return ins_list
@@ -169,6 +177,7 @@ async def update_model(
169177
session: AsyncSession,
170178
pk: int,
171179
obj: UpdateSchema | dict[str, Any],
180+
flush: bool = False,
172181
commit: bool = False,
173182
**kwargs,
174183
) -> int:
@@ -178,6 +187,7 @@ async def update_model(
178187
:param session: The SQLAlchemy async session.
179188
:param pk: The database primary key value.
180189
:param obj: A pydantic schema or dictionary containing the update data
190+
:param flush: If `True`, flush all object changes to the database. Default is `False`.
181191
:param commit: If `True`, commits the transaction immediately. Default is `False`.
182192
:return:
183193
"""
@@ -189,6 +199,8 @@ async def update_model(
189199
instance_data.update(kwargs)
190200
stmt = update(self.model).where(self.primary_key == pk).values(**instance_data)
191201
result = await session.execute(stmt)
202+
if flush:
203+
await session.flush()
192204
if commit:
193205
await session.commit()
194206
return result.rowcount # type: ignore
@@ -198,6 +210,7 @@ async def update_model_by_column(
198210
session: AsyncSession,
199211
obj: UpdateSchema | dict[str, Any],
200212
allow_multiple: bool = False,
213+
flush: bool = False,
201214
commit: bool = False,
202215
**kwargs,
203216
) -> int:
@@ -207,6 +220,7 @@ async def update_model_by_column(
207220
:param session: The SQLAlchemy async session.
208221
:param obj: A pydantic schema or dictionary containing the update data
209222
:param allow_multiple: If `True`, allows updating multiple records that match the filters.
223+
:param flush: If `True`, flush all object changes to the database. Default is `False`.
210224
:param commit: If `True`, commits the transaction immediately. Default is `False`.
211225
:param kwargs: Query expressions.
212226
:return:
@@ -221,6 +235,8 @@ async def update_model_by_column(
221235
instance_data = obj.model_dump(exclude_unset=True)
222236
stmt = update(self.model).where(*filters).values(**instance_data) # type: ignore
223237
result = await session.execute(stmt)
238+
if flush:
239+
await session.flush()
224240
if commit:
225241
await session.commit()
226242
return result.rowcount # type: ignore
@@ -229,18 +245,22 @@ async def delete_model(
229245
self,
230246
session: AsyncSession,
231247
pk: int,
248+
flush: bool = False,
232249
commit: bool = False,
233250
) -> int:
234251
"""
235252
Delete an instance by model's primary key
236253
237254
:param session: The SQLAlchemy async session.
238255
:param pk: The database primary key value.
256+
:param flush: If `True`, flush all object changes to the database. Default is `False`.
239257
:param commit: If `True`, commits the transaction immediately. Default is `False`.
240258
:return:
241259
"""
242260
stmt = delete(self.model).where(self.primary_key == pk)
243261
result = await session.execute(stmt)
262+
if flush:
263+
await session.flush()
244264
if commit:
245265
await session.commit()
246266
return result.rowcount # type: ignore
@@ -251,18 +271,20 @@ async def delete_model_by_column(
251271
allow_multiple: bool = False,
252272
logical_deletion: bool = False,
253273
deleted_flag_column: str = 'del_flag',
274+
flush: bool = False,
254275
commit: bool = False,
255276
**kwargs,
256277
) -> int:
257278
"""
258-
Delete
279+
Delete an instance by model column
259280
260281
:param session: The SQLAlchemy async session.
261-
:param commit: If `True`, commits the transaction immediately. Default is `False`.
262-
:param kwargs: Query expressions.
263282
:param allow_multiple: If `True`, allows deleting multiple records that match the filters.
264283
:param logical_deletion: If `True`, enable logical deletion instead of physical deletion
265284
:param deleted_flag_column: Specify the flag column for logical deletion
285+
:param flush: If `True`, flush all object changes to the database. Default is `False`.
286+
:param commit: If `True`, commits the transaction immediately. Default is `False`.
287+
:param kwargs: Query expressions.
266288
:return:
267289
"""
268290
filters = parse_filters(self.model, **kwargs)
@@ -275,6 +297,8 @@ async def delete_model_by_column(
275297
else:
276298
stmt = delete(self.model).where(*filters)
277299
result = await session.execute(stmt)
300+
if flush:
301+
await session.flush()
278302
if commit:
279303
await session.commit()
280304
return result.rowcount # type: ignore

0 commit comments

Comments
 (0)