Skip to content

Commit f3a62c6

Browse files
committed
update docs
1 parent f2ddfbd commit f3a62c6

File tree

15 files changed

+613
-627
lines changed

15 files changed

+613
-627
lines changed

docs/.vuepress/data/plugin.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,50 @@ export const pluginItems: PluginItem[] = [
55
icon: "fa6-regular:file-code",
66
title: "代码生成",
77
description: "生成通用业务代码",
8-
tags: ["mysql", "pgsql", "后端", "前端"],
8+
tags: ["pgsql", "mysql", "后端", "前端"],
99
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
1010
},
1111
{
1212
icon: "fe:notice-active",
1313
title: "通知公告",
1414
description: "发布系统内部通知、公告",
15-
tags: ["mysql", "pgsql", "后端", "前端"],
15+
tags: ["pgsql", "mysql", "后端", "前端"],
1616
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
1717
},
1818
{
1919
icon: "fluent-mdl2:dictionary",
2020
title: "数据字典",
2121
description: "通常用于约束前端工程数据传输/展示",
22-
tags: ["mysql", "pgsql", "后端", "前端"],
22+
tags: ["pgsql", "mysql", "后端", "前端"],
2323
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
2424
},
2525
{
2626
icon: "icon-park-outline:config",
2727
title: "参数配置",
2828
description: "通常用于动态配置系统参数/前端工程数据展示",
29-
tags: ["mysql", "pgsql", "后端", "前端"],
29+
tags: ["pgsql", "mysql", "后端", "前端"],
3030
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
3131
},
3232
{
3333
icon: "logos:oauth",
3434
title: "OAuth 2.0",
3535
description: "通过 OAuth 2.0 的方式登录系统",
36-
tags: ["mysql", "pgsql", "后端", "前端"],
36+
tags: ["pgsql", "mysql", "后端", "前端"],
3737
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
3838
},
3939
{
4040
icon: "ic:twotone-email",
4141
title: "Email",
4242
description: "发送电子邮件,例如验证码、通知等",
43-
tags: ["mysql", "pgsql", "后端", "前端"],
43+
tags: ["pgsql", "mysql", "后端", "前端"],
4444
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
4545
},
4646
{
4747
icon: "charm:shield-keyhole",
4848
title: "Casbin-RBAC",
4949
description: "基于 Casbin 实现的 RBAC 权限",
5050
label: '官方',
51-
tags: ["mysql", "pgsql", "后端"],
51+
tags: ["pgsql", "mysql", "后端"],
5252
link: "https://github.com/fastapi-practices/casbin_rbac",
5353
image: "https://avatars.githubusercontent.com/u/27810343?s=200&v=4",
5454
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
@@ -58,7 +58,7 @@ export const pluginItems: PluginItem[] = [
5858
title: "MCP 服务器管理",
5959
description: "提供了 MCP 服务器管理功能,并添加了可调用 MCP 的 AI 聊天接口",
6060
label: '官方',
61-
tags: ["mysql", "pgsql", "后端"],
61+
tags: ["pgsql", "mysql", "后端"],
6262
link: "https://github.com/fastapi-practices/mcp",
6363
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
6464
},
@@ -67,7 +67,7 @@ export const pluginItems: PluginItem[] = [
6767
title: "阿里云 oss",
6868
description: "阿里云 oss 文件上传",
6969
label: '官方',
70-
tags: ["mysql", "pgsql", "后端"],
70+
tags: ["pgsql", "mysql", "后端"],
7171
link: "https://github.com/fastapi-practices/aliyun_oss",
7272
image: "https://registry.npmmirror.com/@lobehub/icons-static-png/latest/files/dark/alibabacloud-color.png",
7373
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
@@ -97,7 +97,7 @@ export const pluginItems: PluginItem[] = [
9797
title: "Casdoor SSO",
9898
description: "通过 Casdoor 实现 SSO 单点登录集成",
9999
label: '官方',
100-
tags: ["unfree", "mysql", "pgsql", "后端"],
100+
tags: ["unfree", "pgsql", "mysql", "后端"],
101101
link: "https://github.com/fastapi-practices/casdoor_sso",
102102
image: "https://casdoor.org/zh/img/casdoor.png",
103103
logo: 'https://wu-clan.github.io/picx-images-hosting/logo/fba.png',
@@ -107,7 +107,7 @@ export const pluginItems: PluginItem[] = [
107107
title: "LDAP",
108108
description: "通过 LDAP 登录系统并授权",
109109
label: '第三方',
110-
tags: ["mysql", "pgsql", "后端"],
110+
tags: ["pgsql", "mysql", "后端"],
111111
link: "https://github.com/dividduang/ldap_auth",
112112
image: "https://www.cleo.com/sites/default/files/2023-12/ldap-integration.png",
113113
logo: 'https://avatars.githubusercontent.com/u/110005582?v=4',
@@ -117,7 +117,7 @@ export const pluginItems: PluginItem[] = [
117117
title: "腾讯云 sms",
118118
description: "通过腾讯云短信服务发送短信验证码",
119119
label: '第三方',
120-
tags: ["mysql", "pgsql", "后端"],
120+
tags: ["pgsql", "mysql", "后端"],
121121
link: "https://github.com/RanY-Luck/fba_plugin_sms",
122122
image: "https://registry.npmmirror.com/@lobehub/icons-static-png/latest/files/dark/tencentcloud-color.png",
123123
logo: 'https://avatars.githubusercontent.com/u/67427627?v=4',

docs/backend/reference/CORS.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,3 @@ CORS_ALLOWED_ORIGINS: list[str] = [
4343
```py
4444
CORS_ALLOWED_ORIGINS: list[str] = ['*']
4545
```
46-
47-
## 注意事项
48-
49-
::: warning
50-
当将 `CORS_ALLOWED_ORIGINS` 配置为 `['*']` 时,socketio 将无法正常工作,这是由于它们的配置方式不同所导致的
51-
52-
这里有一个相关 PR: [python-engineio/pull/410](https://github.com/miguelgrinberg/python-engineio/pull/410)
53-
54-
虽然它已被合并,但是并没有发布新版本,截至目前,您需要在 `backend/common/socketio/server.py` 文件中修改以下内容:
55-
56-
`cors_allowed_origins=settings.CORS_ALLOWED_ORIGINS` -> `cors_allowed_origins='*'`
57-
:::

docs/backend/reference/code-generation.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
title: 代码生成
33
---
44

5-
> [!WARNING]
6-
> API 调用无法直观的预览代码生成结果,它必须配合前端项目使用,请查看查看 [效果预览](#效果预览)
5+
::: tip
6+
此功能当前仅适用于后端工程,不包含前端代码
7+
:::
8+
9+
::: warning
10+
API 调用无法直观的预览代码生成结果,它必须配合前端项目使用,请查看查看 [效果预览](#效果预览)
11+
:::
712

813
## 概念
914

docs/backend/reference/data-permission.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
title: 数据权限
33
---
44

5-
数据权限是为了给数据添加权限而建立的,我们最常见的实现方式是仅本人数据,本部门数据...
5+
数据权限是为了给数据添加权限而建立的,我们最常见的实现方案是仅本人数据,本部门数据...
66
这些就是所谓的数据权限,你可以控制不同的角色拥有不同的数据权限,从而实现用户和数据的隔离
77

8-
## 弊端
8+
## 常见方案
99

10-
如上所述,我们常见的这种数据权限,在大多数情况下,也能够满足日常场景所需,但是,这种常见的方式存在严重的弊端。由于数据权限的数据过滤是通过
11-
SQL 语句拼接进行实现的,而这些固定权限,直接写死了数据权限的要求,例如:业务表必须包含 dept_id 和 created_by 字段,
12-
如果没有这些业务字段,你就无法通过 SQL 来控制数据权限,下面是我对这种常规数据权限的实现,仅供参考:
10+
::: caution
11+
fba 已删除此集成方式,此代码仅作为示例保留
12+
:::
1313

1414
@[code python](../../code/data_perm.py)
1515

16-
请注意,当前 fba 中已删除此集成方式,你需要自行更新角色 model、schema,添加数据权限控制标识字段 data_scope 等相关代码
16+
### 弊端
1717

18-
## 想法
18+
我们常见的这种数据权限,在大多数情况下,也能够满足日常场景所需,但是,这种方式存在严重的弊端。由于数据权限的数据过滤是通过
19+
SQL 语句拼接进行实现的,而这些固定权限,直接写死了数据权限的要求
1920

20-
有没有一种更加灵活的方案呢,答案是,当然有,目前,我们在 fba
21-
中实现的正式超灵活方案,但是相对于常见方案来讲,可配更加复杂,但我们仍有很大的优化空间
21+
例如:业务表必须包含 dept_id 和 created_by 字段,如果业务表没有这些字段,你就无法通过 SQL 来控制数据权限
2222

23-
你可以直接查看代码源文件 `backend/common/security/permission.py`,其中,filter\_data\_permission
24-
方法正是我们使用灵活方案,它与常规数据权限使用近乎相同的方式实现数据过滤,但由于其复杂性,下面,我们将通过视频进行讲解: [数据权限管理](https://www.bilibili.com/video/BV13hioY1EQU/?share_source=copy_web\&vd_source=ccb2aae47e179a51460c20d165021cb7)
23+
## 内置方案
24+
25+
有没有一种更加灵活的方案呢,答案是,当然有,目前,我们在 fba 中实现的正是超灵活方案,但相比于常见方案来讲,配置会更加复杂
26+
27+
你可以直接查看代码源文件 `backend/common/security/permission.py`
28+
,它与常规方案使用近乎相同的方式实现数据过滤,但由于其复杂性,下面,我们将通过视频进行讲解: [数据权限管理](https://www.bilibili.com/video/BV13hioY1EQU/?share_source=copy_web\&vd_source=ccb2aae47e179a51460c20d165021cb7)

docs/backend/reference/model.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ title: 模型
88

99
我们未提供自动主键模式,而是必须通过手动定义的方式进行主键声明
1010

11+
### 自增 ID
12+
1113
```python
1214
# 通用 Mapped 类型主键, 需手动添加,参考以下使用方式
1315
# MappedBase -> id: Mapped[id_key]
@@ -26,6 +28,10 @@ id_key = Annotated[
2628
]
2729
```
2830

31+
### 雪花 ID
32+
33+
[请移步至 **切换主键**](pk.md){.read-more}
34+
2935
## Mixin 类
3036

3137
[Mixin](https://en.wikipedia.org/wiki/Mixin) 是一种面向对象编程概念, 使结构变得更加清晰
@@ -34,7 +40,7 @@ id_key = Annotated[
3440

3541
用于集成操作人信息到数据库表
3642

37-
[使用方法请移步至 **操作人**](operator.md){.read-more}
43+
[请移步至 **操作人**](operator.md){.read-more}
3844

3945
### 日期时间
4046

docs/backend/reference/oauth2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: OAuth 2.0
33
---
44

55
我们在 fba 中使用 [fastapi-oauth20](https://github.com/fastapi-practices/fastapi-oauth20) 集成 OAuth 2.0,您可以在
6-
`backend/app/admin/api/v1/oauth2` 目录中查看我们的官方实现示例
6+
`backend/plugin/oauth2` 目录中查看我们的官方实现示例
77

88
::: note
99
此授权方式适用于第三方平台认证登录,第三方授权成功后,将依据第三方平台信息自动创建本地用户并自动授权登录,用户只需同意第三方授权即可

docs/backend/reference/transaction.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: SQLA 事务
2+
title: 事务
33
---
44

55
默认情况下,如果将数据库引擎参数 `echo` 设置为 True,你将会看到事务总是被开启,即便那是一个查询语句。但这并不是因为我们错误的使用了
@@ -13,10 +13,9 @@ SQLAlchemy,你可以查看 [#6921](https://github.com/sqlalchemy/sqlalchemy/di
1313
,详情请查看: [了解 DBAPI 级别的 Autocommit 隔离级别](https://docs.sqlalchemy.org.cn/en/20/core/connections.html#understanding-the-dbapi-level-autocommit-isolation-level)
1414
:::
1515

16-
## Session 生成器
16+
## CurrentSession
1717

18-
这是一种类似于官方文档的使用方法,但这种方法并没有真正达到事务的目的,因为它不会自动执行提交,所以,你可以将它理解为仅适用于查询,否则,必须手动执行
19-
`commit()` 方法
18+
这是一种类似于官方文档的使用方法,但这种方法并没有真正开启事务,它通常仅用于查询操作
2019

2120
```python
2221
async def get_db() -> AsyncGenerator[AsyncSession, None]:
@@ -36,19 +35,35 @@ async def get_pagination_apis(db: CurrentSession) -> ResponseModel:
3635
...
3736
```
3837

38+
## CurrentSessionTransaction
39+
40+
`CurrentSession` 不同,此方法将直接自动开启事务,你可以将它用于增删改操作
41+
42+
```python
43+
async def get_db_transaction() -> AsyncGenerator[AsyncSession, None]:
44+
"""获取带有事务的数据库会话"""
45+
async with async_db_session.begin() as session:
46+
yield session
47+
48+
# Session Annotated
49+
CurrentSessionTransaction = Annotated[AsyncSession, Depends(get_db_transaction)]
50+
```
51+
52+
使用方法与 `CurrentSession` 相同
53+
54+
```python
55+
@router.post('')
56+
async def get_pagination_apis(db: CurrentSession) -> ResponseModel:
57+
...
58+
```
59+
3960
## `begin()`
4061

41-
这种方式由 SQLAlchemy 官方实现,在线程安全方面,由于在同一个函数中,可能存在多次调用,所以没有 Session 生成器严谨
62+
这种方式由 SQLAlchemy 官方实现,在线程安全方面,由于在同一个函数中,可能存在多次调用,所以没有 `CurrentSession`
63+
`CurrentSessionTransaction` 更加严谨,但此方式可以在任意地方使用
4264

4365
```python{2}
4466
async def create(*, obj: CreateIns) -> None:
4567
async with async_db_session.begin() as db:
4668
await xxx_dao.create(db, obj)
4769
```
48-
49-
## 如何选择?
50-
51-
以上两种方法,我们更推荐使用 `begin()`,理由如下:
52-
53-
1. 对于 fba 来说,它更加符合架构风格,并且也能减少千篇一律的接口参数(纯作者强迫症)
54-
2. 而对于无需使用自动提交的事务,我们只需将 `begin()` 方法去掉,直接使用 `async_db_session()` 即可

docs/backend/summary/fsm.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ FastAPI 最佳架构精简版的目标是仅保留最最最简单的架构代码
66

77
## SQLAlchemy
88

9+
::: caution
10+
此版本的更新速度并不同步,我们目前正在积极寻找维护人员
11+
:::
12+
913
<LinkCard
1014
title="fastapi_sqlalchemy_mysql"
1115
description="fastapi + pydantic-v2 + sqlalchemy 2.0 + alembic + mysql + redis"

docs/backend/summary/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mvc 架构作为常规设计模式,在 python web 中很常见,但是三层
1010

1111
在 python web 开发中,三层架构的概念并没有通用标准,所以这里我们称之为伪三层架构
1212

13-
!!但请注意!!
13+
!但请注意
1414

1515
我们并没有传统的多 app (微服务)目录结构(django、springBoot...),而是[自以为是的目录结构](#项目结构)
1616

docs/backend/summary/quick-start.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fba 仅适用于资深 Python 后端开发人员,如果您是非资深用户
2020

2121
2. 创建数据库:`fba`
2222

23-
- PostgreSQL 用户直接创建
24-
- MySQL 用户创建时需选择 utf8mb4 编码
23+
- PostgreSQL 用户直接创建
24+
- MySQL 用户创建时需选择 utf8mb4 编码
2525

2626
3. 启动 Redis
2727
4. 准备源码 <Badge type="warning" text="二选一" />
@@ -93,42 +93,42 @@ fba 仅适用于资深 Python 后端开发人员,如果您是非资深用户
9393
```
9494
:::
9595

96-
9. 启动 celery worker, beat 和 flower <Badge type="warning" text="此步骤为可选,可直接跳过" />
96+
9. 启动
9797

98-
`根目录` 打开终端,执行以下命令启动 celery 相关服务
98+
`根目录` 打开终端,执行以下命令启动 FastAPI 服务
9999

100-
::: code-tabs
101-
@tab Worker
100+
```shell:no-line-numbers
101+
fba run
102+
```
102103

103-
```shell:no-line-numbers
104-
fba celery worker
105-
```
104+
10. 启动 celery worker, beat 和 flower <Badge type="warning" text="此步骤为可选,可直接跳过" />
106105

107-
@tab Beat
106+
`根目录` 打开终端,执行以下命令启动 celery 相关服务
108107

109-
```shell:no-line-numbers
110-
fba celery beat
111-
```
108+
::: code-tabs
109+
@tab Worker
112110

113-
@tab Flower
111+
```shell:no-line-numbers
112+
fba celery worker
113+
```
114114
115-
```shell:no-line-numbers
116-
fba celery flower
117-
```
118-
:::
115+
@tab Beat
119116
120-
::: warning
121-
如果从未执行过以上命令,任务结果表将缺失,此时,无论从何处调用任务结果相关接口都会直接报错,直到至少启动一次 worker 和
122-
beat 服务,相关接口将自动恢复正常
123-
:::
117+
```shell:no-line-numbers
118+
fba celery beat
119+
```
124120
125-
10. 启动
121+
@tab Flower
126122
127-
在 `根目录` 打开终端,执行以下命令启动 FastAPI 服务
123+
```shell:no-line-numbers
124+
fba celery flower
125+
```
126+
:::
128127
129-
```shell:no-line-numbers
130-
fba run
131-
```
128+
::: warning
129+
如果从未执行过以上命令,任务结果表将缺失,此时,无论从何处调用任务结果相关接口都会直接报错,直到至少启动一次 worker 和
130+
beat 服务,相关接口将自动恢复正常
131+
:::
132132
133133
11. 初始化测试数据
134134

0 commit comments

Comments
 (0)