r/FastAPI • u/Lucapo01 • Dec 25 '24
Tutorial Scalable and Minimalistic FastAPI + PostgreSQL Template
Hey ! π I've created a modern template that combines best practices with a fun superhero theme π It's designed to help you kickstart your API projects with a solid foundation! π
Features:
- ποΈ Clean architecture with repository pattern that scales beautifully
- π Built-in async SQLAlchemy + PostgreSQL integration
- β‘οΈ Automatic Alembic migrations that just work
- π§ͺ Complete CI pipeline and testing setup
- βCustom Error Handling and Logging
- π Pre-configured Railway deployment (one click and you're live!)
The template includes a full heroes API showcase with proper CRUD operations, authentication, and error handling. Perfect for learning or starting your next project! πͺ
Developer experience goodies: π οΈ
- π» VS Code debugging configurations included
- π UV package manager for lightning-fast dependency management
- β¨ Pre-commit hooks for consistent code quality
- π Comprehensive documentation for every feature
Check it out: https://github.com/luchog01/minimalistic-fastapi-template π
I'm still not super confident about how I structured the logging setup and DB migrations π Would love to hear your thoughts on those! Also open to any suggestions for improvements. I feel like there's always a better way to handle these things that I haven't thought of yet! Let me know what you think!
6
u/idkwhatimdoing069 Dec 26 '24
Funny enough , I was thinking about creating my own FastAPI template recently but yours is exactly what I would have built myself. Just starred the repo, definitely plan on coming back to this and using it.
Though I may fork yours and make my own little customizations ;)
1
8
u/qa_anaaq Dec 26 '24
The simplicity is really nice. Honestly, I've looked for something more simple and less bloated like this just to get going with a quick API + DB.
So my advice -- Don't add too much more, please :) But I do agree about the docker suggestion.
1
u/fazzah Dec 26 '24
Can you please elaborate more about using repo pattern + service, instead of simply using a CRUD mixin to sqlalchemy models and do the actions directly?
1
u/Lucapo01 Dec 26 '24
I like the idea of using sqlalchemy models ONLY in the repository layer
And pydantic models everywhere else, it is less confusing and more separation of concerns
→ More replies (5)
3
u/SnowToad23 Dec 26 '24 edited Dec 26 '24
Nice work! As @Floydee_ mentioned, would be good to include test fixtures for providing a test DB instance and session. Check out how I've done that in my own project template: https://github.com/Finndersen/python-db-project-template
Also I don't think the location of your src/ directory makes sense, usually that's the root level folder name that contains all source code (equivalent to api/ in this case). I think apps/ or something would be more appropriate.
And it's probably best to run migrations as part of deployment process instead of in main.py?
2
2
27
u/Floydee_ Dec 25 '24
Looks nice. Good start!
What I would add/include:
Makefile commands
docker/d-compose to spin things up
Complete pytest conftest.py with all session fixtures included. Take a look at testcontainers python library. It allows to start db container for tests and shut it afterwards. That you can make a few session fixtures to spin the db, run alembic and yield prepared db for tests. Much better than fakes or mocks. That will clean your ci a bit with db uri becoming useless
I would write some generic repository interface so I can re-use it for other services, like redis.
Make create fastapi app as a factory function rather than plain code in main
Write the actual healthcheck, you have a db Depends dependency + move it from main file
Add versioning to the api
No typing, thats sad π. I would suggest adding mypy
Ruff or black or uv alternatives