Skip to content

Commit 1cd613e

Browse files
committed
convert README to web docs.
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent 7143f25 commit 1cd613e

File tree

8 files changed

+694
-1
lines changed

8 files changed

+694
-1
lines changed

docs/explanation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Code Explanation
2+
3+
!!! note "Under Construction"
4+
This section is still to be written.

docs/index.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Async SQLAlchemy 2 with FastAPI
2+
3+
## Introduction
4+
5+
I've been using [FastAPI][fastapi]{:target="_blank"} and
6+
[SQLAlchemy][sqla]{:target="_blank"} combined with
7+
[encode/databases][databases]{:target="_blank"} for a while now.
8+
9+
The `databases` package is a great wrapper around `SQLAlchemy` that allows you
10+
to use async/await with SQLAlchemy.
11+
12+
However, this does not seem be be actively maintained anymore. So I decided to
13+
give the new [Async SQLAlchemy][async-sqla]{:target="_blank"} a try instead.
14+
15+
This repository contains a very simple example how to use FastAPI with Async
16+
SQLAlchemy 2.0, in `ORM` mode. I'll probably add an example for `Core` mode
17+
also.
18+
19+
[fastapi]:https://fastapi.tiangolo.com/
20+
[sqla]: https://www.sqlalchemy.org/
21+
[databases]:https://www.encode.io/databases/
22+
[async-sqla]:https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html

docs/license.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# License
2+
3+
This project is licensed under the terms of the MIT license.
4+
5+
```pre
6+
--8<-- "LICENSE.txt"
7+
```

docs/usage.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Using this example
2+
3+
## Installation
4+
5+
Clone the repository and install the dependencies. This project uses
6+
[Poetry][poetry]{:target="_blank"} for dependency management which should be
7+
installed on your system first.
8+
9+
```console
10+
poetry install
11+
```
12+
13+
Then switch to the virtual environment:
14+
15+
```console
16+
poetry shell
17+
```
18+
19+
## Usage
20+
21+
Run the server using `Uvicorn`:
22+
23+
```console
24+
uvicorn main:app --reload
25+
```
26+
27+
> You can also run the server by just executing the `main.py` file:
28+
>
29+
> ```console
30+
> python main.py
31+
> ```
32+
33+
Then open your browser at [http://localhost:8000](http://localhost:8000).
34+
35+
There is only one endpoint available: `/users`. It returns a list of all users
36+
for a `GET` request and creates a new user for a `POST` request.
37+
38+
### Local Postgres server using Docker
39+
40+
This example uses [PostgreSQL][postgres]{:target="_blank"} as the database. If
41+
you dont have a local PostgreSQL database running, you can start one with
42+
[Docker][docker]{:target="_blank"} using the following command:
43+
44+
```console
45+
docker run \
46+
--rm \
47+
--name postgres \
48+
-p 5432:5432 \
49+
-e POSTGRES_USER=postgres \
50+
-e POSTGRES_PASSWORD=postgres \
51+
-e POSTGRES_DB=postgres \
52+
-d postgres
53+
```
54+
55+
This will run a PostgreSQL database in a Docker container in the background.
56+
When you are finished and want to stop the database, run:
57+
58+
```console
59+
docker stop postgres
60+
```
61+
62+
If needed, you can connect to the database managment by :
63+
64+
```console
65+
docker exec -it postgres psql -U postgres
66+
```
67+
68+
This will allow you to edit or delete the database or records.
69+
70+
### Use SQLite instead of PostgreSQL
71+
72+
For testing purposes, you can also use [SQLite][sqlite]{:target="_blank"}
73+
instead of PostgreSQL. To do so, open the `db.py` file and comment out the
74+
PostgreSQL database in the `DATABASE_URL` environment variable and uncomment the
75+
SQLite database.
76+
77+
```python
78+
# DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres"
79+
DATABASE_URL = "sqlite+aiosqlite:///./test.db"
80+
```
81+
82+
[poetry]: https://python-poetry.org/
83+
[postgres]:https://www.postgresql.org/
84+
[docker]:https://www.docker.com/
85+
[sqlite]:https://www.sqlite.org/

mkdocs.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
site_name: Async SQLAlchemy 2 with FastAPI
2+
3+
repo_url: https://github.com/seapagan/fastapi_async_sqlalchemy2_example
4+
repo_name: fastapi_async_sqlalchemy2
5+
6+
theme:
7+
name: material
8+
features:
9+
- navigation.footer
10+
- navigation.expand
11+
12+
extra:
13+
social:
14+
- icon: material/web
15+
link: https://www.gnramsay.com
16+
- icon: fontawesome/brands/github
17+
link: https://github.com/seapagan
18+
- icon: fontawesome/brands/twitter
19+
link: https://twitter.com/gnramsay-dev
20+
21+
plugins:
22+
- search
23+
- minify:
24+
minify_html: true
25+
minify_css: true
26+
minify_js: true
27+
htmlmin_opts:
28+
remove_comments: true
29+
30+
markdown_extensions:
31+
- admonition
32+
- pymdownx.snippets
33+
- pymdownx.highlight:
34+
linenums: false
35+
auto_title: false
36+
- attr_list
37+
38+
nav:
39+
- Home: index.md
40+
- Usage: usage.md
41+
- Code Description: explanation.md
42+
- License: license.md

models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Define Models used in this example."""
12
from sqlalchemy import Column, Integer, String
23

34
from db import Base

poetry.lock

Lines changed: 528 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ asyncpg = "^0.27.0"
1313
sqlalchemy = {extras = ["asyncio"], version = "^2.0.16"}
1414
uvicorn = {extras = ["standard"], version = "^0.22.0"}
1515
aiosqlite = "^0.19.0"
16+
mkdocs = "^1.4.3"
17+
mkdocs-material = "^9.1.16"
18+
mkdocs-minify-plugin = "^0.6.4"
19+
pymdown-extensions = "^10.0.1"
20+
pygments = "^2.15.1"
1621

1722
[tool.poetry.group.dev.dependencies]
1823
# linting and formatting tools

0 commit comments

Comments
 (0)