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

grillazz/fastapi-sqlalchemy-asyncpg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

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

This example demonstrates the seamless integration of FastAPI, a modern, high-performance web framework, with Pydantic 2.0, a robust and powerful data validation library. The integration is further enhanced by the use of SQLAlchemy ORM, a popular and feature-rich Object-Relational Mapping tool, and PostgreSQL16 relational database.

The entire stack is connected using the asyncpg Database Client Library, which provides a robust and efficient way to interact with PostgreSQL databases in Python, leveraging the power of asyncio and event loops.

Notably, this example showcases the latest and greatest versions of SQLAlchemy and psycopg, which are renowned for their robustness, power, and speed. The inclusion of FastAPI adds a modern, fast, and high-performance web framework to the mix allowing for the rapid development of APIs with Python 3.8+.

FastAPI has received significant recognition in the industry, including a review on thoughtworks Technology Radar in April 2021, where it was classified as a Trial technology, with comments praising its performance, ease of use, and features such as API documentation using OpenAPI. Additionally, FastAPI was recognized in the Python Developers Survey 2022 Results, conducted by the Python Software Foundation and JetBrains, where it was reported that 1 in 4 Python developers use FastAPI, with a 4 percentage point increase from the previous year.

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.

1. make docker-build
2. make docker-up > alternatively > make docker-up-granian
3. make docker-apply-db-migrations
4. make docker-feed-database

(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

(back to top)

Rainbow logs with rich 🌈

To enhance the developer experience when viewing logs with extensive information from multiple emitters (which are particularly useful during development), this project uses the rich library. Event with the superpowers of rich, reading logs can be challenging. The rich library is highly beneficial, but integrating it properly as a logger object and maintaining it as a singleton took some effort.

To address the following needs:

  • Difficulty in finding specific information in logs.
  • Avoiding the complexity of setting up an ELK stack for log management.
  • Speeding up the debugging process.

he following steps were taken to integrate rich into the project:

  1. Configure emitters using the logging-uvicorn.json or use logging-granian.json for granian
  2. Eliminate duplicates, such as SQLAlchemy echo, by using separate handlers.
  3. Maintain the logger as a singleton to prevent multiple instances.
  4. Add the --log-config ./logging-uvicorn.json parameter to Uvicorn or --log-config ./logging-granian.json to Granian.

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.12 && pyenv local 3.12
poetry install --with dev

Hope you enjoy it.

Import xlsx files with polars and calamine

Power of Polars Library in data manipulation and analysis. It uses the polars library to read the Excel data into a DataFrame by passing the bytes to the pl.read_excel() function - https://docs.pola.rs/py-polars/html/reference/api/polars.read_excel.html In pl.read_excel() β€œcalamine” engine can be used for reading all major types of Excel Workbook (.xlsx, .xlsb, .xls) and is dramatically faster than the other options, using the fastexcel module to bind calamine.

(back to top)

Worker aware async scheduler πŸ•

The project uses the APScheduler library to schedule tasks in the background. The APScheduler library is a powerful and flexible in-process task scheduler with Cron-like capabilities. It allows you to schedule jobs to run at specific times or intervals, and it supports multiple job stores, triggers, and executors. The library is designed to be easy to use and highly configurable, making it suitable for a wide range of use cases. It was added to project in version 4.0.0a5 with Redis as event broker and SQLAlchemy as data store.

(back to top)

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 πŸ”’ πŸ”‘
  • [OCT 21 2023] refactor shakespeare models to use sqlalchemy 2.0 ⏩
  • [FEB 1 2024] bump project to Python 3.12 ⏩
  • [MAR 15 2024] add polars and calamine to project 😻
  • [JUN 8 2024] implement asyncpg connection pool ⏩
  • [AUG 17 2024] granian use case implemented with docker compose and rich logger ⏩
  • [OCT 16 2024] apscheduler added to project ⏩

(back to top)