| 
2 | 2 | 
 
  | 
3 | 3 | ## Introduction  | 
4 | 4 | 
 
  | 
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  | 
6 | 20 | [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.  | 
8 | 24 | 
 
  | 
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.  | 
11 | 30 | 
 
  | 
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.  | 
14 | 34 | 
 
  | 
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`.  | 
18 | 38 | 
 
  | 
19 |  | -[fastapi]:https://fastapi.tiangolo.com/  | 
 | 39 | +[fastapi]: https://fastapi.tiangolo.com/  | 
20 | 40 | [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