Giter Site home page Giter Site logo

paulovitorweb / fastapi-alembic-sqlmodel-async Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jonra1993/fastapi-alembic-sqlmodel-async

0.0 0.0 0.0 6.33 MB

This is a project template which uses FastAPI, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.

License: MIT License

Shell 1.04% Python 96.05% Makefile 1.75% Mako 0.42% Dockerfile 0.74%

fastapi-alembic-sqlmodel-async's Introduction

Async configuration for FastAPI and SQLModel

This is a project template which uses FastAPI, Alembic and async SQLModel as ORM. It shows a complete async CRUD template using authentication.

Set environment variables

Create an .env file on root folder and copy the content from .env.example. Feel free to change it according to your own configuration.

Run the project using Docker containers and forcing build containers

Using docker compose command

docker compose -f docker-compose-dev.yml up --build

Using Makefile command

make run-dev-build

Run project using Docker containers

Using docker compose command

docker compose -f docker-compose-dev.yml up

Using Makefile command

make run-dev

Setup database with initial data

This creates sample users on database.

Using docker compose command

docker compose -f docker-compose-dev.yml exec fastapi_server python app/initial_data.py

Using Makefile command

make init-db

Any of the above commands creates three users with the following passwords:

You can connect to the Database using pgAdmin4 and use the credentials from .env file. Database port on local machine has been configured to 5454 on docker-compose-dev.yml file

(Optional) If you prefer you can run pgAdmin4 on a docker container using the following commands, they should executed on different terminals:

Starts pgadmin

make run-pgadmin

Load server configuration (It is required just the first time)

make load-server-pgadmin

This starts pgamin in http://localhost:15432.

ERD Database model

Containers architecture

As this project uses traefik as a reverse proxy, which uses namespaces routing, you can access the documentation with the following path http://fastapi.localhost/docs

Preview

Traefik Dashboard

Traefik has been configurated as a reverse proxy on the ingress of the project; you can access Traefik Dashboard using the following link http://traefik.localhost/. You should use username: test and pass: test. If you want to change the password, you can find more information on how to do it here

Static files

All files on static folder will be served by nginx container as static files. You can check it with this link http://nginx.localhost/1.png

Minio server

This template allows users can upload their photos. The images are stored using the open source Object Storage Service (OSS) minio, which provides storage of images using buckets in a secure way through presigned URLs.

  • Minio credentials -> username: minioadmin and password: minioadmin

Run Alembic migrations (Only if you change the DB model)

Using docker compose command

docker compose -f docker-compose-dev.yml exec fastapi_server alembic revision --autogenerate
docker compose -f docker-compose-dev.yml exec fastapi_server alembic upgrade head

Using Makefile command

make add-dev-migration

Production Deployment

Remember to use a persistant PostgreSQL database, update the new credentials on .env file and use this command to run the project in a production environment. For testing this configuration on localhost you can uncomment the database container and depends_on of fastapi container otherwise it will not work on a local environment.

Using docker compose command

docker compose up --build

Database unique IDs

Generating and using unique IDs is a really important desicion when starting a new project and its most common use is as primary keys for database tables. This project uses a custom UUID7 Draft04 implementation to make it simple to use and take advantage of UUID type of PostgreSQL. UUID7 combines timestamp with random data in order to help to convert data into time-stamped sequencially. If you are looking for another alternatives for tables IDs like Snowflakes, ULID, KSUID, pushID, xid among others you can check these references.

Code Style

Code style: black

To ensure a standardized code style this project uses black and flake8. If you want to change the config rules you can edit flake8 rules in the file setup.cfg or black in the file pyproject.toml.

To reformat files execute the following command

make formatter

To run lint, you can run the following command:

make lint

Inspiration and References

TODO List:

  • Add Custom Response model
  • Create sample one to many relationship
  • Create sample many to many relationship
  • Add JWT authentication
  • Add Pagination
  • Add User birthday field with timezone
  • Add reverse proxy (traefik) with docker compose
  • Add static server with nginx
  • Add basic RBAC (Role base access control)
  • Add sample heroes, teams and groups on init db
  • Add cache configuration using fastapi-cache2 and redis
  • Create a global database pool of sessions to avoid to pass the session as dependency injection on each handle
  • Refactor tablename to Pascal case
  • Add one to one relationship sample
  • Add sample to upload images and store them using minio
  • Invalidate access and refresh tokens when the password is changed using Redis
  • Add shortcuts using a Makefile
  • Add sample async, sync and concurrent functions using asyncer
  • Add Black formatter and flake8 lint (Rasa as reference)
  • Add testing
  • Add static code analysis using SonarQube
  • Install pg_trgm by code and add a query for smart search of users by name
  • Add Enum sample column
  • Add jsonb field on table sample
  • Add AuthN and AuthZ using Keycloak
  • Add instructions for production deployment using github actions and dockerhub (CI/CD)
  • Convert repo into template using cookiecutter
  • Add Celery sample for tasks

PR are welcome ❤️

License

License

  • This project is licensed under the terms of the MIT license

fastapi-alembic-sqlmodel-async's People

Contributors

bazylhorsey avatar dongfengweixiao avatar hambergerpls avatar jamazi avatar jonra1993 avatar stefan2409 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.