Skip to content

Integration of FastAPI framework supported by Pydantic with SQLAlchemy ORM and PostgreSQL on asyncpg driver

License

Notifications You must be signed in to change notification settings

Rox87/fastapi.sqlalchemy.asyncpg

 
 

Repository files navigation

fastapi-sqlalchemy-asyncpg

Contributors Forks Stargazers Issues MIT License LinkedIn

fastapi-sqlalchemy-asyncpg

Table of Contents
  1. About The Project
  2. Getting Started
  3. Acknowledgments

About The Project

Example of FastAPI integration supported by almighty Pydantic 2.0 with SQLAlchemy ORM and PostgreSQL connected via fastest Database Client Library for python/asyncio asyncpg.

Beside of using latest and greatest version of SQLAlchemy with it robustness, powerfulness and speed of asyncpg there is FastAPI (modern, fast (high-performance), web framework for building APIs with Python 3.8+ based on standard Python type hints.) already reviewed on thoughtworks and noted in Python Developers Survey 2021 Results as the fifth official annual Python Developers Survey, conducted as a collaborative effort between the Python Software Foundation and JetBrains.

Built With

FastAPI Pydantic SQLAlchemy Uvicorn pytest asyncpg alembic rich

(back to top)

Getting Started

Make will help you

To build , run and test and more ... use magic of make help to play with this project.

make help

and you receive below list:

build                Build project with compose
clean                Clean Reset project containers and volumes with compose
feed_db              create database objects and insert data
format               Format project code.
help                 Show this help
lint                 Lint project code.
migrate-apply        apply alembic migrations to database/schema
migrate-create       create new alembic migration
py-upgrade           Upgrade project py files with pyupgrade library for python version 3.10
requirements         Refresh requirements.txt from pipfile.lock
safety               Check project and dependencies with safety https://github.com/pyupio/safety
slim-build           with power of docker-slim build smaller and safer images
test                 Run project tests
up                   Run project with compose

(back to top)

How to feed database

It took me a while to find nice data set. Hope works of Shakespeare as example will be able to cover first part with read only declarative base configuration and all type of funny selects :) Data set is coming form https://github.com/catherinedevlin/opensourceshakespeare Next models were generated with https://github.com/agronholm/sqlacodegen And after some tweaking I got desired result

(back to top)

Rainbow logs with rich 🌈

To deliver better user(developer) experience when watching logs with tons of information from few emitters (which are really needy on development stage) project is using rich library. Event with rich superpowers reading logs is not easy. Found rich really nice - but it took time to learn how to integrate it as logger object properly and keep it as singleton.

To address below needs:

  • it is hard to find what I am looking for even with glasses on.
  • don’t want to hire ELK to be able to use logs.
  • want to move fast enough with debugging.

Below steps were done to integrate rich into project.

  1. Configure emitters with config.ini
  2. Eliminate duplicates i.e. sqlalchemy echo by separate handlers
  3. Keep logger as singleton pattern to avoid multiple instances
  4. add uvicorn parameter --log-config config.ini

sample-logs-with-rich

(back to top)

Setup User Auth

Setup user authentication with JWT and Redis as token storage.

Local development with poetry

pyenv install 3.11 && pyenv local 3.11
poetry install

Hope you enjoy it.

Acknowledgments

Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!

(back to top)

Change Log

  • [long time ago...] it was a long time ago in galaxy far far away...
  • [JUN 4 2022] alembic migrations added to project
  • [JUN 6 2022] initial dataset for shakespeare models
  • [OCT 3 2022] poetry added to project
  • [NOV 12 2022] ruff implemented to project as linting tool
  • [FEB 14 2023] bump project to Python 3.11
  • [APR 10 2023] implement logging with rich
  • [APR 28 2023] Rainbow logs with rich 🌈
  • [JUL 7 2023] migrate to pydantic 2.0 ⏩
  • [JUL 25 2023] add user authentication with JWT and Redis as token storage 🔒 🔑
  • [SEP 2 2023] add passlib and bcrypt for password hashing 🔒 🔑

(back to top)

About

Integration of FastAPI framework supported by Pydantic with SQLAlchemy ORM and PostgreSQL on asyncpg driver

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 89.9%
  • Makefile 5.1%
  • Dockerfile 3.9%
  • Mako 1.1%