1
- # Simple example how to use FastAPI with Async SQLAlchemy 2.0
1
+ # Simple example how to use FastAPI with Async SQLAlchemy 2.0 <!-- omit from toc -->
2
+
3
+ - [ Introduction] ( #introduction )
4
+ - [ Installation] ( #installation )
5
+ - [ Usage] ( #usage )
6
+ - [ Local Postgres server using Docker] ( #local-postgres-server-using-docker )
7
+ - [ License] ( #license )
8
+
9
+ ## Introduction
2
10
3
11
I've been using [ FastAPI] ( https://fastapi.tiangolo.com/ ) and
4
12
[ SQLAlchemy] ( https://www.sqlalchemy.org/ ) combined with
@@ -21,43 +29,60 @@ Clone the repository and install the dependencies. This project uses
21
29
[ Poetry] ( https://python-poetry.org/ ) for dependency management which should be
22
30
installed on your system first.
23
31
24
- ``` bash
32
+ ``` console
25
33
poetry install
26
34
```
27
35
28
36
Then switch to the virtual environment:
29
37
30
- ``` bash
38
+ ``` console
31
39
poetry shell
32
40
```
33
41
34
42
## Usage
35
43
44
+ Run the server using ` Uvicorn ` :
45
+
46
+ ``` console
47
+ uvicorn main:app --reload
48
+ ```
49
+
50
+ Then open your browser at [ http://localhost:8000 ] ( http://localhost:8000 ) .
51
+
52
+ There is only one endpoint available: ` /users ` . It returns a list of all users
53
+ for a ` GET ` request and creates a new user for a ` POST ` request.
54
+
55
+ ### Local Postgres server using Docker
56
+
36
57
This example uses [ PostgreSQL] ( https://www.postgresql.org/ ) as the database. If
37
58
you dont have a local PostgreSQL database running, you can start one with
38
59
[ Docker] ( https://www.docker.com ) using the following command:
39
60
40
- ``` bash
41
- docker exec -it postgres psql -U postgres
61
+ ``` console
62
+ docker run \
63
+ --rm \
64
+ --name postgres \
65
+ -p 5432:5432 \
66
+ -e POSTGRES_USER=postgres \
67
+ -e POSTGRES_PASSWORD=postgres \
68
+ -e POSTGRES_DB=postgres \
69
+ -d postgres
42
70
```
43
71
44
72
This will run a PostgreSQL database in a Docker container in the background.
45
73
When you are finished and want to stop the database, run:
46
74
47
- ``` bash
75
+ ``` console
48
76
docker stop postgres
49
77
```
50
78
51
- Run the server using ` Uvicorn ` :
79
+ If needed, you can connect to the database managment by :
52
80
53
- ``` bash
54
- uvicorn main:app --reload
81
+ ``` console
82
+ docker exec -it postgres psql -U postgres
55
83
```
56
84
57
- Then open your browser at [ http://localhost:8000 ] ( http://localhost:8000 ) .
58
-
59
- There is only one endpoint available: ` /users ` . It returns a list of all users
60
- for a ` GET ` request and creates a new user for a ` POST ` request.
85
+ This will allow you to edit or delete the database or records.
61
86
62
87
## License
63
88
0 commit comments