Skip to content

Commit f74a784

Browse files
committed
📝 separate database & server
1 parent 27e7867 commit f74a784

File tree

10 files changed

+321
-151
lines changed

10 files changed

+321
-151
lines changed

.vitepress/config/sidebar.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ const tutorial = [
3434
},
3535
{
3636
text: "Entari",
37-
link: "/tutorial/entari.md"
37+
link: "/tutorial/entari/index.md",
38+
items: [
39+
{ text: "服务器", link: "/tutorial/entari/server.md" },
40+
{ text: "数据库", link: "/tutorial/entari/database.md" },
41+
]
3842
},
3943
{
4044
text: "Tarina",

tutorial/alconna/v1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pdm add arclet-alconna
6969
```
7070

7171

72-
```bash:no-line-numbers [poetry]
73-
poetry add arclet-alconna
72+
```bash:no-line-numbers [uv]
73+
uv add arclet-alconna
7474
```
7575

7676
```bash:no-line-numbers [pip]

tutorial/alconna/v2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ git clone https://github.com/ArcletProject/Alconna@dev
8181

8282
[//]: # ()
8383
[//]: # ()
84-
[//]: # (```bash [poetry])
84+
[//]: # (```bash [uv])
8585

86-
[//]: # (poetry add arclet-alconna)
86+
[//]: # (uv add arclet-alconna)
8787

8888
[//]: # (```)
8989

tutorial/entari/database.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
prev: 服务器插件
3+
next: Tarina
4+
---
5+
6+
# 数据库插件
7+
8+
`entari-plugin-database` 属于官方插件,允许你在插件中使用数据库进行数据存储和查询。
9+
10+
由于基于 [`SQLAlchemy`](https://www.sqlalchemy.org/),大部分情况下,你可以直接使用 SQLAlchemy 的 API 来操作数据库。
11+
12+
本插件只提供了 ORM 功能,没有数据库后端,也没有直接连接数据库后端的能力。 所以你需要另行安装数据库驱动和数据库后端,并且配置数据库连接信息。
13+
14+
## 安装
15+
16+
::: code-group
17+
```bash:no-line-numbers [pdm]
18+
pdm add entari-plugin-database
19+
```
20+
21+
```bash:no-line-numbers [uv]
22+
uv add entari-plugin-database
23+
```
24+
25+
```bash:no-line-numbers [pip]
26+
pip install entari-plugin-database
27+
```
28+
:::
29+
30+
## 配置连接
31+
在配置文件中,你可以通过 `database` 字段来配置数据库连接:
32+
33+
```yaml:no-line-numbers title=entari.yml
34+
basic:
35+
log:
36+
ignores: ["aiosqlite.core"] # 忽略 aiosqlite 的 DEBUG 日志
37+
plugins:
38+
database:
39+
type: sqlite # 数据库类型, 可选值有 sqlite, mysql, postgresql, oracle 等
40+
name: my_database.db # 数据库名称或文件目录
41+
driver: aiosqlite # 数据库驱动, 根据数据库类型选择
42+
...
43+
```
44+
45+
`type``driver` 的支持列表详见 [Dialects](https://docs.sqlalchemy.org/en/21/dialects/#included-dialects)
46+
47+
其余的配置项包括:
48+
- `host`: 数据库主机地址 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要)
49+
- `port`: 数据库端口号 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要)
50+
- `username`: 数据库用户名 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要)
51+
- `password`: 数据库密码 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要)
52+
- `query`: 数据库连接参数 (仅在使用 MySQL/PostgreSQL 等远程数据库时需要)
53+
- `options`: SQLAlchemy 的其他选项。参见 [Engine Creation API](https://docs.sqlalchemy.org/en/21/core/engines.html#engine-creation-api)
54+
55+
若不传入配置项,则默认使用 SQLite 数据库,并将数据库文件存储在当前目录下。
56+
57+
## 定义模型
58+
59+
`database` 插件使用 SQLAlchemy 的 ORM 功能来定义模型。你可以通过继承 `database.Base` 类来定义你的模型类。
60+
61+
```python title=my_plugin.py
62+
from entari_plugin_database import Base, Mapped, mapped_column
63+
64+
class Weather(Base):
65+
__tablename__ = "weather"
66+
67+
location: Mapped[str] = mapped_column(primary_key=True)
68+
weather: Mapped[str]
69+
```
70+
71+
我们可以用以下代码检查模型生成的数据库模式是否正确:
72+
73+
```python
74+
from sqlalchemy.schema import CreateTable
75+
76+
print(CreateTable(Weather.__table__))
77+
```
78+
79+
```sql
80+
CREATE TABLE weather (
81+
location VARCHAR NOT NULL,
82+
weather VARCHAR NOT NULL,
83+
CONSTRAINT pk_weather PRIMARY KEY (location)
84+
)
85+
```
86+
87+
88+
## 使用会话
89+
90+
`database` 插件通过 `SqlalchemyService` 提供数据库会话服务。
91+
92+
你可以通过依赖注入的方式获取 `SqlalchemyService` 实例,并使用它来获取数据库会话。
93+
94+
:::code-group
95+
96+
```python title=my_plugin.py [ORM]
97+
from arclet.entari import Session, command
98+
from entari_plugin_database import SqlalchemyService
99+
from sqlalchemy import select
100+
101+
@command.on("get_weather {location}")
102+
async def on_message(location: str, session: Session, db: SqlalchemyService):
103+
async with db.get_session() as db_session:
104+
# 在这里使用 SQLAlchemy 的会话进行数据库操作
105+
result = await db_session.scalars(select(Weather).where(Weather.location == location))
106+
data = result.all()
107+
await session.send(f"Data: {data}")
108+
```
109+
110+
111+
```python title=my_plugin.py [SQL语句]
112+
from arclet.entari import Session, command
113+
from entari_plugin_database import SqlalchemyService
114+
from sqlalchemy import text
115+
116+
@command.on("get_weather {location}")
117+
async def on_message(location: str, session: Session, db: SqlalchemyService):
118+
async with db.get_session() as db_session:
119+
# 在这里使用 SQLAlchemy 的会话进行数据库操作
120+
result = await db_session.execute(text("SELECT * FROM weather WHERE location=:location"), {"location": location})
121+
data = result.fetchall()
122+
await session.send(f"Data: {data}")
123+
```
124+
125+
:::
126+
127+
关于如何使用 SQLAlchemy 的 ORM 功能,你可以参考 [SQLAlchemy 官方文档](https://docs.sqlalchemy.org/en/21/orm/quickstart.html)

0 commit comments

Comments
 (0)