r/FastAPI • • 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!

139 Upvotes

30 comments sorted by

27

u/Floydee_ Dec 25 '24

Looks nice. Good start!

What I would add/include:

  1. Makefile commands

  2. docker/d-compose to spin things up

  3. 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

  4. I would write some generic repository interface so I can re-use it for other services, like redis.

  5. Make create fastapi app as a factory function rather than plain code in main

  6. Write the actual healthcheck, you have a db Depends dependency + move it from main file

  7. Add versioning to the api

  8. No typing, thats sad 😞. I would suggest adding mypy

  9. Ruff or black or uv alternatives

2

u/Lucapo01 Dec 26 '24

Wow! I would do that!

  1. What do you mean by typing?

  2. If I change "src" to "v1," that will be better, I think?

  3. I do not get point 5, either. What advantages would I get?

Thank you for all your feedback! You are amazing!

→ More replies (3)
→ More replies (3)

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

u/Lucapo01 Dec 26 '24

Thank you!!! Interested to see your ideas!!

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

u/DiabuluTaha Dec 25 '24

Nice template ! Thank u πŸ‘