# ๐ ๐ (๐) ๐ฝ
๐ ๐ช โ๏ธ `encode/databases` โฎ๏ธ **FastAPI** ๐ ๐ฝ โ๏ธ `async` & `await`.
โซ๏ธ ๐ โฎ๏ธ:
* โณ
* โณ
* ๐
๐ ๐ผ, ๐ฅ ๐ โ๏ธ **๐**, โฉ๏ธ โซ๏ธ โ๏ธ ๐ ๐ & ๐ โ๏ธ ๐ ๏ธ ๐โ๐ฆบ. , ๐ ๐ช ๐ ๐ ๐ผ & ๐ โซ๏ธ.
โช, ๐ ๐ญ ๐ธ, ๐ ๐ช ๐ โ๏ธ ๐ฝ ๐ฝ ๐ **โณ**.
!!! tip
๐ ๐ช ๐ ๏ธ ๐ญ โช๏ธโก๏ธ ๐ ๐ ๐ธ๐ฒ ๐ ([๐ (๐) ๐ฝ](../tutorial/sql-databases.md){.internal-link target=_blank}), ๐ โ๏ธ ๐ ๐ข ๐ญ ๐ ๏ธ ๐ฝ, ๐ฌ ๐ **FastAPI** ๐.
๐ ๐ ๐ซ โ ๐ ๐ญ, ๐ ๐ ๐.
## ๐ & โ ๐ `SQLAlchemy`
* ๐ `SQLAlchemy`.
* โ `metadata` ๐.
* โ ๐ `notes` โ๏ธ `metadata` ๐.
```Python hl_lines="4 14 16-22"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
!!! tip
๐ ๐ ๐ ๐ ๐ ๐ ๐ธ๐ฒ ๐.
`databases` ๐ซ ๐จ ๐ณ ๐ฅ.
## ๐ & โ ๐ `databases`
* ๐ `databases`.
* โ `DATABASE_URL`.
* โ `database` ๐.
```Python hl_lines="3 9 12"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
!!! tip
๐ฅ ๐ ๐ ๐ ๐ฝ (โ
โณ), ๐ ๐ ๐ช ๐ `DATABASE_URL`.
## โ ๐
๐ ๐ผ, ๐ฅ ๐ ๐ ๐ ๐ ๐, โ๏ธ ๐ญ, ๐ ๐ ๐ฒ ๐ โ ๐ซ โฎ๏ธ โ, ๐ ๏ธ โฎ๏ธ ๐ ๏ธ, โ๏ธ.
๐ฅ, ๐ ๐ ๐ ๐ ๐, โถ๏ธ๏ธ โญ โถ๏ธ ๐ **FastAPI** ๐ธ.
* โ `engine`.
* โ ๐ ๐ โช๏ธโก๏ธ `metadata` ๐.
```Python hl_lines="25-28"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
## โ ๐ท
โ Pydantic ๐ท:
* ๐ โ (`NoteIn`).
* ๐ ๐จ (`Note`).
```Python hl_lines="31-33 36-39"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
๐ ๐ซ Pydantic ๐ท, ๐ข ๐ฝ ๐ โ, ๐ป (๐), & โ (๐).
, ๐ ๐ ๐ช ๐ โซ๏ธ ๐ ๐ ๐ ๏ธ ๐ฉบ.
## ๐ & ๐
* โ ๐ `FastAPI` ๐ธ.
* โ ๐ ๐โ๐ฆบ ๐ & ๐ โช๏ธโก๏ธ ๐ฝ.
```Python hl_lines="42 45-47 50-52"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
## โ ๐
โ *โก ๐ ๏ธ ๐ข* โ ๐:
```Python hl_lines="55-58"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
!!! Note
๐ ๐ ๐ฅ ๐ โฎ๏ธ ๐ฝ โ๏ธ `await`, *โก ๐ ๏ธ ๐ข* ๐ฃ โฎ๏ธ `async`.
### ๐ `response_model=List[Note]`
โซ๏ธ โ๏ธ `typing.List`.
๐ ๐ (& โ, ๐ป, โฝ) ๐ข ๐ฝ, `list` `Note`โ.
## โ ๐
โ *โก ๐ ๏ธ ๐ข* โ ๐:
```Python hl_lines="61-65"
{!../../../docs_src/async_sql_databases/tutorial001.py!}
```
!!! Note
๐ ๐ ๐ฅ ๐ โฎ๏ธ ๐ฝ โ๏ธ `await`, *โก ๐ ๏ธ ๐ข* ๐ฃ โฎ๏ธ `async`.
### ๐ `{**note.dict(), "id": last_record_id}`
`note` Pydantic `Note` ๐.
`note.dict()` ๐จ `dict` โฎ๏ธ ๐ฎ ๐ฝ, ๐ณ ๐:
```Python
{
"text": "Some note",
"completed": False,
}
```
โ๏ธ โซ๏ธ ๐ซ โ๏ธ `id` ๐.
๐ฅ โ ๐ `dict`, ๐ ๐ ๐-๐ฒ ๐ซ โช๏ธโก๏ธ `note.dict()` โฎ๏ธ:
```Python
{**note.dict()}
```
`**note.dict()` "unpacks" the key value pairs directly, so, `{**note.dict()}` would be, more or less, a copy of `note.dict()`.
& โคด๏ธ, ๐ฅ โ ๐ ๐ `dict`, โ โ1๏ธโฃ ๐-๐ฒ ๐ซ: `"id": last_record_id`:
```Python
{**note.dict(), "id": last_record_id}
```
, ๐ ๐ ๐จ ๐ ๐ณ ๐:
```Python
{
"id": 1,
"text": "Some note",
"completed": False,
}
```
## โ
โซ๏ธ
๐ ๐ช ๐ ๐ ๐, & ๐ ๐ฉบ http://127.0.0.1:8000/docs.
๐ค ๐ ๐ช ๐ ๐ ๐ ๐ ๏ธ ๐ & ๐ โฎ๏ธ โซ๏ธ:
## ๐
โน
๐ ๐ช โ ๐
๐ `encode/databases` ๐ฎ ๐ ๐.