Skip to content

Commit 1bacca9

Browse files
committed
docs: some tweaks to existing wording
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent 63245b6 commit 1bacca9

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

docs/explanation/database.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres"
3535
We set a variable to be used later which contains the database URL. We are using
3636
PostgreSQL, but you can use any database that SQLAlchemy supports. The commented
3737
out line is for SQLite, which is a good choice for testing. You can comment out
38-
the PostgreSQL line (**13**)and uncomment the SQLite line (**14**)to use SQLite
39-
instead.
38+
the PostgreSQL line (**13**) and uncomment the SQLite line (**14**) to use
39+
SQLite instead.
4040

4141
This is a basic connection string, in reality you would want to use environment
4242
variables to store the user/password and database name.
@@ -134,7 +134,7 @@ async def init_models() -> None:
134134
In a real-life example we would use Alembic to manage migrations.
135135
"""
136136
async with async_engine.begin() as conn:
137-
# await conn.run_sync(Base.metadata.drop_all) # noqa: ERA001
137+
# await conn.run_sync(Base.metadata.drop_all)
138138
await conn.run_sync(Base.metadata.create_all)
139139
```
140140

@@ -144,5 +144,9 @@ On line **57** we use the `run_sync` method to run the `create_all` method of
144144
the `Base.metadata` object (a syncronous function). This will create all of the
145145
tables defined in the models.
146146

147+
If you want to drop the tables and recreate them every time the server restarts,
148+
you can uncomment line **56**. This is obviously not much good for production
149+
use, but it can be useful for testing.
150+
147151
Next, we will look at the models themselves and the Schemas used to validate
148152
them within FastAPI.

docs/index.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,42 @@
22

33
## Introduction
44

5-
I've been using [FastAPI][fastapi]{:target="_blank"} and
5+
This repository contains a very simple example how to use FastAPI with Async
6+
SQLAlchemy 2.0, in `ORM` mode. I'll probably add an example for `Core` mode
7+
also. No effort has been made to make this a production ready application, it's
8+
just a simple demo since at the time of writing there were few clear examples of
9+
how to do this.
10+
11+
Last update 29th January 2024, and tested to work with the following versions:
12+
13+
- Python 3.9+
14+
- FastAPI 0.109.0
15+
- SQLAlchemy 2.0.25
16+
17+
## Why use Raw SQLAlchemy?
18+
19+
I was using [FastAPI][fastapi]{:target="_blank"} and
620
[SQLAlchemy][sqla]{:target="_blank"} combined with
7-
[encode/databases][databases]{:target="_blank"} for a while now.
21+
[encode/databases][databases]{:target="_blank"} for a while. This worked fine
22+
originally but I felt I needed a bit more control over the database session
23+
management.
824

9-
The `databases` package is a great wrapper around `SQLAlchemy` that allows you
10-
to use async/await with SQLAlchemy.
25+
!!! info
26+
The [databases][databases]{:target="_blank"} package is a great wrapper
27+
around `SQLAlchemy` that allows you to use async/await for database
28+
operations. It also has a nice way of managing the database session, which
29+
is why I used it originally.
1130

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.
31+
However, this did not seem be be actively maintained at the time, so I decided
32+
to give the newer [Async SQLAlchemy][async-sqla]{:target="_blank"} a try
33+
instead.
1434

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.
35+
This repository is the result of my exprimentation while converting my
36+
[FastAPI-template][fastapi-template]{:target="_blank"} project to use `Async
37+
SQLAlchemy` instead of `databases`.
1838

19-
[fastapi]:https://fastapi.tiangolo.com/
39+
[fastapi]: https://fastapi.tiangolo.com/
2040
[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
41+
[databases]: https://www.encode.io/databases/
42+
[async-sqla]: https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html
43+
[fastapi-template]: https://github.com/seapagan/fastapi-template

0 commit comments

Comments
 (0)