fastapi-alembic-sqlmodel-async
fastapi-alembic-sqlmodel-async copied to clipboard
This is a project template which uses FastAPI, Pydantic 2.0, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.
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 project using Docker compose
docker-compose -f docker-compose-dev.yml up --build
Setup database with initial data
This creates a sample users on database.
docker-compose -f docker-compose-dev.yml exec fastapi_server python app/initial_data.py
- Admin credentials -> username: [email protected] and password: admin
- Manager credentials -> username: [email protected] and password: admin
- User credentials -> username: [email protected] and password: admin
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
ERD Database model
Containers architecture
As this project uses traefik as reverse proxy, which uses namespaces routing, you can access to the documentation with the following path http://fastapi.localhost/docs
Preview
Traefik Dashboard
Traefik has been configurated as reverse proxy on the ingress of the project; you can access to 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 of how to do it here
Static files
All files on static folder will be server by nginx container as static files. You can check it with this link http://nginx.localhost/1.png
Run Alembic migrations (Only if you change the DB model)
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
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.
docker-compose up --build
Inspiration and References
- full-stack-fastapi-postgresql.
- fastapi-sqlmodel-alembic.
- sqlmodel-tutorial.
- fastapi-pagination.
- fastapi-cache.
- fastapi-keycloak.
- fastapi-async-sqlalchemy.
TODO List:
- [x] Add Custom Response model
- [x] Create sample one to many relationship
- [x] Create sample many to many relationship
- [x] Add JWT authentication
- [x] Add Pagination
- [x] Add User birthday field with timezone
- [x] Add reverse proxy (traefik) with docker compose
- [x] Add static server with nginx
- [x] Add basic RBAC (Role base access control)
- [x] Add sample heroes, teams and groups on init db
- [x] Add cache configuration using fastapi-cache2 and redis
- [x] Create a global database pool of sessions to avoid to pass the session as dependency injection on each handle
- [ ] Add one to one relationship sample
- [ ] 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
- This project is licensed under the terms of the MIT license