zhanymkanov / fastapi-best-practices

FastAPI Best Practices and Conventions we used at our startup
9.38k stars 700 forks source link

What are some best practices for Fastapi + Mongodb #34

Open bengabp opened 1 year ago

bengabp commented 1 year ago

Almost all of of here have used some ORM library like Sqlalchemy which is equivalent to how django does its things. I found myself using mongodb as the main database choice for most of my projects. Also in all company projects that Ive been working on since this year, I have not used any sql database. I usually use pymongo and Its always a hassle to configure everything for every new project. What ive done is create a DBConnection class which has all the collections I need as class attributes and possibly some methods does some db operations.

The problem

As you already figured out, Its nothing close to using an ORM, Are there some libraries that support ODM that i can use in fastapi like Sqlalchemy (when dealing with SQL). I mean relationsips, and most important being able to still my favourite mongodb operation which is the aggregate searches.

yallxe commented 1 year ago

If you're looking for an ODM compatible with FastAPI and Pydantic, you would definitively like this one: https://beanie-odm.dev/

Repo: https://github.com/roman-right/beanie

bengabp commented 1 year ago

Hi @yallxe Thank you for your recommendation. After i created this issue, I also came across this beanie library and I will definitely check it out and I will try to provide valuable information about it in this issue.